Unverified Commit 6eb601bc authored by Kate Lovett's avatar Kate Lovett Committed by GitHub

Fix AppBar scrolledUnder initial state - the third (#102582)

parent 3edeb947
...@@ -809,11 +809,24 @@ class _AppBarState extends State<AppBar> { ...@@ -809,11 +809,24 @@ class _AppBarState extends State<AppBar> {
} }
void _handleScrollNotification(ScrollNotification notification) { void _handleScrollNotification(ScrollNotification notification) {
if (notification is ScrollUpdateNotification) { if (notification is ScrollUpdateNotification && notification.depth == 0) {
final bool oldScrolledUnder = _scrolledUnder; final bool oldScrolledUnder = _scrolledUnder;
_scrolledUnder = notification.depth == 0 final ScrollMetrics metrics = notification.metrics;
&& notification.metrics.extentBefore > 0 switch (metrics.axisDirection) {
&& notification.metrics.axis == Axis.vertical; case AxisDirection.up:
// Scroll view is reversed
_scrolledUnder = metrics.extentAfter > 0;
break;
case AxisDirection.down:
_scrolledUnder = metrics.extentBefore > 0;
break;
case AxisDirection.right:
case AxisDirection.left:
// Scrolled under is only supported in the vertical axis.
_scrolledUnder = false;
break;
}
if (_scrolledUnder != oldScrolledUnder) { if (_scrolledUnder != oldScrolledUnder) {
setState(() { setState(() {
// React to a change in MaterialState.scrolledUnder // React to a change in MaterialState.scrolledUnder
......
...@@ -9,6 +9,7 @@ import 'package:flutter/foundation.dart'; ...@@ -9,6 +9,7 @@ import 'package:flutter/foundation.dart';
import 'framework.dart'; import 'framework.dart';
import 'notification_listener.dart'; import 'notification_listener.dart';
import 'scroll_notification.dart'; import 'scroll_notification.dart';
import 'scroll_position.dart';
/// A [ScrollNotification] listener for [ScrollNotificationObserver]. /// A [ScrollNotification] listener for [ScrollNotificationObserver].
/// ///
...@@ -151,7 +152,19 @@ class ScrollNotificationObserverState extends State<ScrollNotificationObserver> ...@@ -151,7 +152,19 @@ class ScrollNotificationObserverState extends State<ScrollNotificationObserver>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return NotificationListener<ScrollNotification>( // A ScrollMetricsNotification allows listeners to be notified for an
// initial state, as well as if the content dimensions change without
// scrolling.
return NotificationListener<ScrollMetricsNotification>(
onNotification: (ScrollMetricsNotification notification) {
_notifyListeners(_ConvertedScrollMetricsNotification(
metrics: notification.metrics,
context: notification.context,
depth: notification.depth,
));
return false;
},
child: NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) { onNotification: (ScrollNotification notification) {
_notifyListeners(notification); _notifyListeners(notification);
return false; return false;
...@@ -160,6 +173,7 @@ class ScrollNotificationObserverState extends State<ScrollNotificationObserver> ...@@ -160,6 +173,7 @@ class ScrollNotificationObserverState extends State<ScrollNotificationObserver>
scrollNotificationObserverState: this, scrollNotificationObserverState: this,
child: widget.child, child: widget.child,
), ),
),
); );
} }
...@@ -170,3 +184,11 @@ class ScrollNotificationObserverState extends State<ScrollNotificationObserver> ...@@ -170,3 +184,11 @@ class ScrollNotificationObserverState extends State<ScrollNotificationObserver>
super.dispose(); super.dispose();
} }
} }
class _ConvertedScrollMetricsNotification extends ScrollUpdateNotification {
_ConvertedScrollMetricsNotification({
required super.metrics,
required super.context,
required super.depth,
});
}
...@@ -2621,47 +2621,63 @@ void main() { ...@@ -2621,47 +2621,63 @@ void main() {
expect(actionIconTheme.color, foregroundColor); expect(actionIconTheme.color, foregroundColor);
}); });
testWidgets('SliverAppBar.backgroundColor MaterialStateColor scrolledUnder', (WidgetTester tester) async { group('MaterialStateColor scrolledUnder', () {
const double collapsedHeight = kToolbarHeight; const double collapsedHeight = kToolbarHeight;
const double expandedHeight = 200.0; const double expandedHeight = 200.0;
const Color scrolledColor = Color(0xff00ff00); const Color scrolledColor = Color(0xff00ff00);
const Color defaultColor = Color(0xff0000ff); const Color defaultColor = Color(0xff0000ff);
await tester.pumpWidget( Finder findAppBarMaterial() {
MaterialApp( return find.descendant(of: find.byType(AppBar), matching: find.byType(Material)).first;
}
Color? getAppBarBackgroundColor(WidgetTester tester) {
return tester.widget<Material>(findAppBarMaterial()).color;
}
group('SliverAppBar', () {
Widget _buildSliverApp({
required double contentHeight,
bool reverse = false,
bool includeFlexibleSpace = false,
}) {
return MaterialApp(
home: Scaffold( home: Scaffold(
body: CustomScrollView( body: CustomScrollView(
reverse: reverse,
slivers: <Widget>[ slivers: <Widget>[
SliverAppBar( SliverAppBar(
elevation: 0, elevation: 0,
backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) { backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor; return states.contains(MaterialState.scrolledUnder)
? scrolledColor
: defaultColor;
}), }),
expandedHeight: expandedHeight, expandedHeight: expandedHeight,
pinned: true, pinned: true,
flexibleSpace: includeFlexibleSpace
? const FlexibleSpaceBar(title: Text('SliverAppBar'))
: null,
), ),
SliverList( SliverList(
delegate: SliverChildListDelegate( delegate: SliverChildListDelegate(
<Widget>[ <Widget>[
Container(height: 1200.0, color: Colors.teal), Container(height: contentHeight, color: Colors.teal),
], ],
), ),
), ),
], ],
), ),
), ),
),
); );
Finder findAppBarMaterial() {
return find.descendant(of: find.byType(AppBar), matching: find.byType(Material));
} }
Color? getAppBarBackgroundColor() { testWidgets('backgroundColor', (WidgetTester tester) async {
return tester.widget<Material>(findAppBarMaterial()).color; await tester.pumpWidget(
} _buildSliverApp(contentHeight: 1200.0)
);
expect(getAppBarBackgroundColor(), defaultColor); expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight); expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0)); TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
...@@ -2669,7 +2685,7 @@ void main() { ...@@ -2669,7 +2685,7 @@ void main() {
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), scrolledColor); expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, collapsedHeight); expect(tester.getSize(findAppBarMaterial()).height, collapsedHeight);
gesture = await tester.startGesture(const Offset(50.0, 300.0)); gesture = await tester.startGesture(const Offset(50.0, 300.0));
...@@ -2677,107 +2693,168 @@ void main() { ...@@ -2677,107 +2693,168 @@ void main() {
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), defaultColor); expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight); expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
}); });
testWidgets('SliverAppBar.backgroundColor with FlexibleSpace MaterialStateColor scrolledUnder', (WidgetTester tester) async { testWidgets('backgroundColor with FlexibleSpace', (WidgetTester tester) async {
const double collapsedHeight = kToolbarHeight;
const double expandedHeight = 200.0;
const Color scrolledColor = Color(0xff00ff00);
const Color defaultColor = Color(0xff0000ff);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( _buildSliverApp(contentHeight: 1200.0, includeFlexibleSpace: true)
home: Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
elevation: 0,
backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor;
}),
expandedHeight: expandedHeight,
pinned: true,
flexibleSpace: const FlexibleSpaceBar(
title: Text('SliverAppBar'),
),
),
SliverList(
delegate: SliverChildListDelegate(
<Widget>[
Container(height: 1200.0, color: Colors.teal),
],
),
),
],
),
),
),
); );
Finder findAppBarMaterial() { expect(getAppBarBackgroundColor(tester), defaultColor);
// There are 2 Material widgets below AppBar. The second is only added if expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
// flexibleSpace is non-null.
return find.descendant(of: find.byType(AppBar), matching: find.byType(Material)).first;
}
Color? getAppBarBackgroundColor() { TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
return tester.widget<Material>(findAppBarMaterial()).color; await gesture.moveBy(const Offset(0.0, -expandedHeight));
} await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, collapsedHeight);
expect(getAppBarBackgroundColor(), defaultColor); gesture = await tester.startGesture(const Offset(50.0, 300.0));
await gesture.moveBy(const Offset(0.0, expandedHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
});
testWidgets('backgroundColor - reverse', (WidgetTester tester) async {
await tester.pumpWidget(
_buildSliverApp(contentHeight: 1200.0, reverse: true)
);
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight); expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0)); TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(0.0, -expandedHeight)); await gesture.moveBy(const Offset(0.0, expandedHeight));
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), scrolledColor); expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, collapsedHeight); expect(tester.getSize(findAppBarMaterial()).height, collapsedHeight);
gesture = await tester.startGesture(const Offset(50.0, 300.0)); gesture = await tester.startGesture(const Offset(50.0, 300.0));
await gesture.moveBy(const Offset(0.0, -expandedHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
});
testWidgets('backgroundColor with FlexibleSpace - reverse', (WidgetTester tester) async {
await tester.pumpWidget(
_buildSliverApp(
contentHeight: 1200.0,
reverse: true,
includeFlexibleSpace: true,
)
);
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(0.0, expandedHeight)); await gesture.moveBy(const Offset(0.0, expandedHeight));
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), defaultColor); expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, collapsedHeight);
gesture = await tester.startGesture(const Offset(50.0, 300.0));
await gesture.moveBy(const Offset(0.0, -expandedHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight); expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
}); });
testWidgets('AppBar.backgroundColor MaterialStateColor scrolledUnder', (WidgetTester tester) async { testWidgets('backgroundColor - not triggered in reverse for short content', (WidgetTester tester) async {
const Color scrolledColor = Color(0xff00ff00); await tester.pumpWidget(
const Color defaultColor = Color(0xff0000ff); _buildSliverApp(contentHeight: 200, reverse: true)
);
// In reverse, the content here is not long enough to scroll under the app
// bar.
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
final TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(0.0, expandedHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
});
testWidgets('backgroundColor with FlexibleSpace - not triggered in reverse for short content', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( _buildSliverApp(
contentHeight: 200,
reverse: true,
includeFlexibleSpace: true,
)
);
// In reverse, the content here is not long enough to scroll under the app
// bar.
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
final TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(0.0, expandedHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
});
});
group('AppBar', () {
Widget _buildAppBar({
required double contentHeight,
bool reverse = false,
bool includeFlexibleSpace = false
}) {
return MaterialApp(
home: Scaffold( home: Scaffold(
appBar: AppBar( appBar: AppBar(
elevation: 0, elevation: 0,
backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) { backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor; return states.contains(MaterialState.scrolledUnder)
? scrolledColor
: defaultColor;
}), }),
title: const Text('AppBar'), title: const Text('AppBar'),
flexibleSpace: includeFlexibleSpace
? const FlexibleSpaceBar(title: Text('FlexibleSpace'))
: null,
), ),
body: ListView( body: ListView(
reverse: reverse,
children: <Widget>[ children: <Widget>[
Container(height: 1200.0, color: Colors.teal), Container(height: contentHeight, color: Colors.teal),
], ],
), ),
), ),
),
); );
Finder findAppBarMaterial() {
return find.descendant(of: find.byType(AppBar), matching: find.byType(Material));
} }
Color? getAppBarBackgroundColor() { testWidgets('backgroundColor', (WidgetTester tester) async {
return tester.widget<Material>(findAppBarMaterial()).color; await tester.pumpWidget(
} _buildAppBar(contentHeight: 1200.0)
);
expect(getAppBarBackgroundColor(), defaultColor); expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight); expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0)); TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
...@@ -2785,7 +2862,7 @@ void main() { ...@@ -2785,7 +2862,7 @@ void main() {
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), scrolledColor); expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight); expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
gesture = await tester.startGesture(const Offset(50.0, 300.0)); gesture = await tester.startGesture(const Offset(50.0, 300.0));
...@@ -2793,67 +2870,96 @@ void main() { ...@@ -2793,67 +2870,96 @@ void main() {
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), defaultColor); expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight); expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
}); });
testWidgets('AppBar.backgroundColor with FlexibleSpace MaterialStateColor scrolledUnder', (WidgetTester tester) async { testWidgets('backgroundColor with FlexibleSpace', (WidgetTester tester) async {
const Color scrolledColor = Color(0xff00ff00);
const Color defaultColor = Color(0xff0000ff);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( _buildAppBar(contentHeight: 1200.0, includeFlexibleSpace: true)
home: Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor;
}),
title: const Text('AppBar'),
flexibleSpace: const FlexibleSpaceBar(
title: Text('FlexibleSpace'),
),
),
body: ListView(
children: <Widget>[
Container(height: 1200.0, color: Colors.teal),
],
),
),
),
); );
Finder findAppBarMaterial() { expect(getAppBarBackgroundColor(tester), defaultColor);
// There are 2 Material widgets below AppBar. The second is only added if expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
// flexibleSpace is non-null.
return find.descendant(of: find.byType(AppBar), matching: find.byType(Material)).first;
}
Color? getAppBarBackgroundColor() { TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
return tester.widget<Material>(findAppBarMaterial()).color; await gesture.moveBy(const Offset(0.0, -kToolbarHeight));
} await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
gesture = await tester.startGesture(const Offset(50.0, 300.0));
await gesture.moveBy(const Offset(0.0, kToolbarHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
});
expect(getAppBarBackgroundColor(), defaultColor); testWidgets('backgroundColor - reverse', (WidgetTester tester) async {
await tester.pumpWidget(
_buildAppBar(contentHeight: 1200.0, reverse: true)
);
await tester.pump();
// In this test case, the content always extends under the AppBar, so it
// should always be the scrolledColor.
expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight); expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0)); TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(0.0, -kToolbarHeight)); await gesture.moveBy(const Offset(0.0, kToolbarHeight));
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), scrolledColor); expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight); expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
gesture = await tester.startGesture(const Offset(50.0, 300.0)); gesture = await tester.startGesture(const Offset(50.0, 300.0));
await gesture.moveBy(const Offset(0.0, -kToolbarHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
});
testWidgets('backgroundColor with FlexibleSpace - reverse', (WidgetTester tester) async {
await tester.pumpWidget(
_buildAppBar(
contentHeight: 1200.0,
reverse: true,
includeFlexibleSpace: true,
)
);
await tester.pump();
// In this test case, the content always extends under the AppBar, so it
// should always be the scrolledColor.
expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(0.0, kToolbarHeight)); await gesture.moveBy(const Offset(0.0, kToolbarHeight));
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), defaultColor); expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
gesture = await tester.startGesture(const Offset(50.0, 300.0));
await gesture.moveBy(const Offset(0.0, -kToolbarHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), scrolledColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight); expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
}); });
testWidgets('AppBar._handleScrollNotification safely calls setState()', (WidgetTester tester) async { testWidgets('_handleScrollNotification safely calls setState()', (WidgetTester tester) async {
// Regression test for failures found in Google internal issue b/185192049. // Regression test for failures found in Google internal issue b/185192049.
final ScrollController controller = ScrollController(initialScrollOffset: 400); final ScrollController controller = ScrollController(initialScrollOffset: 400);
await tester.pumpWidget( await tester.pumpWidget(
...@@ -2879,17 +2985,16 @@ void main() { ...@@ -2879,17 +2985,16 @@ void main() {
expect(tester.takeException(), isNull); expect(tester.takeException(), isNull);
}); });
testWidgets('AppBar scrolledUnder does not trigger on horizontal scroll', (WidgetTester tester) async { testWidgets('does not trigger on horizontal scroll', (WidgetTester tester) async {
const Color scrolledColor = Color(0xff00ff00);
const Color defaultColor = Color(0xff0000ff);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
home: Scaffold( home: Scaffold(
appBar: AppBar( appBar: AppBar(
elevation: 0, elevation: 0,
backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) { backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor; return states.contains(MaterialState.scrolledUnder)
? scrolledColor
: defaultColor;
}), }),
title: const Text('AppBar'), title: const Text('AppBar'),
), ),
...@@ -2903,29 +3008,70 @@ void main() { ...@@ -2903,29 +3008,70 @@ void main() {
), ),
); );
Finder findAppBarMaterial() { expect(getAppBarBackgroundColor(tester), defaultColor);
return find.descendant(of: find.byType(AppBar), matching: find.byType(Material));
}
Color? getAppBarBackgroundColor() {
return tester.widget<Material>(findAppBarMaterial()).color;
}
expect(getAppBarBackgroundColor(), defaultColor);
TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0)); TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(-100.0, 0.0)); await gesture.moveBy(const Offset(-100.0, 0.0));
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), defaultColor); expect(getAppBarBackgroundColor(tester), defaultColor);
gesture = await tester.startGesture(const Offset(50.0, 400.0)); gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(100.0, 0.0)); await gesture.moveBy(const Offset(100.0, 0.0));
await gesture.up(); await gesture.up();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(), defaultColor); expect(getAppBarBackgroundColor(tester), defaultColor);
});
testWidgets('backgroundColor - not triggered in reverse for short content', (WidgetTester tester) async {
await tester.pumpWidget(
_buildAppBar(
contentHeight: 200.0,
reverse: true,
)
);
await tester.pump();
// In reverse, the content here is not long enough to scroll under the app
// bar.
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
final TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(0.0, kToolbarHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
});
testWidgets('backgroundColor with FlexibleSpace - not triggered in reverse for short content', (WidgetTester tester) async {
await tester.pumpWidget(
_buildAppBar(
contentHeight: 200.0,
reverse: true,
includeFlexibleSpace: true,
)
);
await tester.pump();
// In reverse, the content here is not long enough to scroll under the app
// bar.
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
final TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
await gesture.moveBy(const Offset(0.0, kToolbarHeight));
await gesture.up();
await tester.pumpAndSettle();
expect(getAppBarBackgroundColor(tester), defaultColor);
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
});
});
}); });
testWidgets('AppBar.preferredHeightFor', (WidgetTester tester) async { testWidgets('AppBar.preferredHeightFor', (WidgetTester tester) async {
......
...@@ -349,6 +349,7 @@ void main() { ...@@ -349,6 +349,7 @@ void main() {
' Material\n' ' Material\n'
' _ScrollNotificationObserverScope\n' ' _ScrollNotificationObserverScope\n'
' NotificationListener<ScrollNotification>\n' ' NotificationListener<ScrollNotification>\n'
' NotificationListener<ScrollMetricsNotification>\n'
' ScrollNotificationObserver\n' ' ScrollNotificationObserver\n'
' _ScaffoldScope\n' ' _ScaffoldScope\n'
' Scaffold-[LabeledGlobalKey<ScaffoldState>#00000]\n' ' Scaffold-[LabeledGlobalKey<ScaffoldState>#00000]\n'
......
...@@ -2339,6 +2339,7 @@ void main() { ...@@ -2339,6 +2339,7 @@ void main() {
' Material\n' ' Material\n'
' _ScrollNotificationObserverScope\n' ' _ScrollNotificationObserverScope\n'
' NotificationListener<ScrollNotification>\n' ' NotificationListener<ScrollNotification>\n'
' NotificationListener<ScrollMetricsNotification>\n'
' ScrollNotificationObserver\n' ' ScrollNotificationObserver\n'
' _ScaffoldScope\n' ' _ScaffoldScope\n'
' Scaffold\n' ' Scaffold\n'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment