@@ -1390,6 +1390,95 @@ void main() {
1390
1390
});
1391
1391
});
1392
1392
1393
+ group ('SliverAppBar.forceMaterialTransparency' , () {
1394
+ Material getSliverAppBarMaterial (WidgetTester tester) {
1395
+ return tester.widget <Material >(find
1396
+ .descendant (of: find.byType (SliverAppBar ), matching: find.byType (Material ))
1397
+ .first);
1398
+ }
1399
+
1400
+ // Generates a MaterialApp with a SliverAppBar in a CustomScrollView.
1401
+ // The first cell of the scroll view contains a button at its top, and is
1402
+ // initially scrolled so that it is beneath the SliverAppBar.
1403
+ Widget buildWidget ({
1404
+ required bool forceMaterialTransparency,
1405
+ required VoidCallback onPressed
1406
+ }) {
1407
+ const double appBarHeight = 120 ;
1408
+ return MaterialApp (
1409
+ home: Scaffold (
1410
+ body: CustomScrollView (
1411
+ controller: ScrollController (initialScrollOffset: appBarHeight),
1412
+ slivers: < Widget > [
1413
+ SliverAppBar (
1414
+ collapsedHeight: appBarHeight,
1415
+ expandedHeight: appBarHeight,
1416
+ pinned: true ,
1417
+ elevation: 0 ,
1418
+ backgroundColor: Colors .transparent,
1419
+ forceMaterialTransparency: forceMaterialTransparency,
1420
+ title: const Text ('AppBar' ),
1421
+ ),
1422
+ SliverList (
1423
+ delegate: SliverChildBuilderDelegate ((BuildContext context, int index) {
1424
+ return SizedBox (
1425
+ height: appBarHeight,
1426
+ child: index == 0
1427
+ ? Align (
1428
+ alignment: Alignment .topCenter,
1429
+ child: TextButton (onPressed: onPressed, child: const Text ('press' )))
1430
+ : const SizedBox (),
1431
+ );
1432
+ },
1433
+ childCount: 20 ,
1434
+ ),
1435
+ ),
1436
+ ]),
1437
+ ),
1438
+ );
1439
+ }
1440
+
1441
+ testWidgets (
1442
+ 'forceMaterialTransparency == true allows gestures beneath the app bar' , (WidgetTester tester) async {
1443
+ bool buttonWasPressed = false ;
1444
+ final Widget widget = buildWidget (
1445
+ forceMaterialTransparency: true ,
1446
+ onPressed: () { buttonWasPressed = true ; },
1447
+ );
1448
+ await tester.pumpWidget (widget);
1449
+
1450
+ final Material material = getSliverAppBarMaterial (tester);
1451
+ expect (material.type, MaterialType .transparency);
1452
+
1453
+ final Finder buttonFinder = find.byType (TextButton );
1454
+ await tester.tap (buttonFinder);
1455
+ await tester.pump ();
1456
+ expect (buttonWasPressed, isTrue);
1457
+ });
1458
+
1459
+ testWidgets (
1460
+ 'forceMaterialTransparency == false does not allow gestures beneath the app bar' , (WidgetTester tester) async {
1461
+ // Set this, and tester.tap(warnIfMissed:false), to suppress
1462
+ // errors/warning that the button is not hittable (which is expected).
1463
+ WidgetController .hitTestWarningShouldBeFatal = false ;
1464
+
1465
+ bool buttonWasPressed = false ;
1466
+ final Widget widget = buildWidget (
1467
+ forceMaterialTransparency: false ,
1468
+ onPressed: () { buttonWasPressed = true ; },
1469
+ );
1470
+ await tester.pumpWidget (widget);
1471
+
1472
+ final Material material = getSliverAppBarMaterial (tester);
1473
+ expect (material.type, MaterialType .canvas);
1474
+
1475
+ final Finder buttonFinder = find.byType (TextButton );
1476
+ await tester.tap (buttonFinder, warnIfMissed: false );
1477
+ await tester.pump ();
1478
+ expect (buttonWasPressed, isFalse);
1479
+ });
1480
+ });
1481
+
1393
1482
testWidgets ('AppBar dimensions, with and without bottom, primary' , (WidgetTester tester) async {
1394
1483
const MediaQueryData topPadding100 = MediaQueryData (padding: EdgeInsets .only (top: 100.0 ));
1395
1484
@@ -3760,4 +3849,78 @@ void main() {
3760
3849
expect (tester.getTopLeft (find.byKey (titleKey)).dx, leadingWidth + 16.0 );
3761
3850
expect (tester.getSize (find.byKey (leadingKey)).width, leadingWidth);
3762
3851
});
3852
+
3853
+ group ('AppBar.forceMaterialTransparency' , () {
3854
+ Material getAppBarMaterial (WidgetTester tester) {
3855
+ return tester.widget <Material >(find
3856
+ .descendant (of: find.byType (AppBar ), matching: find.byType (Material ))
3857
+ .first);
3858
+ }
3859
+
3860
+ // Generates a MaterialApp with an AppBar with a TextButton beneath it
3861
+ // (via extendBodyBehindAppBar = true).
3862
+ Widget buildWidget ({
3863
+ required bool forceMaterialTransparency,
3864
+ required VoidCallback onPressed
3865
+ }) {
3866
+ return MaterialApp (
3867
+ home: Scaffold (
3868
+ extendBodyBehindAppBar: true ,
3869
+ appBar: AppBar (
3870
+ forceMaterialTransparency: forceMaterialTransparency,
3871
+ elevation: 3 ,
3872
+ backgroundColor: Colors .red,
3873
+ title: const Text ('AppBar' ),
3874
+ ),
3875
+ body: Align (
3876
+ alignment: Alignment .topCenter,
3877
+ child: TextButton (
3878
+ onPressed: onPressed,
3879
+ child: const Text ('press me' ),
3880
+ ),
3881
+ ),
3882
+ ),
3883
+ );
3884
+ }
3885
+
3886
+ testWidgets (
3887
+ 'forceMaterialTransparency == true allows gestures beneath the app bar' , (WidgetTester tester) async {
3888
+ bool buttonWasPressed = false ;
3889
+ final Widget widget = buildWidget (
3890
+ forceMaterialTransparency: true ,
3891
+ onPressed: () { buttonWasPressed = true ; },
3892
+ );
3893
+ await tester.pumpWidget (widget);
3894
+
3895
+ final Material material = getAppBarMaterial (tester);
3896
+ expect (material.type, MaterialType .transparency);
3897
+
3898
+ final Finder buttonFinder = find.byType (TextButton );
3899
+ await tester.tap (buttonFinder);
3900
+ await tester.pump ();
3901
+ expect (buttonWasPressed, isTrue);
3902
+ });
3903
+
3904
+ testWidgets (
3905
+ 'forceMaterialTransparency == false does not allow gestures beneath the app bar' , (WidgetTester tester) async {
3906
+ // Set this, and tester.tap(warnIfMissed:false), to suppress
3907
+ // errors/warning that the button is not hittable (which is expected).
3908
+ WidgetController .hitTestWarningShouldBeFatal = false ;
3909
+
3910
+ bool buttonWasPressed = false ;
3911
+ final Widget widget = buildWidget (
3912
+ forceMaterialTransparency: false ,
3913
+ onPressed: () { buttonWasPressed = true ; },
3914
+ );
3915
+ await tester.pumpWidget (widget);
3916
+
3917
+ final Material material = getAppBarMaterial (tester);
3918
+ expect (material.type, MaterialType .canvas);
3919
+
3920
+ final Finder buttonFinder = find.byType (TextButton );
3921
+ await tester.tap (buttonFinder, warnIfMissed: false );
3922
+ await tester.pump ();
3923
+ expect (buttonWasPressed, isFalse);
3924
+ });
3925
+ });
3763
3926
}
0 commit comments