Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Non-confidential feedback for Posse
import 'dart:async';
import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'package:numberpicker/numberpicker.dart';
class _BirthDatePickerDialog extends StatefulWidget {
const _BirthDatePickerDialog({
Key key,
@required this.initialDate,
@required this.firstYear,
@required this.lastYear,
}) : super(key: key);
final DateTime initialDate;
final int firstYear;
final int lastYear;
@override
_BirthDatePickerDialogState createState() =>
new _BirthDatePickerDialogState();
}
class _BirthDatePickerDialogState extends State<_BirthDatePickerDialog> {
@override
void initState() {
super.initState();
_selectedYear = widget.initialDate.year;
_selectedMonth = widget.initialDate.month;
_selectedDay = widget.initialDate.day;
}
int _selectedYear;
int _selectedMonth;
int _selectedDay;
void _vibrate() {
switch (Theme
.of(context)
.platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
HapticFeedback.vibrate();
break;
case TargetPlatform.iOS:
break;
}
}
void _handleYearChanged(int value) {
if (value < widget.firstYear || value > widget.lastYear)
return;
_vibrate();
setState(() {
_selectedYear = value;
});
}
void _handleMonthChanged(int value) {
if (value < 1 || value > 12)
return;
_vibrate();
setState(() {
_selectedMonth = value;
});
}
void _handleDayChanged(int value) {
if (value < 1 || value > 31)
return;
_vibrate();
setState(() {
_selectedDay = value;
});
}
void _handleCancel() {
Navigator.pop(context);
}
void _handleOk() {
Navigator.pop(
context, new DateTime(_selectedYear, _selectedMonth, _selectedDay));
}
@override
Widget build(BuildContext context) {
TextStyle headerStyle = Theme.of(context).textTheme.headline;
return new AlertDialog(
actions: <Widget>[
new FlatButton(
child: const Text('CANCEL'),
onPressed: _handleCancel,
),
new FlatButton(
child: const Text('OK'),
onPressed: _handleOk,
),
],
content: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Text('Year', style: headerStyle),
new NumberPicker.integer(
initialValue: _selectedYear,
minValue: widget.firstYear,
maxValue: widget.lastYear ?? new DateTime.now().year,
onChanged: _handleYearChanged,
),
],
),
new Flexible(
child: new Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
new Text('Month', style: headerStyle),
new NumberPicker.integer(
initialValue: _selectedMonth,
minValue: 1,
maxValue: 12,
onChanged: _handleMonthChanged,
),
],
),
),
new Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Text('Day', style: headerStyle),
new NumberPicker.integer(
initialValue: _selectedDay,
minValue: 1,
maxValue: 31,
onChanged: _handleDayChanged,
),
],
),
],
),
);
}
}
/// Shows a dialog containing a birth date picker.
///
/// The returned [Future] resolves to the date selected by the user when the
/// user closes the dialog. If the user cancels the dialog, null is returned.
///
/// See also:
///
/// * [showTimePicker]
/// * [showDatePicker]
/// * <https://material.google.com/components/pickers.html#pickers-date-pickers>
Future<DateTime> showBirthDatePicker({
@required BuildContext context,
@required DateTime initialDate,
@required int firstYear,
int lastYear,
}) async {
assert(lastYear == null ||
lastYear >= firstYear, 'lastYear must be on or after firstYear');
assert(initialDate.year >= firstYear, 'initialDate must be after firstYear');
assert(lastYear == null ||
initialDate.year <= lastYear, 'initialDate must be before lastYear');
return await showDialog(
context: context,
child: new _BirthDatePickerDialog(
initialDate: initialDate,
firstYear: firstYear,
lastYear: lastYear,
)
);
}
// Example usage:
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Birthdate Example',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
DateTime _birthDate;
Future<Null> _showBirthDateDialog(BuildContext context) async {
DateTime birthDate = await showBirthDatePicker(
context: context,
firstYear: 1900,
initialDate: _birthDate ?? new DateTime(1999, 6, 15),
);
if (mounted && birthDate != null) {
setState(() {
_birthDate = birthDate;
});
}
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Birthdate Example'),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
_birthDate == null
? 'You have not yet set your birthdate.'
: 'Your birthdate is ${new DateFormat.yMMMd().format(
_birthDate)}.',
style: Theme
.of(context)
.textTheme
.subhead,
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: () => _showBirthDateDialog(context),
tooltip: 'Edit',
child: new Icon(Icons.edit),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.