/
main.dart
80 lines (73 loc) · 2.22 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import 'package:date_range_form_field/date_range_form_field.dart';
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: MyFormField(),
);
}
}
class MyFormField extends StatefulWidget {
@override
_MyFormFieldState createState() => _MyFormFieldState();
}
GlobalKey<FormState> myFormKey = new GlobalKey();
class _MyFormFieldState extends State<MyFormField> {
DateTimeRange? myDateRange;
void _submitForm() {
final FormState? form = myFormKey.currentState;
form!.save();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Date Range Form Example"),
),
body: SafeArea(
child: Form(
key: myFormKey,
child: Column(
children: [
SafeArea(
child: DateRangeField(
firstDate: DateTime(1990),
enabled: true,
initialValue: DateTimeRange(
start: DateTime.now(),
end: DateTime.now().add(Duration(days: 5))),
decoration: InputDecoration(
labelText: 'Date Range',
prefixIcon: Icon(Icons.date_range),
hintText: 'Please select a start and end date',
border: OutlineInputBorder(),
),
validator: (value) {
if (value!.start.isBefore(DateTime.now())) {
return 'Please enter a later start date';
}
return null;
},
onSaved: (value) {
setState(() {
myDateRange = value!;
});
}),
),
ElevatedButton(
child: Text('Submit'),
onPressed: _submitForm,
),
if (myDateRange != null)
Text("Saved value is: ${myDateRange.toString()}")
],
),
),
),
);
}
}