Created
January 16, 2023 12:58
-
-
Save vipulshah2010/6bb9ec23e79fbc4535de5576387738e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
static const String _title = 'Flutter Code Sample'; | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
restorationScopeId: 'app', | |
title: _title, | |
home: MyStatefulWidget(restorationId: 'main'), | |
); | |
} | |
} | |
class MyStatefulWidget extends StatefulWidget { | |
const MyStatefulWidget({super.key, this.restorationId}); | |
final String? restorationId; | |
@override | |
State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); | |
} | |
/// RestorationProperty objects can be used because of RestorationMixin. | |
class _MyStatefulWidgetState extends State<MyStatefulWidget> | |
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; | |
final RestorableDateTime _selectedDate = | |
RestorableDateTime(DateTime(2021, 7, 25)); | |
late final RestorableRouteFuture<DateTime?> _restorableDatePickerRouteFuture = | |
RestorableRouteFuture<DateTime?>( | |
onComplete: _selectDate, | |
onPresent: (NavigatorState navigator, Object? arguments) { | |
return navigator.restorablePush( | |
_datePickerRoute, | |
arguments: _selectedDate.value.millisecondsSinceEpoch, | |
); | |
}, | |
); | |
static Route<DateTime> _datePickerRoute( | |
BuildContext context, | |
Object? arguments, | |
) { | |
return DialogRoute<DateTime>( | |
context: context, | |
builder: (BuildContext context) { | |
return DatePickerDialog( | |
restorationId: 'date_picker_dialog', | |
initialEntryMode: DatePickerEntryMode.calendarOnly, | |
initialDate: DateTime.fromMillisecondsSinceEpoch(arguments! as int), | |
firstDate: DateTime(2021), | |
lastDate: DateTime(2022), | |
); | |
}, | |
); | |
} | |
@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'), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment