@@ -1403,6 +1403,163 @@ void main() {
1403
1403
});
1404
1404
});
1405
1405
1406
+ group ('Asserts in jumpToPage and animateToPage methods works properly' , () {
1407
+ Widget createPageView ([PageController ? controller]) {
1408
+ return MaterialApp (
1409
+ home: Scaffold (
1410
+ body: PageView (
1411
+ controller: controller,
1412
+ children: < Widget > [
1413
+ Container (color: Colors .red),
1414
+ Container (color: Colors .green),
1415
+ Container (color: Colors .blue),
1416
+ ],
1417
+ ),
1418
+ ),
1419
+ );
1420
+ }
1421
+
1422
+ group ('One pageController is attached to multiple PageViews' , () {
1423
+ Widget createMultiplePageViews (PageController controller) {
1424
+ return MaterialApp (
1425
+ home: Scaffold (
1426
+ body: Column (
1427
+ children: < Widget > [
1428
+ Expanded (
1429
+ child: PageView (
1430
+ controller: controller,
1431
+ children: < Widget > [
1432
+ Container (color: Colors .red),
1433
+ Container (color: Colors .green),
1434
+ Container (color: Colors .blue),
1435
+ ],
1436
+ ),
1437
+ ),
1438
+ Expanded (
1439
+ child: PageView (
1440
+ controller: controller,
1441
+ children: < Widget > [
1442
+ Container (color: Colors .orange),
1443
+ Container (color: Colors .purple),
1444
+ Container (color: Colors .yellow),
1445
+ ],
1446
+ ),
1447
+ ),
1448
+ ],
1449
+ ),
1450
+ ),
1451
+ );
1452
+ }
1453
+
1454
+ testWidgets (
1455
+ 'animateToPage assertion is working properly when pageController is attached to multiple PageViews' ,
1456
+ (WidgetTester tester) async {
1457
+ final PageController controller = PageController ();
1458
+ addTearDown (controller.dispose);
1459
+ await tester.pumpWidget (createMultiplePageViews (controller));
1460
+
1461
+ expect (
1462
+ () => controller.animateToPage (
1463
+ 2 ,
1464
+ duration: const Duration (milliseconds: 300 ),
1465
+ curve: Curves .ease,
1466
+ ),
1467
+ throwsA (
1468
+ isAssertionError.having (
1469
+ (AssertionError error) => error.message,
1470
+ 'message' ,
1471
+ equals (
1472
+ 'Multiple PageViews are attached to '
1473
+ 'the same PageController.' ,
1474
+ ),
1475
+ ),
1476
+ ),
1477
+ );
1478
+ },
1479
+ );
1480
+
1481
+ testWidgets (
1482
+ 'jumpToPage assertion is working properly when pageController is attached to multiple PageViews' ,
1483
+ (WidgetTester tester) async {
1484
+ final PageController controller = PageController ();
1485
+ addTearDown (controller.dispose);
1486
+ await tester.pumpWidget (createMultiplePageViews (controller));
1487
+
1488
+ expect (
1489
+ () => controller.jumpToPage (2 ),
1490
+ throwsA (
1491
+ isAssertionError.having (
1492
+ (AssertionError error) => error.message,
1493
+ 'message' ,
1494
+ equals (
1495
+ 'Multiple PageViews are attached to '
1496
+ 'the same PageController.' ,
1497
+ ),
1498
+ ),
1499
+ ),
1500
+ );
1501
+ },
1502
+ );
1503
+ });
1504
+
1505
+ group ('PageController is attached or is not attached to PageView' , () {
1506
+ testWidgets ('Assert behavior of animateToPage works properly' , (WidgetTester tester) async {
1507
+ final PageController controller = PageController ();
1508
+ addTearDown (controller.dispose);
1509
+
1510
+ // pageController is not attached to PageView
1511
+ await tester.pumpWidget (createPageView ());
1512
+ expect (
1513
+ () => controller.animateToPage (
1514
+ 2 ,
1515
+ duration: const Duration (milliseconds: 300 ),
1516
+ curve: Curves .ease,
1517
+ ),
1518
+ throwsA (
1519
+ isAssertionError.having (
1520
+ (AssertionError error) => error.message,
1521
+ 'message' ,
1522
+ equals ('PageController is not attached to a PageView.' ),
1523
+ ),
1524
+ ),
1525
+ );
1526
+
1527
+ // pageController is attached to PageView
1528
+ await tester.pumpWidget (createPageView (controller));
1529
+ expect (
1530
+ () => controller.animateToPage (
1531
+ 2 ,
1532
+ duration: const Duration (milliseconds: 300 ),
1533
+ curve: Curves .ease,
1534
+ ),
1535
+ returnsNormally,
1536
+ );
1537
+ });
1538
+
1539
+ testWidgets ('Assert behavior of jumpToPage works properly' , (WidgetTester tester) async {
1540
+ final PageController controller = PageController ();
1541
+ addTearDown (controller.dispose);
1542
+
1543
+ // pageController is not attached to PageView
1544
+ await tester.pumpWidget (createPageView ());
1545
+ expect (
1546
+ () => controller.jumpToPage (2 ),
1547
+ throwsA (
1548
+ isAssertionError.having (
1549
+ (AssertionError error) => error.message,
1550
+ 'message' ,
1551
+ equals ('PageController is not attached to a PageView.' ),
1552
+ ),
1553
+ ),
1554
+ );
1555
+
1556
+ // pageController is attached to PageView
1557
+ await tester.pumpWidget (createPageView (controller));
1558
+ expect (() => controller.jumpToPage (2 ), returnsNormally);
1559
+ });
1560
+ });
1561
+ });
1562
+
1406
1563
testWidgets (
1407
1564
'Get the page value before the content dimension is determined,do not throw an assertion and return null' ,
1408
1565
(WidgetTester tester) async {
0 commit comments