date_picker_demo.dart 1.27 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2015 The Chromium 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 'dart:async';

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

10
class DatePickerDemo extends StatefulWidget {
11
  @override
12 13 14 15 16 17
  _DatePickerDemoState createState() => new _DatePickerDemoState();
}

class _DatePickerDemoState extends State<DatePickerDemo> {
  DateTime _selectedDate = new DateTime.now();

18
  Future<Null> _handleSelectDate() async {
19 20 21 22 23 24 25 26 27 28 29 30 31
    DateTime picked = await showDatePicker(
      context: context,
      initialDate: _selectedDate,
      firstDate: new DateTime(2015, 8),
      lastDate: new DateTime(2101)
    );
    if (picked != _selectedDate) {
      setState(() {
        _selectedDate = picked;
      });
    }
  }

32
  @override
33
  Widget build(BuildContext context) {
34 35
    return
      new Scaffold(
36
      appBar: new AppBar(title: new Text('Date picker')),
37 38 39 40 41 42 43 44
      body: new Column(
        children: <Widget>[
          new Text(new DateFormat.yMMMd().format(_selectedDate)),
          new RaisedButton(
            onPressed: _handleSelectDate,
            child: new Text('SELECT DATE')
          ),
        ],
45
        mainAxisAlignment: MainAxisAlignment.center
46
      )
47
    );
48 49
  }
}