Skip to content

Instantly share code, notes, and snippets.

@muncman
Forked from jmolins/date_picker.dart
Last active May 18, 2020 20:15
Show Gist options
  • Save muncman/e641f2dde34fa57292f9c9caff1b8f06 to your computer and use it in GitHub Desktop.
Save muncman/e641f2dde34fa57292f9c9caff1b8f06 to your computer and use it in GitHub Desktop.
Add Material date picker, time picker, and Cupertino date-time picker to a flutter app.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
DateTime selectedDate = DateTime.now();
TimeOfDay selectedTime = TimeOfDay.now();
Future<Null> _selectDate(BuildContext context) async {
final DateTime pickedDate = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(1920, 8),
lastDate: DateTime(2101));
if (pickedDate != null && pickedDate != selectedDate)
setState(() {
selectedDate = pickedDate;
});
}
Future<Null> _selectTime(BuildContext context) async {
final TimeOfDay pickedTime = await showTimePicker(
initialTime: TimeOfDay.now(),
context: context,
);
if (pickedTime != null && pickedTime != selectedTime) {
setState(() {
selectedTime = pickedTime;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Selected:'),
SizedBox(
height: 20.0,
),
Text('${selectedDate.toLocal()}'.split(' ')[0]),
Text(selectedTime.format(context)),
SizedBox(
height: 20.0,
),
RaisedButton(
child: Text('Material - Select date'),
onPressed: () => _selectDate(context),
),
RaisedButton(
child: Text('Material - Select time'),
onPressed: () => _selectTime(context),
),
RaisedButton(
child: Text('Cupertino - Select date+time'),
onPressed: () {
showModalBottomSheet(
context: context,
builder: (BuildContext builder) {
return Container(
height: MediaQuery.of(context).copyWith().size.height / 3,
child: CupertinoDatePicker(
initialDateTime: DateTime.now(),
onDateTimeChanged: (DateTime newDate) {
print(newDate);
setState(
() {
selectedDate = newDate;
selectedTime = TimeOfDay.fromDateTime(newDate);
},
);
},
use24hFormat: false,
maximumDate: new DateTime(2022, 12, 30),
minimumYear: 2010,
maximumYear: 2022,
minuteInterval: 1,
mode: CupertinoDatePickerMode.dateAndTime,
),
);
},
);
},
),
RaisedButton(
child: Text('Cupertino - Select just time'),
onPressed: () {
showModalBottomSheet(
context: context,
builder: (BuildContext builder) {
return Container(
height: MediaQuery.of(context).copyWith().size.height / 3,
child: CupertinoDatePicker(
mode: CupertinoDatePickerMode.time,
initialDateTime: DateTime.now(),
minuteInterval: 5,
onDateTimeChanged: (DateTime newDate) {
print(newDate);
setState(
() {
selectedDate = newDate;
selectedTime = TimeOfDay.fromDateTime(newDate);
},
);
},
),
);
},
);
},
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment