1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6
7/// Flutter code sample for [showDatePicker].
8
9void main() => runApp(const DatePickerApp());
10
11class DatePickerApp extends StatelessWidget {
12 const DatePickerApp({super.key});
13
14 @override
15 Widget build(BuildContext context) {
16 return const MaterialApp(
17 restorationScopeId: 'app',
18 home: DatePickerExample(restorationId: 'main'),
19 );
20 }
21}
22
23class DatePickerExample extends StatefulWidget {
24 const DatePickerExample({super.key, this.restorationId});
25
26 final String? restorationId;
27
28 @override
29 State<DatePickerExample> createState() => _DatePickerExampleState();
30}
31
32/// RestorationProperty objects can be used because of RestorationMixin.
33class _DatePickerExampleState extends State<DatePickerExample> with RestorationMixin {
34 // In this example, the restoration ID for the mixin is passed in through
35 // the [StatefulWidget]'s constructor.
36 @override
37 String? get restorationId => widget.restorationId;
38
39 final RestorableDateTime _selectedDate = RestorableDateTime(DateTime(2021, 7, 25));
40 late final RestorableRouteFuture<DateTime?> _restorableDatePickerRouteFuture =
41 RestorableRouteFuture<DateTime?>(
42 onComplete: _selectDate,
43 onPresent: (NavigatorState navigator, Object? arguments) {
44 return navigator.restorablePush(
45 _datePickerRoute,
46 arguments: _selectedDate.value.millisecondsSinceEpoch,
47 );
48 },
49 );
50
51 @pragma('vm:entry-point')
52 static Route<DateTime> _datePickerRoute(BuildContext context, Object? arguments) {
53 return DialogRoute<DateTime>(
54 context: context,
55 builder: (BuildContext context) {
56 return DatePickerDialog(
57 restorationId: 'date_picker_dialog',
58 initialEntryMode: DatePickerEntryMode.calendarOnly,
59 initialDate: DateTime.fromMillisecondsSinceEpoch(arguments! as int),
60 firstDate: DateTime(2021),
61 lastDate: DateTime(2022),
62 );
63 },
64 );
65 }
66
67 @override
68 void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
69 registerForRestoration(_selectedDate, 'selected_date');
70 registerForRestoration(_restorableDatePickerRouteFuture, 'date_picker_route_future');
71 }
72
73 void _selectDate(DateTime? newSelectedDate) {
74 if (newSelectedDate != null) {
75 setState(() {
76 _selectedDate.value = newSelectedDate;
77 ScaffoldMessenger.of(context).showSnackBar(
78 SnackBar(
79 content: Text(
80 'Selected: ${_selectedDate.value.day}/${_selectedDate.value.month}/${_selectedDate.value.year}',
81 ),
82 ),
83 );
84 });
85 }
86 }
87
88 @override
89 Widget build(BuildContext context) {
90 return Scaffold(
91 body: Center(
92 child: OutlinedButton(
93 onPressed: () {
94 _restorableDatePickerRouteFuture.present();
95 },
96 child: const Text('Open Date Picker'),
97 ),
98 ),
99 );
100 }
101}
102

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com