From 49adef67920a5c0c949be68473b3f26652423621 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Tue, 23 Mar 2021 09:24:34 +0800 Subject: [PATCH] [State Restoration] Code sample templates, migrate existing code samples (#78757) * Create state restoration templates, * Update existing state restoration samples to use the new templates * Add to README, decided to require stateful widgets to include RestorationMixin --- dev/snippets/config/templates/README.md | 30 +- ...stateful_widget_restoration_cupertino.tmpl | 49 +++ .../stateful_widget_restoration_material.tmpl | 49 +++ ...tateless_widget_restoration_cupertino.tmpl | 36 +++ ...stateless_widget_restoration_material.tmpl | 36 +++ packages/flutter/lib/src/cupertino/route.dart | 176 ++++------- .../flutter/lib/src/material/date_picker.dart | 284 +++++++----------- packages/flutter/lib/src/material/dialog.dart | 62 ++-- packages/flutter/lib/src/widgets/routes.dart | 71 ++--- 9 files changed, 405 insertions(+), 388 deletions(-) create mode 100644 dev/snippets/config/templates/stateful_widget_restoration_cupertino.tmpl create mode 100644 dev/snippets/config/templates/stateful_widget_restoration_material.tmpl create mode 100644 dev/snippets/config/templates/stateless_widget_restoration_cupertino.tmpl create mode 100644 dev/snippets/config/templates/stateless_widget_restoration_material.tmpl diff --git a/dev/snippets/config/templates/README.md b/dev/snippets/config/templates/README.md index 6f4495570b..c7e1dea3c1 100644 --- a/dev/snippets/config/templates/README.md +++ b/dev/snippets/config/templates/README.md @@ -65,7 +65,7 @@ follows: This is a simple template for which you provide everything. It has no code of its own, just the sections for `imports`, `main`, and `preamble`. You must provide the `main` section to have a `main()`. - + ### WidgetsApp Templates These templates create a `WidgetsApp` that encloses the snippet widget. These templates import @@ -82,7 +82,7 @@ the widgets library. `build()` method, and any state variables. It also has an `imports` section to import additional packages. Please only import things that are part of Flutter or part of default dependencies for a `flutter create` project. - + - [`stateful_widget_ticker`](stateful_widget_ticker.tmpl) : Identical to the `stateful_widget` template, with the addition of the `TickerProviderStateMixin` class, enabling easy generation of animated samples. @@ -104,11 +104,18 @@ These templates follow the same conventions as the `WidgetsApp` templates above `MaterialApp` instead. These templates import the material library. - [`stateful_widget_material`](stateful_widget_material.tmpl) - + - [`stateful_widget_material_ticker`](stateful_widget_material_ticker.tmpl) - [`stateless_widget_material`](stateless_widget_material.tmpl) +- [`stateful_widget_restoration_material`](stateful_widget_restoration_material.tmpl) : + Similar to the `stateful_widget_restoration` template, but for `MaterialApp`. + +- [`stateless_widget_restoration_material`](stateful_widget_restoration_material.tmpl) : + Similar to the `stateless_widget` template, but the `MaterialApp` has `restorationScopeId` + defined. + - [`stateful_widget_scaffold`](stateful_widget_scaffold.tmpl) : Adds a `Scaffold` widget as the home of the enclosing `MaterialApp` to wrap the stateful widget snippet. The `Scaffold` widget contains an `AppBar`. @@ -132,17 +139,24 @@ These templates follow the same conventions as the `WidgetsApp` templates above These templates follow the same conventions as the `WidgetsApp` templates above but use a `CupertinoApp` instead. These templates import the Cupertino library. - + - [`stateful_widget_cupertino`](stateful_widget_cupertino.tmpl) - + - [`stateful_widget_cupertino_ticker`](stateful_widget_cupertino_ticker.tmpl) - + - [`stateless_widget_cupertino`](stateless_widget_cupertino.tmpl) - + - [`stateful_widget_cupertinoPageScaffold`](stateful_widget_cupertino_page_scaffold.tmpl) : Similar to `stateful_widget_cupertino`, except that it wraps the stateful widget with a `CupertinoPageScaffold`. - + - [`stateless_widget_cupertinoPageScaffold`](stateless_widget_cupertino_page_scaffold.tmpl) : Similar to `stateless_widget_cupertino`, except that it wraps the stateless widget with a `CupertinoPageScaffold`. + +- [`stateless_widget_restoration_cupertino`](stateful_widget_restoration_material.tmpl) : + Similar to the `stateless_widget` template, but the `CupertinoApp` has `restorationScopeId` + defined. + +- [`stateful_widget_restoration_cupertino`](stateful_widget_restoration_material.tmpl) : + Similar to the `stateful_widget_restoration` template, but for `CupertinoApp`. diff --git a/dev/snippets/config/templates/stateful_widget_restoration_cupertino.tmpl b/dev/snippets/config/templates/stateful_widget_restoration_cupertino.tmpl new file mode 100644 index 0000000000..e9316d837c --- /dev/null +++ b/dev/snippets/config/templates/stateful_widget_restoration_cupertino.tmpl @@ -0,0 +1,49 @@ +/// Flutter code sample for {{element}} + +{{description}} + +{{code-dartImports}} + +import 'package:flutter/cupertino.dart'; +{{code-imports}} + +void main() => runApp(const MyApp()); + +/// This is the main application widget. +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + static const String _title = 'Flutter Code Sample'; + + @override + Widget build(BuildContext context) { + return const CupertinoApp( + restorationScopeId: 'app', + title: _title, + home: MyStatefulWidget(restorationId: 'main'), + ); + } +} + +{{code-preamble}} + +/// This is the stateful widget that the main application instantiates. +class MyStatefulWidget extends StatefulWidget { + const MyStatefulWidget({Key? key, this.restorationId}) : super(key: key); + + final String? restorationId; + + @override + _MyStatefulWidgetState createState() => _MyStatefulWidgetState(); +} + +/// This is the private State class that goes with MyStatefulWidget. +/// RestorationProperty objects can be used because of RestorationMixin. +class _MyStatefulWidgetState extends State with RestorationMixin { + // In this example, the restoration ID for the mixin is passed in through + // the [StatefulWidget]'s constructor. + @override + String? get restorationId => widget.restorationId; + +{{code}} +} diff --git a/dev/snippets/config/templates/stateful_widget_restoration_material.tmpl b/dev/snippets/config/templates/stateful_widget_restoration_material.tmpl new file mode 100644 index 0000000000..841b7ad45f --- /dev/null +++ b/dev/snippets/config/templates/stateful_widget_restoration_material.tmpl @@ -0,0 +1,49 @@ +/// Flutter code sample for {{element}} + +{{description}} + +{{code-dartImports}} + +import 'package:flutter/material.dart'; +{{code-imports}} + +void main() => runApp(const MyApp()); + +/// This is the main application widget. +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + static const String _title = 'Flutter Code Sample'; + + @override + Widget build(BuildContext context) { + return const MaterialApp( + restorationScopeId: 'app', + title: _title, + home: MyStatefulWidget(restorationId: 'main'), + ); + } +} + +{{code-preamble}} + +/// This is the stateful widget that the main application instantiates. +class MyStatefulWidget extends StatefulWidget { + const MyStatefulWidget({Key? key, this.restorationId}) : super(key: key); + + final String? restorationId; + + @override + _MyStatefulWidgetState createState() => _MyStatefulWidgetState(); +} + +/// This is the private State class that goes with MyStatefulWidget. +/// RestorationProperty objects can be used because of RestorationMixin. +class _MyStatefulWidgetState extends State with RestorationMixin { + // In this example, the restoration ID for the mixin is passed in through + // the [StatefulWidget]'s constructor. + @override + String? get restorationId => widget.restorationId; + +{{code}} +} diff --git a/dev/snippets/config/templates/stateless_widget_restoration_cupertino.tmpl b/dev/snippets/config/templates/stateless_widget_restoration_cupertino.tmpl new file mode 100644 index 0000000000..5e2a6e13c1 --- /dev/null +++ b/dev/snippets/config/templates/stateless_widget_restoration_cupertino.tmpl @@ -0,0 +1,36 @@ +/// Flutter code sample for {{element}} + +{{description}} + +{{code-dartImports}} + +import 'package:flutter/cupertino.dart'; +{{code-imports}} + +void main() => runApp(const MyApp()); + +/// This is the main application widget. +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + static const String _title = 'Flutter Code Sample'; + + @override + Widget build(BuildContext context) { + return const CupertinoApp( + restorationScopeId: 'app', + title: _title, + home: MyStatelessWidget(), + ); + } +} + +{{code-preamble}} + +/// This is the stateless widget that the main application instantiates. +class MyStatelessWidget extends StatelessWidget { + const MyStatelessWidget({Key? key}) : super(key: key); + + @override + {{code}} +} diff --git a/dev/snippets/config/templates/stateless_widget_restoration_material.tmpl b/dev/snippets/config/templates/stateless_widget_restoration_material.tmpl new file mode 100644 index 0000000000..2d936ef347 --- /dev/null +++ b/dev/snippets/config/templates/stateless_widget_restoration_material.tmpl @@ -0,0 +1,36 @@ +/// Flutter code sample for {{element}} + +{{description}} + +{{code-dartImports}} + +import 'package:flutter/material.dart'; +{{code-imports}} + +void main() => runApp(const MyApp()); + +/// This is the main application widget. +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + static const String _title = 'Flutter Code Sample'; + + @override + Widget build(BuildContext context) { + return const MaterialApp( + restorationScopeId: 'app', + title: _title, + home: MyStatelessWidget(), + ); + } +} + +{{code-preamble}} + +/// This is the stateless widget that the main application instantiates. +class MyStatelessWidget extends StatelessWidget { + const MyStatelessWidget({Key? key}) : super(key: key); + + @override + {{code}} +} diff --git a/packages/flutter/lib/src/cupertino/route.dart b/packages/flutter/lib/src/cupertino/route.dart index 4ce6764766..751628209f 100644 --- a/packages/flutter/lib/src/cupertino/route.dart +++ b/packages/flutter/lib/src/cupertino/route.dart @@ -1127,7 +1127,7 @@ class CupertinoModalPopupRoute extends PopupRoute { /// /// For more information about state restoration, see [RestorationManager]. /// -/// {@tool sample --template=freeform} +/// {@tool sample --template=stateless_widget_restoration_cupertino} /// /// This sample demonstrates how to create a restorable Cupertino modal route. /// This is accomplished by enabling state restoration by specifying @@ -1136,69 +1136,44 @@ class CupertinoModalPopupRoute extends PopupRoute { /// /// {@macro flutter.widgets.RestorationManager} /// -/// ```dart imports -/// import 'package:flutter/cupertino.dart'; -/// ``` -/// /// ```dart -/// void main() { -/// runApp(const MyApp()); -/// } -/// -/// class MyApp extends StatelessWidget { -/// const MyApp({Key? key}) : super(key: key); -/// -/// @override -/// Widget build(BuildContext context) { -/// return const CupertinoApp( -/// restorationScopeId: 'app', -/// home: MyHomePage(), -/// ); -/// } -/// } -/// -/// class MyHomePage extends StatelessWidget { -/// const MyHomePage({Key? key}) : super(key: key); -/// -/// static Route _modalBuilder(BuildContext context, Object? arguments) { -/// return CupertinoModalPopupRoute( -/// builder: (BuildContext context) { -/// return CupertinoActionSheet( -/// title: const Text('Title'), -/// message: const Text('Message'), -/// actions: [ -/// CupertinoActionSheetAction( -/// child: const Text('Action One'), -/// onPressed: () { -/// Navigator.pop(context); -/// }, -/// ), -/// CupertinoActionSheetAction( -/// child: const Text('Action Two'), -/// onPressed: () { -/// Navigator.pop(context); -/// }, -/// ), -/// ], -/// ); +/// Widget build(BuildContext context) { +/// return CupertinoPageScaffold( +/// navigationBar: const CupertinoNavigationBar( +/// middle: Text('Home'), +/// ), +/// child: Center(child: CupertinoButton( +/// onPressed: () { +/// Navigator.of(context).restorablePush(_modalBuilder); /// }, -/// ); -/// } +/// child: const Text('Open Modal'), +/// )), +/// ); +/// } /// -/// @override -/// Widget build(BuildContext context) { -/// return CupertinoPageScaffold( -/// navigationBar: const CupertinoNavigationBar( -/// middle: Text('Home'), -/// ), -/// child: Center(child: CupertinoButton( -/// onPressed: () { -/// Navigator.of(context).restorablePush(_modalBuilder); -/// }, -/// child: const Text('Open Modal'), -/// )), -/// ); -/// } +/// static Route _modalBuilder(BuildContext context, Object? arguments) { +/// return CupertinoModalPopupRoute( +/// builder: (BuildContext context) { +/// return CupertinoActionSheet( +/// title: const Text('Title'), +/// message: const Text('Message'), +/// actions: [ +/// CupertinoActionSheetAction( +/// child: const Text('Action One'), +/// onPressed: () { +/// Navigator.pop(context); +/// }, +/// ), +/// CupertinoActionSheetAction( +/// child: const Text('Action Two'), +/// onPressed: () { +/// Navigator.pop(context); +/// }, +/// ), +/// ], +/// ); +/// }, +/// ); /// } /// ``` /// @@ -1293,7 +1268,7 @@ Widget _buildCupertinoDialogTransitions(BuildContext context, Animation /// /// For more information about state restoration, see [RestorationManager]. /// -/// {@tool sample --template=freeform} +/// {@tool sample --template=stateless_widget_restoration_cupertino} /// /// This sample demonstrates how to create a restorable Cupertino dialog. This is /// accomplished by enabling state restoration by specifying @@ -1302,60 +1277,35 @@ Widget _buildCupertinoDialogTransitions(BuildContext context, Animation /// /// {@macro flutter.widgets.RestorationManager} /// -/// ```dart imports -/// import 'package:flutter/cupertino.dart'; -/// ``` -/// /// ```dart -/// void main() { -/// runApp(const MyApp()); -/// } -/// -/// class MyApp extends StatelessWidget { -/// const MyApp({Key? key}) : super(key: key); -/// -/// @override -/// Widget build(BuildContext context) { -/// return const CupertinoApp( -/// restorationScopeId: 'app', -/// home: MyHomePage(), -/// ); -/// } -/// } -/// -/// class MyHomePage extends StatelessWidget { -/// const MyHomePage({Key? key}) : super(key: key); -/// -/// static Route _dialogBuilder(BuildContext context, Object? arguments) { -/// return CupertinoDialogRoute( -/// context: context, -/// builder: (BuildContext context) { -/// return const CupertinoAlertDialog( -/// title: Text('Title'), -/// content: Text('Content'), -/// actions: [ -/// CupertinoDialogAction(child: Text('Yes')), -/// CupertinoDialogAction(child: Text('No')), -/// ], -/// ); +/// Widget build(BuildContext context) { +/// return CupertinoPageScaffold( +/// navigationBar: const CupertinoNavigationBar( +/// middle: Text('Home'), +/// ), +/// child: Center(child: CupertinoButton( +/// onPressed: () { +/// Navigator.of(context).restorablePush(_dialogBuilder); /// }, -/// ); -/// } +/// child: const Text('Open Dialog'), +/// )), +/// ); +/// } /// -/// @override -/// Widget build(BuildContext context) { -/// return CupertinoPageScaffold( -/// navigationBar: const CupertinoNavigationBar( -/// middle: Text('Home'), -/// ), -/// child: Center(child: CupertinoButton( -/// onPressed: () { -/// Navigator.of(context).restorablePush(_dialogBuilder); -/// }, -/// child: const Text('Open Dialog'), -/// )), -/// ); -/// } +/// static Route _dialogBuilder(BuildContext context, Object? arguments) { +/// return CupertinoDialogRoute( +/// context: context, +/// builder: (BuildContext context) { +/// return const CupertinoAlertDialog( +/// title: Text('Title'), +/// content: Text('Content'), +/// actions: [ +/// CupertinoDialogAction(child: Text('Yes')), +/// CupertinoDialogAction(child: Text('No')), +/// ], +/// ); +/// }, +/// ); /// } /// ``` /// diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index aff5458298..1382bdb29c 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -111,108 +111,75 @@ const double _inputFormLandscapeHeight = 108.0; /// /// For more information about state restoration, see [RestorationManager]. /// -/// {@tool sample --template=freeform} +/// {@macro flutter.widgets.RestorationManager} +/// +/// {@tool sample --template=stateful_widget_restoration_material} /// /// This sample demonstrates how to create a restorable Material date picker. /// This is accomplished by enabling state restoration by specifying /// [MaterialApp.restorationScopeId] and using [Navigator.restorablePush] to /// push [DatePickerDialog] when the button is tapped. /// -/// {@macro flutter.widgets.RestorationManager} -/// -/// ```dart imports -/// import 'package:flutter/material.dart'; -/// ``` -/// /// ```dart -/// void main() { -/// runApp(const MyApp()); -/// } -/// -/// class MyApp extends StatelessWidget { -/// const MyApp({Key? key}) : super(key: key); -/// -/// @override -/// Widget build(BuildContext context) { -/// return const MaterialApp( -/// restorationScopeId: 'app', -/// title: 'Restorable Date Picker Demo', -/// home: MyHomePage(), +/// final RestorableDateTime _selectedDate = RestorableDateTime(DateTime(2021, 7, 25)); +/// late final RestorableRouteFuture _restorableDatePickerRouteFuture = RestorableRouteFuture( +/// onComplete: _selectDate, +/// onPresent: (NavigatorState navigator, Object? arguments) { +/// return navigator.restorablePush( +/// _datePickerRoute, +/// arguments: _selectedDate.value.millisecondsSinceEpoch, /// ); -/// } -/// } +/// }, +/// ); /// -/// class MyHomePage extends StatefulWidget { -/// const MyHomePage({Key? key}) : super(key: key); -/// -/// @override -/// _MyHomePageState createState() => _MyHomePageState(); -/// } -/// -/// class _MyHomePageState extends State with RestorationMixin { -/// @override -/// String get restorationId => 'scaffold_state'; -/// -/// final RestorableDateTime _selectedDate = RestorableDateTime(DateTime(2021, 7, 25)); -/// late final RestorableRouteFuture _restorableDatePickerRouteFuture = RestorableRouteFuture( -/// onComplete: _selectDate, -/// onPresent: (NavigatorState navigator, Object? arguments) { -/// return navigator.restorablePush( -/// _datePickerRoute, -/// arguments: _selectedDate.value.millisecondsSinceEpoch, +/// static Route _datePickerRoute( +/// BuildContext context, +/// Object? arguments, +/// ) { +/// return DialogRoute( +/// context: context, +/// builder: (BuildContext context) { +/// return DatePickerDialog( +/// restorationId: 'date_picker_dialog', +/// initialEntryMode: DatePickerEntryMode.calendarOnly, +/// initialDate: DateTime.fromMillisecondsSinceEpoch(arguments! as int), +/// firstDate: DateTime(2021, 1, 1), +/// lastDate: DateTime(2022, 1, 1), /// ); /// }, /// ); +/// } /// -/// static Route _datePickerRoute( -/// BuildContext context, -/// Object? arguments, -/// ) { -/// return DialogRoute( -/// context: context, -/// builder: (BuildContext context) { -/// return DatePickerDialog( -/// restorationId: 'date_picker_dialog', -/// initialEntryMode: DatePickerEntryMode.calendarOnly, -/// initialDate: DateTime.fromMillisecondsSinceEpoch(arguments! as int), -/// firstDate: DateTime(2021, 1, 1), -/// lastDate: DateTime(2022, 1, 1), -/// ); -/// }, -/// ); +/// @override +/// void restoreState(RestorationBucket? oldBucket, bool initialRestore) { +/// registerForRestoration(_selectedDate, 'selected_date'); +/// registerForRestoration(_restorableDatePickerRouteFuture, 'date_picker_route_future'); +/// } +/// +/// void _selectDate(DateTime? newSelectedDate) { +/// if (newSelectedDate != null) { +/// setState(() { +/// _selectedDate.value = newSelectedDate; +/// ScaffoldMessenger.of(context).showSnackBar(SnackBar( +/// content: Text( +/// 'Selected: ${_selectedDate.value.day}/${_selectedDate.value.month}/${_selectedDate.value.year}'), +/// )); +/// }); /// } +/// } /// -/// @override -/// void restoreState(RestorationBucket? oldBucket, bool initialRestore) { -/// registerForRestoration(_selectedDate, 'selected_date'); -/// registerForRestoration(_restorableDatePickerRouteFuture, 'date_picker_route_future'); -/// } -/// -/// void _selectDate(DateTime? newSelectedDate) { -/// if (newSelectedDate != null) { -/// setState(() { -/// _selectedDate.value = newSelectedDate; -/// ScaffoldMessenger.of(context).showSnackBar(SnackBar( -/// content: Text( -/// 'Selected: ${_selectedDate.value.day}/${_selectedDate.value.month}/${_selectedDate.value.year}'), -/// )); -/// }); -/// } -/// } -/// -/// @override -/// Widget build(BuildContext context) { -/// return Scaffold( -/// body: Center( -/// child: OutlinedButton( -/// onPressed: () { -/// _restorableDatePickerRouteFuture.present(); -/// }, -/// child: const Text('Open Date Picker'), -/// ), +/// @override +/// Widget build(BuildContext context) { +/// return Scaffold( +/// body: Center( +/// child: OutlinedButton( +/// onPressed: () { +/// _restorableDatePickerRouteFuture.present(); +/// }, +/// child: const Text('Open Date Picker'), /// ), -/// ); -/// } +/// ), +/// ); /// } /// ``` /// @@ -969,117 +936,84 @@ class _DatePickerHeader extends StatelessWidget { /// /// {@macro flutter.widgets.RestorationManager} /// -/// {@tool sample --template=freeform} +/// {@tool sample --template=stateful_widget_restoration_material} /// /// This sample demonstrates how to create a restorable Material date range picker. /// This is accomplished by enabling state restoration by specifying /// [MaterialApp.restorationScopeId] and using [Navigator.restorablePush] to /// push [DateRangePickerDialog] when the button is tapped. /// -/// ```dart imports -/// import 'package:flutter/material.dart'; -/// ``` -/// /// ```dart -/// void main() { -/// runApp(const MyApp()); -/// } -/// -/// class MyApp extends StatelessWidget { -/// const MyApp({Key? key}) : super(key: key); -/// -/// @override -/// Widget build(BuildContext context) { -/// return const MaterialApp( -/// restorationScopeId: 'app', -/// title: 'Restorable Date Range Picker Demo', -/// home: MyHomePage(), +/// final RestorableDateTimeN _startDate = RestorableDateTimeN(DateTime(2021, 1, 1)); +/// final RestorableDateTimeN _endDate = RestorableDateTimeN(DateTime(2021, 1, 5)); +/// late final RestorableRouteFuture _restorableDateRangePickerRouteFuture = RestorableRouteFuture( +/// onComplete: _selectDateRange, +/// onPresent: (NavigatorState navigator, Object? arguments) { +/// return navigator.restorablePush( +/// _dateRangePickerRoute, +/// arguments: { +/// 'initialStartDate': _startDate.value?.millisecondsSinceEpoch, +/// 'initialEndDate': _endDate.value?.millisecondsSinceEpoch, +/// } /// ); +/// }, +/// ); +/// +/// void _selectDateRange(DateTimeRange? newSelectedDate) { +/// if (newSelectedDate != null) { +/// setState(() { +/// _startDate.value = newSelectedDate.start; +/// _endDate.value = newSelectedDate.end; +/// }); /// } /// } /// -/// class MyHomePage extends StatefulWidget { -/// const MyHomePage({Key? key}) : super(key: key); -/// -/// @override -/// _MyHomePageState createState() => _MyHomePageState(); +/// @override +/// void restoreState(RestorationBucket? oldBucket, bool initialRestore) { +/// registerForRestoration(_startDate, 'start_date'); +/// registerForRestoration(_endDate, 'end_date'); +/// registerForRestoration(_restorableDateRangePickerRouteFuture, 'date_picker_route_future'); /// } /// -/// class _MyHomePageState extends State with RestorationMixin { -/// final RestorableDateTimeN _startDate = RestorableDateTimeN(DateTime(2021, 1, 1)); -/// final RestorableDateTimeN _endDate = RestorableDateTimeN(DateTime(2021, 1, 5)); -/// late final RestorableRouteFuture _restorableDateRangePickerRouteFuture = RestorableRouteFuture( -/// onComplete: _selectDateRange, -/// onPresent: (NavigatorState navigator, Object? arguments) { -/// return navigator.restorablePush( -/// _dateRangePickerRoute, -/// arguments: { -/// 'initialStartDate': _startDate.value?.millisecondsSinceEpoch, -/// 'initialEndDate': _endDate.value?.millisecondsSinceEpoch, -/// } +/// static Route _dateRangePickerRoute( +/// BuildContext context, +/// Object? arguments, +/// ) { +/// return DialogRoute( +/// context: context, +/// builder: (BuildContext context) { +/// return DateRangePickerDialog( +/// restorationId: 'date_picker_dialog', +/// initialDateRange: _initialDateTimeRange(arguments! as Map), +/// firstDate: DateTime(2021, 1, 1), +/// currentDate: DateTime(2021, 1, 25), +/// lastDate: DateTime(2022, 1, 1), /// ); /// }, /// ); +/// } /// -/// void _selectDateRange(DateTimeRange? newSelectedDate) { -/// if (newSelectedDate != null) { -/// setState(() { -/// _startDate.value = newSelectedDate.start; -/// _endDate.value = newSelectedDate.end; -/// }); -/// } -/// } -/// -/// @override -/// String get restorationId => 'scaffold_state'; -/// -/// @override -/// void restoreState(RestorationBucket? oldBucket, bool initialRestore) { -/// registerForRestoration(_startDate, 'start_date'); -/// registerForRestoration(_endDate, 'end_date'); -/// registerForRestoration(_restorableDateRangePickerRouteFuture, 'date_picker_route_future'); -/// } -/// -/// static Route _dateRangePickerRoute( -/// BuildContext context, -/// Object? arguments, -/// ) { -/// return DialogRoute( -/// context: context, -/// builder: (BuildContext context) { -/// return DateRangePickerDialog( -/// restorationId: 'date_picker_dialog', -/// initialDateRange: _initialDateTimeRange(arguments! as Map), -/// firstDate: DateTime(2021, 1, 1), -/// currentDate: DateTime(2021, 1, 25), -/// lastDate: DateTime(2022, 1, 1), -/// ); -/// }, +/// static DateTimeRange? _initialDateTimeRange(Map arguments) { +/// if (arguments['initialStartDate'] != null && arguments['initialEndDate'] != null) { +/// return DateTimeRange( +/// start: DateTime.fromMillisecondsSinceEpoch(arguments['initialStartDate'] as int), +/// end: DateTime.fromMillisecondsSinceEpoch(arguments['initialEndDate'] as int), /// ); /// } /// -/// static DateTimeRange? _initialDateTimeRange(Map arguments) { -/// if (arguments['initialStartDate'] != null && arguments['initialEndDate'] != null) { -/// return DateTimeRange( -/// start: DateTime.fromMillisecondsSinceEpoch(arguments['initialStartDate'] as int), -/// end: DateTime.fromMillisecondsSinceEpoch(arguments['initialEndDate'] as int), -/// ); -/// } +/// return null; +/// } /// -/// return null; -/// } -/// -/// @override -/// Widget build(BuildContext context) { -/// return Scaffold( -/// body: Center( -/// child: OutlinedButton( -/// onPressed: () { _restorableDateRangePickerRouteFuture.present(); }, -/// child: const Text('Open Date Range Picker'), -/// ), +/// @override +/// Widget build(BuildContext context) { +/// return Scaffold( +/// body: Center( +/// child: OutlinedButton( +/// onPressed: () { _restorableDateRangePickerRouteFuture.present(); }, +/// child: const Text('Open Date Range Picker'), /// ), -/// ); -/// } +/// ), +/// ); /// } /// ``` /// diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 9aeaa23206..20616a8ba4 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -994,7 +994,7 @@ Widget _buildMaterialDialogTransitions(BuildContext context, Animation a /// /// For more information about state restoration, see [RestorationManager]. /// -/// {@tool sample --template=freeform} +/// {@tool sample --template=stateless_widget_restoration_material} /// /// This sample demonstrates how to create a restorable Material dialog. This is /// accomplished by enabling state restoration by specifying @@ -1003,51 +1003,25 @@ Widget _buildMaterialDialogTransitions(BuildContext context, Animation a /// /// {@macro flutter.widgets.RestorationManager} /// -/// ```dart imports -/// import 'package:flutter/material.dart'; -/// ``` -/// /// ```dart -/// void main() { -/// runApp(const MyApp()); -/// } -/// -/// class MyApp extends StatelessWidget { -/// const MyApp({Key? key}) : super(key: key); -/// -/// @override -/// Widget build(BuildContext context) { -/// return const MaterialApp( -/// restorationScopeId: 'app', -/// title: 'Restorable Routes Demo', -/// home: MyHomePage(), -/// ); -/// } -/// } -/// -/// class MyHomePage extends StatelessWidget { -/// const MyHomePage({Key? key}) : super(key: key); -/// -/// static Route _dialogBuilder(BuildContext context, Object? arguments) { -/// return DialogRoute( -/// context: context, -/// builder: (BuildContext context) => const AlertDialog(title: Text('Material Alert!')), -/// ); -/// } -/// -/// @override -/// Widget build(BuildContext context) { -/// return Scaffold( -/// body: Center( -/// child: OutlinedButton( -/// onPressed: () { -/// Navigator.of(context).restorablePush(_dialogBuilder); -/// }, -/// child: const Text('Open Dialog'), -/// ), +/// Widget build(BuildContext context) { +/// return Scaffold( +/// body: Center( +/// child: OutlinedButton( +/// onPressed: () { +/// Navigator.of(context).restorablePush(_dialogBuilder); +/// }, +/// child: const Text('Open Dialog'), /// ), -/// ); -/// } +/// ), +/// ); +/// } +/// +/// static Route _dialogBuilder(BuildContext context, Object? arguments) { +/// return DialogRoute( +/// context: context, +/// builder: (BuildContext context) => const AlertDialog(title: Text('Material Alert!')), +/// ); /// } /// ``` /// diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 4d60fd8528..eb5040cdca 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -1901,7 +1901,7 @@ class RawDialogRoute extends PopupRoute { /// /// For more information about state restoration, see [RestorationManager]. /// -/// {@tool sample --template=freeform} +/// {@tool sample --template=stateless_widget_restoration_material} /// /// This sample demonstrates how to create a restorable dialog. This is /// accomplished by enabling state restoration by specifying @@ -1910,55 +1910,30 @@ class RawDialogRoute extends PopupRoute { /// /// {@macro flutter.widgets.RestorationManager} /// -/// ```dart imports -/// import 'package:flutter/material.dart'; -/// ``` -/// /// ```dart -/// void main() { -/// runApp(const MyApp()); -/// } -/// -/// class MyApp extends StatelessWidget { -/// const MyApp({Key? key}) : super(key: key); -/// -/// @override -/// Widget build(BuildContext context) { -/// return const MaterialApp( -/// restorationScopeId: 'app', -/// home: MyHomePage(), -/// ); -/// } -/// } -/// -/// class MyHomePage extends StatelessWidget { -/// const MyHomePage({Key? key}) : super(key: key); -/// -/// static Route _dialogBuilder(BuildContext context, Object? arguments) { -/// return RawDialogRoute( -/// pageBuilder: ( -/// BuildContext context, -/// Animation animation, -/// Animation secondaryAnimation, -/// ) { -/// return const AlertDialog(title: Text('Alert!')); -/// }, -/// ); -/// } -/// -/// @override -/// Widget build(BuildContext context) { -/// return Scaffold( -/// body: Center( -/// child: OutlinedButton( -/// onPressed: () { -/// Navigator.of(context).restorablePush(_dialogBuilder); -/// }, -/// child: const Text('Open Dialog'), -/// ), +/// Widget build(BuildContext context) { +/// return Scaffold( +/// body: Center( +/// child: OutlinedButton( +/// onPressed: () { +/// Navigator.of(context).restorablePush(_dialogBuilder); +/// }, +/// child: const Text('Open Dialog'), /// ), -/// ); -/// } +/// ), +/// ); +/// } +/// +/// static Route _dialogBuilder(BuildContext context, Object? arguments) { +/// return RawDialogRoute( +/// pageBuilder: ( +/// BuildContext context, +/// Animation animation, +/// Animation secondaryAnimation, +/// ) { +/// return const AlertDialog(title: Text('Alert!')); +/// }, +/// ); /// } /// ``` ///