Flutter DayPicker


简介

显示给定月份的日期,并可以选择一天

  • 该选择器widget很少使用,相反,请考虑使用showDatePicker,它会创建一个日期选择器对话框

基本用法

  • currentDate,设置显示器当前显示时间
  • displayedMonth ,设置选择器显示天数的月份
  • firstDate,设置选择的最早日期
  • lastDate,设置可选择的最晚日期
  • selectedDate,设置当前选择的日期

实例演示

import 'dart:async';

import 'package:flutter/material.dart';

class DayPickerDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _DayPickerState();
}

class _DayPickerState extends State<DayPickerDemo> {
  DateTime _date =  DateTime.now();
  TimeOfDay _time =  TimeOfDay.now();

  Future<void> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: _date,
        firstDate: DateTime(2015, 8),
        lastDate: DateTime(2101));
    if (picked != null && picked != _date)
      print("data selectied :${_date.toString()}");
    setState(() {
      _date = picked;
    });

    if (picked == null) _date =  DateTime.now();
  }

  Future<void> _selectTime(BuildContext context) async {
    final TimeOfDay picked =
        await showTimePicker(context: context, initialTime: _time);
    if (picked != null && picked != _time)
      print("data selectied :${_time.toString()}");
    setState(() {
      _time = picked;
    });
    if (picked == null) _time =  TimeOfDay.now();
  }

  @override
  Widget build(BuildContext context) {
    return  Column(
      children: <Widget>[
         Text('日期选择'),
         RaisedButton(
          child:  Text('date selected:${_date.toString()}'),
          onPressed: () {
            _selectDate(context);
          },
        ),
         Text('时间选择'),
         RaisedButton(
          child:  Text('date selected:${_time.toString()}'),
          onPressed: () {
            _selectTime(context);
          },
        )
      ],
    );
  }
}