Simplify drawer scrimColor defaults, update tests (#31947)

This commit is contained in:
Hans Muller 2019-05-02 11:57:01 -07:00 committed by GitHub
parent 34325ba33a
commit 4230e9674c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 27 deletions

View File

@ -181,7 +181,7 @@ class DrawerController extends StatefulWidget {
@required this.alignment, @required this.alignment,
this.drawerCallback, this.drawerCallback,
this.dragStartBehavior = DragStartBehavior.start, this.dragStartBehavior = DragStartBehavior.start,
this.scrimColor = Colors.black54, this.scrimColor,
}) : assert(child != null), }) : assert(child != null),
assert(dragStartBehavior != null), assert(dragStartBehavior != null),
assert(alignment != null), assert(alignment != null),
@ -223,7 +223,7 @@ class DrawerController extends StatefulWidget {
/// The color to use for the scrim that obscures primary content while a drawer is open. /// The color to use for the scrim that obscures primary content while a drawer is open.
/// ///
/// By default, the color is [Colors.black54] /// By default, the color used is [Colors.black54]
final Color scrimColor; final Color scrimColor;
@override @override
@ -237,7 +237,7 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_color = ColorTween(begin: Colors.transparent, end: widget.scrimColor); _scrimColorTween = _buildScrimColorTween();
_controller = AnimationController(duration: _kBaseSettleDuration, vsync: this) _controller = AnimationController(duration: _kBaseSettleDuration, vsync: this)
..addListener(_animationChanged) ..addListener(_animationChanged)
..addStatusListener(_animationStatusChanged); ..addStatusListener(_animationStatusChanged);
@ -250,6 +250,13 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
super.dispose(); super.dispose();
} }
@override
void didUpdateWidget(DrawerController oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.scrimColor != oldWidget.scrimColor)
_scrimColorTween = _buildScrimColorTween();
}
void _animationChanged() { void _animationChanged() {
setState(() { setState(() {
// The animation controller's state is our build state, and it changed already. // The animation controller's state is our build state, and it changed already.
@ -386,9 +393,13 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
widget.drawerCallback(false); widget.drawerCallback(false);
} }
ColorTween _color; ColorTween _scrimColorTween;
final GlobalKey _gestureDetectorKey = GlobalKey(); final GlobalKey _gestureDetectorKey = GlobalKey();
ColorTween _buildScrimColorTween() {
return ColorTween(begin: Colors.transparent, end: widget.scrimColor ?? Colors.black54);
}
AlignmentDirectional get _drawerOuterAlignment { AlignmentDirectional get _drawerOuterAlignment {
assert(widget.alignment != null); assert(widget.alignment != null);
switch (widget.alignment) { switch (widget.alignment) {
@ -452,8 +463,8 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
onTap: close, onTap: close,
child: Semantics( child: Semantics(
label: MaterialLocalizations.of(context)?.modalBarrierDismissLabel, label: MaterialLocalizations.of(context)?.modalBarrierDismissLabel,
child: Container( child: Container( // The drawer's "scrim"
color: _color.evaluate(_controller), color: _scrimColorTween.evaluate(_controller),
), ),
), ),
), ),

View File

@ -904,7 +904,7 @@ class Scaffold extends StatefulWidget {
this.primary = true, this.primary = true,
this.drawerDragStartBehavior = DragStartBehavior.start, this.drawerDragStartBehavior = DragStartBehavior.start,
this.extendBody = false, this.extendBody = false,
this.drawerScrimColor = Colors.black54, this.drawerScrimColor,
}) : assert(primary != null), }) : assert(primary != null),
assert(extendBody != null), assert(extendBody != null),
assert(drawerDragStartBehavior != null), assert(drawerDragStartBehavior != null),

View File

@ -107,31 +107,71 @@ void main() {
semantics.dispose(); semantics.dispose();
}); });
testWidgets('Drawer scrimDrawerColor test', (WidgetTester tester) async { testWidgets('Scaffold drawerScrimColor', (WidgetTester tester) async {
// The scrim is a Container within a Semantics node labeled "Dismiss",
await tester.pumpWidget( // within a DrawerController. Sorry.
const MaterialApp( Container getScrim() {
home: Scaffold( return tester.widget<Container>(
drawerScrimColor: Color(0xFF323232), find.descendant(
drawer: Drawer(), of: find.descendant(
of: find.byType(DrawerController),
matching: find.byWidgetPredicate((Widget widget) {
if (widget is! Semantics)
return false;
final Semantics semantics = widget;
return semantics.properties.label == 'Dismiss';
}),
),
matching: find.byType(Container),
), ),
), );
); }
final ScaffoldState state = tester.firstState(find.byType(Scaffold)); final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
state.openDrawer(); Widget buildFrame({ Color drawerScrimColor }) {
return MaterialApp(
home: Scaffold(
key: scaffoldKey,
drawerScrimColor: drawerScrimColor,
drawer: Drawer(
child: Builder(
builder: (BuildContext context) {
return GestureDetector(
onTap: () { Navigator.pop(context); }, // close drawer
);
},
),
),
),
);
}
await tester.pump(); // Default drawerScrimColor
await tester.pump(const Duration(seconds: 1));
final Container container = tester.widget<Container>(find.descendant( await tester.pumpWidget(buildFrame(drawerScrimColor: null));
of: find.byType(Scaffold), scaffoldKey.currentState.openDrawer();
matching: find.byType(Container), await tester.pumpAndSettle();
).first,
);
final BoxDecoration decoration = container.decoration; BoxDecoration decoration = getScrim().decoration;
expect(decoration.color, Colors.black54);
expect(decoration.shape, BoxShape.rectangle);
await tester.tap(find.byType(Drawer));
await tester.pumpAndSettle();
expect(find.byType(Drawer), findsNothing);
// Specific drawerScrimColor
await tester.pumpWidget(buildFrame(drawerScrimColor: const Color(0xFF323232)));
scaffoldKey.currentState.openDrawer();
await tester.pumpAndSettle();
decoration = getScrim().decoration;
expect(decoration.color, const Color(0xFF323232)); expect(decoration.color, const Color(0xFF323232));
expect(decoration.shape, BoxShape.rectangle); expect(decoration.shape, BoxShape.rectangle);
await tester.tap(find.byType(Drawer));
await tester.pumpAndSettle();
expect(find.byType(Drawer), findsNothing);
}); });
} }