-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
Copy pathform_widgets.dart
208 lines (197 loc) · 7.44 KB
/
form_widgets.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:intl/intl.dart' as intl;
class FormWidgetsDemo extends StatefulWidget {
const FormWidgetsDemo({super.key});
@override
State<FormWidgetsDemo> createState() => _FormWidgetsDemoState();
}
class _FormWidgetsDemoState extends State<FormWidgetsDemo> {
final _formKey = GlobalKey<FormState>();
String title = '';
String description = '';
DateTime date = DateTime.now();
double maxValue = 0;
bool? brushedTeeth = false;
bool enableFeature = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Form widgets')),
body: Form(
key: _formKey,
child: Scrollbar(
child: Align(
alignment: Alignment.topCenter,
child: Card(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
...[
TextFormField(
decoration: const InputDecoration(
filled: true,
hintText: 'Enter a title...',
labelText: 'Title',
),
onChanged: (value) {
setState(() {
title = value;
});
},
),
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
filled: true,
hintText: 'Enter a description...',
labelText: 'Description',
),
onChanged: (value) {
description = value;
},
maxLines: 5,
),
_FormDatePicker(
date: date,
onChanged: (value) {
setState(() {
date = value;
});
},
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Estimated value',
style: Theme.of(context).textTheme.bodyLarge,
),
],
),
Text(
intl.NumberFormat.currency(
symbol: "\$",
decimalDigits: 0,
).format(maxValue),
style: Theme.of(context).textTheme.titleMedium,
),
Slider(
min: 0,
max: 500,
divisions: 500,
value: maxValue,
onChanged: (value) {
setState(() {
maxValue = value;
});
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Checkbox(
value: brushedTeeth,
onChanged: (checked) {
setState(() {
brushedTeeth = checked;
});
},
),
Text(
'Brushed Teeth',
style: Theme.of(context).textTheme.titleMedium,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Enable feature',
style: Theme.of(context).textTheme.bodyLarge,
),
Switch(
value: enableFeature,
onChanged: (enabled) {
setState(() {
enableFeature = enabled;
});
},
),
],
),
].expand(
(widget) => [widget, const SizedBox(height: 24)],
),
],
),
),
),
),
),
),
),
);
}
}
class _FormDatePicker extends StatefulWidget {
final DateTime date;
final ValueChanged<DateTime> onChanged;
const _FormDatePicker({required this.date, required this.onChanged});
@override
State<_FormDatePicker> createState() => _FormDatePickerState();
}
class _FormDatePickerState extends State<_FormDatePicker> {
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text('Date', style: Theme.of(context).textTheme.bodyLarge),
Text(
intl.DateFormat.yMd().format(widget.date),
style: Theme.of(context).textTheme.titleMedium,
),
],
),
TextButton(
child: const Text('Edit'),
onPressed: () async {
var newDate = await showDatePicker(
context: context,
initialDate: widget.date,
firstDate: DateTime(1900),
lastDate: DateTime(2100),
);
// Don't change the date if the date picker returns null.
if (newDate == null) {
return;
}
widget.onChanged(newDate);
},
),
],
);
}
}