Last active
May 4, 2020 20:18
-
-
Save stegrams/af7e2e82a191a3c69cddc1621ee9a89b to your computer and use it in GitHub Desktop.
A datetime picker with custom initial value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:intl/intl.dart' show DateFormat; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
final _formKey = GlobalKey<FormState>(); | |
String startDate; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Demo'), | |
), | |
body: Form( | |
key: _formKey, | |
child: Column( | |
children: <Widget>[ | |
BasicDateField(initialText: startDate), | |
TextFormField( | |
decoration: InputDecoration( | |
hintText: "Insert date here and press Enter...", | |
), | |
onChanged: (val) => setState(() => startDate = val), | |
onEditingComplete: () => setState(() { | |
print('onEditingComplete: "$startDate"'); | |
_formKey.currentState.validate(); | |
}), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class BasicDateField extends StatelessWidget { | |
BasicDateField({Key key, String initialText}) | |
: _editdatanascita = TextEditingController(text: initialText), | |
super(key: key); | |
final TextEditingController _editdatanascita; | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: <Widget>[ | |
DateTimeField( | |
controller: _editdatanascita, | |
decoration: InputDecoration(hintText: 'Nata il'), | |
format: DateFormat("dd/MM/yyyy"), | |
onShowPicker: (context, currentValue) { | |
return showDatePicker( | |
context: context, | |
firstDate: DateTime(1930), | |
initialDate: currentValue ?? DateTime.now(), | |
lastDate: DateTime(2080)); | |
}, | |
validator: (input) { | |
print('validate: "$input"'); | |
return null; | |
}, | |
onSaved: (input) => null, | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment