date_picker_demo.dart 1.36 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 12
  static const String routeName = '/date-picker';

13
  @override
14 15 16 17 18 19
  _DatePickerDemoState createState() => new _DatePickerDemoState();
}

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

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

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