Flutter MonthPicker


简介

运行选择一个月的可滚动月份列表

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

基本用法

  • firstData,设置用户选择的最早日期
  • lastData,设置用户可选择的最晚日期
  • selectedDate,设置当前选择的日期
  • onchanged,当用户选择一个月的时候调用

实例演示

import 'dart:async';

import 'package:flutter/material.dart';

class MothPickerDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MothPickerState();
}

class _MothPickerState extends State<MothPickerDemo> {
  DateTime _date =  DateTime.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();
  }

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