show_date_picker.0.dart 3.02 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

7 8
/// Flutter code sample for [showDatePicker].

9
void main() => runApp(const DatePickerApp());
10

11 12
class DatePickerApp extends StatelessWidget {
  const DatePickerApp({super.key});
13 14 15

  @override
  Widget build(BuildContext context) {
16 17
    return MaterialApp(
      theme: ThemeData(useMaterial3: true),
18
      restorationScopeId: 'app',
19
      home: const DatePickerExample(restorationId: 'main'),
20 21 22 23
    );
  }
}

24 25
class DatePickerExample extends StatefulWidget {
  const DatePickerExample({super.key, this.restorationId});
26 27 28 29

  final String? restorationId;

  @override
30
  State<DatePickerExample> createState() => _DatePickerExampleState();
31 32 33
}

/// RestorationProperty objects can be used because of RestorationMixin.
34
class _DatePickerExampleState extends State<DatePickerExample> with RestorationMixin {
35 36 37 38 39
  // In this example, the restoration ID for the mixin is passed in through
  // the [StatefulWidget]'s constructor.
  @override
  String? get restorationId => widget.restorationId;

40 41
  final RestorableDateTime _selectedDate = RestorableDateTime(DateTime(2021, 7, 25));
  late final RestorableRouteFuture<DateTime?> _restorableDatePickerRouteFuture = RestorableRouteFuture<DateTime?>(
42 43 44 45 46 47 48 49 50
    onComplete: _selectDate,
    onPresent: (NavigatorState navigator, Object? arguments) {
      return navigator.restorablePush(
        _datePickerRoute,
        arguments: _selectedDate.value.millisecondsSinceEpoch,
      );
    },
  );

51
  @pragma('vm:entry-point')
52 53 54 55 56 57 58 59 60 61 62
  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),
63 64
          firstDate: DateTime(2021),
          lastDate: DateTime(2022),
65 66 67 68 69 70 71 72
        );
      },
    );
  }

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    registerForRestoration(_selectedDate, 'selected_date');
73
    registerForRestoration(_restorableDatePickerRouteFuture, 'date_picker_route_future');
74 75 76 77 78 79 80
  }

  void _selectDate(DateTime? newSelectedDate) {
    if (newSelectedDate != null) {
      setState(() {
        _selectedDate.value = newSelectedDate;
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
81 82
          content:
              Text('Selected: ${_selectedDate.value.day}/${_selectedDate.value.month}/${_selectedDate.value.year}'),
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        ));
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: OutlinedButton(
          onPressed: () {
            _restorableDatePickerRouteFuture.present();
          },
          child: const Text('Open Date Picker'),
        ),
      ),
    );
  }
}