flutter –version
今回実施したflutterのバージョンは下記です。
1 2 3 4 |
Flutter 1.9.1+hotfix.2 • channel beta • https://github.com/flutter/flutter.git Framework • revision 2d2a1ffec9 (4 months ago) • 2019-09-06 18:39:49 -0700 Engine • revision b863200c37 Tools • Dart 2.5.0 |
DatePicker
カレンダーを表示して日付を取得できるウィジェットなのですが、公式HPのサンプルを実行すると例外が出て実行できませんでした。(2020/01/05現在)
例外が出ないように修正しました。
pubspec.yaml
intlを追加してください。
1 2 3 4 5 6 7 8 |
dependencies: flutter: sdk: flutter # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 intl: ^0.16.0 |
main.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import 'package:flutter/material.dart'; import 'dart:async'; void main(){ runApp(new MaterialApp( home: new MyApp(), )); } class MyApp extends StatefulWidget { @override _State createState() => new _State(); } //State is information of the application that can change over time or when some actions are taken. class _State extends State<MyApp>{ String _value = ''; Future _selectDate() async { final DateTime picked = await showDatePicker( context: context, initialDate: new DateTime.now(), firstDate: DateTime.now().subtract(Duration(days: 1)), lastDate: DateTime.now().add( Duration(days: 1095)), // firstDate: new DateTime(2016), // lastDate: new DateTime(2019) ); if(picked != null) setState(() => _value = picked.toString()); } @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text('Name here'), ), //hit Ctrl+space in intellij to know what are the options you can use in flutter widgets body: new Container( padding: new EdgeInsets.all(32.0), child: new Center( child: new Column( children: <Widget>[ new Text(_value), new RaisedButton(onPressed: _selectDate, child: new Text('Click me'),) ], ), ), ), ); } } |
過去の日付が選べない。
ただ、この場合だと過去の日付が選択できないようです。未来の日付は選択できるのですが。。。引き続き調査が必要です。
できた
firstDate: http://DateTime.now().subtract(Duration(days: 1)),
↓
firstDate: http://DateTime.now().add(Duration(days: -1095)),
に変更したらできました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import 'package:flutter/material.dart'; import 'dart:async'; void main(){ runApp(new MaterialApp( home: new MyApp(), )); } class MyApp extends StatefulWidget { @override _State createState() => new _State(); } //State is information of the application that can change over time or when some actions are taken. class _State extends State<MyApp>{ String _value = ''; Future _selectDate() async { final DateTime picked = await showDatePicker( context: context, initialDate: new DateTime.now(), // firstDate: DateTime.now().subtract(Duration(days: 1)), firstDate: DateTime.now().add(Duration(days: -1095)), lastDate: DateTime.now().add( Duration(days: 1095)), // firstDate: new DateTime(2016), // lastDate: new DateTime(2019) ); if(picked != null) setState(() => _value = picked.toString()); } @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text('Name here'), ), //hit Ctrl+space in intellij to know what are the options you can use in flutter widgets body: new Container( padding: new EdgeInsets.all(32.0), child: new Center( child: new Column( children: <Widget>[ new Text(_value), new RaisedButton(onPressed: _selectDate, child: new Text('Click me'),) ], ), ), ), ); } } |