show_date_range_picker.0.dart 3.55 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 [showDateRangePicker].

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 42
  final RestorableDateTimeN _startDate = RestorableDateTimeN(DateTime(2021));
  final RestorableDateTimeN _endDate = RestorableDateTimeN(DateTime(2021, 1, 5));
  late final RestorableRouteFuture<DateTimeRange?> _restorableDateRangePickerRouteFuture =
43 44 45
      RestorableRouteFuture<DateTimeRange?>(
    onComplete: _selectDateRange,
    onPresent: (NavigatorState navigator, Object? arguments) {
46
      return navigator.restorablePush(_dateRangePickerRoute, arguments: <String, dynamic>{
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        'initialStartDate': _startDate.value?.millisecondsSinceEpoch,
        'initialEndDate': _endDate.value?.millisecondsSinceEpoch,
      });
    },
  );

  void _selectDateRange(DateTimeRange? newSelectedDate) {
    if (newSelectedDate != null) {
      setState(() {
        _startDate.value = newSelectedDate.start;
        _endDate.value = newSelectedDate.end;
      });
    }
  }

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    registerForRestoration(_startDate, 'start_date');
    registerForRestoration(_endDate, 'end_date');
66
    registerForRestoration(_restorableDateRangePickerRouteFuture, 'date_picker_route_future');
67 68
  }

69
  @pragma('vm:entry-point')
70 71 72 73 74 75 76 77 78
  static Route<DateTimeRange?> _dateRangePickerRoute(
    BuildContext context,
    Object? arguments,
  ) {
    return DialogRoute<DateTimeRange?>(
      context: context,
      builder: (BuildContext context) {
        return DateRangePickerDialog(
          restorationId: 'date_picker_dialog',
79
          initialDateRange: _initialDateTimeRange(arguments! as Map<dynamic, dynamic>),
80
          firstDate: DateTime(2021),
81
          currentDate: DateTime(2021, 1, 25),
82
          lastDate: DateTime(2022),
83 84 85 86 87 88
        );
      },
    );
  }

  static DateTimeRange? _initialDateTimeRange(Map<dynamic, dynamic> arguments) {
89
    if (arguments['initialStartDate'] != null && arguments['initialEndDate'] != null) {
90
      return DateTimeRange(
91 92
        start: DateTime.fromMillisecondsSinceEpoch(arguments['initialStartDate'] as int),
        end: DateTime.fromMillisecondsSinceEpoch(arguments['initialEndDate'] as int),
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      );
    }

    return null;
  }

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