AppBar automatically show a close button for dialogs (#9268)
* Wiring fullscreen dialog * Make fullscreen dialog use the new API. Add tests * Review notes * Move field back up * final
This commit is contained in:
@@ -17,6 +17,7 @@ import 'icon_theme.dart';
|
||||
import 'icon_theme_data.dart';
|
||||
import 'icons.dart';
|
||||
import 'material.dart';
|
||||
import 'page.dart';
|
||||
import 'scaffold.dart';
|
||||
import 'tabs.dart';
|
||||
import 'theme.dart';
|
||||
@@ -332,13 +333,16 @@ class AppBar extends StatefulWidget {
|
||||
class _AppBarState extends State<AppBar> {
|
||||
bool _hasDrawer = false;
|
||||
bool _canPop = false;
|
||||
bool _useCloseButton = false;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
final ScaffoldState scaffold = Scaffold.of(context, nullOk: true);
|
||||
_hasDrawer = scaffold?.hasDrawer ?? false;
|
||||
_canPop = ModalRoute.of(context)?.canPop ?? false;
|
||||
final ModalRoute<dynamic> parentRoute = ModalRoute.of(context);
|
||||
_canPop = parentRoute?.canPop ?? false;
|
||||
_useCloseButton = parentRoute is MaterialPageRoute<dynamic> && parentRoute.fullscreenDialog;
|
||||
}
|
||||
|
||||
void _handleDrawerButton() {
|
||||
@@ -380,7 +384,7 @@ class _AppBarState extends State<AppBar> {
|
||||
);
|
||||
} else {
|
||||
if (_canPop)
|
||||
leading = const BackButton();
|
||||
leading = _useCloseButton ? const CloseButton() : const BackButton();
|
||||
}
|
||||
}
|
||||
if (leading != null) {
|
||||
|
||||
@@ -29,6 +29,8 @@ import 'theme.dart';
|
||||
/// [AppBar.leading] slot when appropriate.
|
||||
/// * [IconButton], which is a more general widget for creating buttons with
|
||||
/// icons.
|
||||
/// * [CloseButton], an alternative which may be more appropriate for leaf
|
||||
/// node pages in the navigation tree.
|
||||
class BackButton extends StatelessWidget {
|
||||
/// Creates an [IconButton] with the appropriate "back" icon for the current
|
||||
/// target platform.
|
||||
@@ -58,3 +60,33 @@ class BackButton extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A material design close button.
|
||||
///
|
||||
/// A [CloseButton] is an [IconButton] with a "close" icon. When pressed, the
|
||||
/// close button calls [Navigator.maybePop] to return to the previous route.
|
||||
///
|
||||
/// Use a [CloseButton] instead of a [BackButton] on fullscreen dialogs or
|
||||
/// pages that may solicit additional actions to close.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [AppBar], which automatically uses a [CloseButton] in its
|
||||
/// [AppBar.leading] slot when appropriate.
|
||||
/// * [BackButton], which is more appropriate for middle nodes in the
|
||||
/// navigation tree or where pages can be popped instantaneously with
|
||||
/// no user data consequence.
|
||||
class CloseButton extends StatelessWidget {
|
||||
const CloseButton({ Key key }) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
tooltip: 'Close',
|
||||
onPressed: () {
|
||||
Navigator.of(context).maybePop();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,6 +329,42 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('close button', () {
|
||||
Future<Null> expectCloseIcon(WidgetTester tester, TargetPlatform platform, IconData expectedIcon) async {
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
theme: new ThemeData(platform: platform),
|
||||
home: new Scaffold(appBar: new AppBar(), body: new Text('Page 1')),
|
||||
)
|
||||
);
|
||||
|
||||
tester.state<NavigatorState>(find.byType(Navigator)).push(new MaterialPageRoute<Null>(
|
||||
builder: (BuildContext context) {
|
||||
return new Scaffold(appBar: new AppBar(), body: new Text('Page 2'));
|
||||
},
|
||||
fullscreenDialog: true,
|
||||
));
|
||||
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
|
||||
final Icon icon = tester.widget(find.byType(Icon));
|
||||
expect(icon.icon, expectedIcon);
|
||||
}
|
||||
|
||||
testWidgets('Close button shows correctly on Android', (WidgetTester tester) async {
|
||||
await expectCloseIcon(tester, TargetPlatform.android, Icons.close);
|
||||
});
|
||||
|
||||
testWidgets('Close button shows correctly on Fuchsia', (WidgetTester tester) async {
|
||||
await expectCloseIcon(tester, TargetPlatform.fuchsia, Icons.close);
|
||||
});
|
||||
|
||||
testWidgets('Close button shows correctly on iOS', (WidgetTester tester) async {
|
||||
await expectCloseIcon(tester, TargetPlatform.iOS, Icons.close);
|
||||
});
|
||||
});
|
||||
|
||||
group('body size', () {
|
||||
testWidgets('body size with container', (WidgetTester tester) async {
|
||||
final Key testKey = new UniqueKey();
|
||||
|
||||
Reference in New Issue
Block a user