Skip to content

Commit ddf3ffa

Browse files
committed
Revert plant_forms to previous implementation w/ minimal changes required to support nbbd
- planning on refactoring this from scratch in the future Add windows platform support for easier testing
1 parent 6830d9f commit ddf3ffa

36 files changed

+1346
-231
lines changed

vignettes/plant_forms/.metadata

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ migration:
1515
- platform: root
1616
create_revision: abb292a07e20d696c4568099f918f6c5f330e6b0
1717
base_revision: abb292a07e20d696c4568099f918f6c5f330e6b0
18-
- platform: android
18+
- platform: windows
1919
create_revision: abb292a07e20d696c4568099f918f6c5f330e6b0
2020
base_revision: abb292a07e20d696c4568099f918f6c5f330e6b0
2121

vignettes/plant_forms/lib/components/section_separator.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import '../styles.dart';
44

55
class Separator extends StatelessWidget {
66
@override
7-
Widget build(BuildContext context) => Container(
8-
margin: EdgeInsets.symmetric(vertical: 24.0),
9-
color: Styles.lightGrayColor,
10-
height: 1,
11-
);
7+
Widget build(BuildContext context) {
8+
return Container(
9+
margin: EdgeInsets.symmetric(vertical: 24.0),
10+
color: Styles.lightGrayColor,
11+
height: 1,
12+
);
13+
}
1214
}

vignettes/plant_forms/lib/components/section_title.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ class FormSectionTitle extends StatelessWidget {
99
const FormSectionTitle(this.title, {this.padding = const EdgeInsets.all(0), Key? key}) : super(key: key);
1010

1111
@override
12-
Widget build(BuildContext context) => Padding(
13-
padding: const EdgeInsets.only(bottom: 10.0).add(padding),
14-
child: Text(title.toUpperCase(), style: Styles.formSection),
15-
);
12+
Widget build(BuildContext context) {
13+
return Padding(
14+
padding: const EdgeInsets.only(bottom: 10.0).add(padding),
15+
child: Text(title.toUpperCase(), style: Styles.formSection),
16+
);
17+
}
1618
}

vignettes/plant_forms/lib/components/submit_button.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import '../styles.dart';
55
class SubmitButton extends StatelessWidget {
66
final double percentage;
77
final Widget child;
8-
final VoidCallback? onPressed;
8+
final void Function()? onPressed;
99
final bool isErrorVisible;
1010
final EdgeInsets padding;
1111

vignettes/plant_forms/lib/demo.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:flutter/material.dart';
2+
import 'package:plant_forms/form_pages/plant_form_payment.dart';
23
import 'package:provider/provider.dart';
34

45
import 'components/header.dart';
56
import 'components/stack_pages_route.dart';
6-
import 'plant_form_summary.dart';
77

88
class PlantFormsDemo extends StatefulWidget {
99
@override
@@ -35,7 +35,11 @@ class _PlantFormsDemoState extends State<PlantFormsDemo> {
3535
child: Navigator(
3636
key: navKey,
3737
onGenerateRoute: (route) {
38-
return StackPagesRoute(previousPages: [], enterPage: PlantFormSummary());
38+
return StackPagesRoute(
39+
previousPages: [],
40+
//enterPage: PlantFormSummary(),
41+
enterPage: PlantFormPayment(),
42+
);
3943
},
4044
),
4145
),
@@ -45,10 +49,8 @@ class _PlantFormsDemoState extends State<PlantFormsDemo> {
4549
}
4650

4751
Future<bool> _handleBackPop() async {
48-
if (navKey.currentState?.canPop() ?? false) {
49-
//If the pop worked, return false, preventing any pops in the MaterialApp's navigator
52+
if (navKey.currentState?.canPop() == true) {
5053
navKey.currentState?.pop();
51-
return false;
5254
}
5355
return true;
5456
}

vignettes/plant_forms/lib/demo_data.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
class FormKeys {
32
static String instructions = "instructions";
43
static String firstName = "first_name";
@@ -137,7 +136,7 @@ class CountryData {
137136
'Wyoming'
138137
];
139138

140-
static String getSubdivisionTitle(String country) {
139+
static String getSubdivisionTitle(String? country) {
141140
String subdivision = '';
142141
switch (country) {
143142
case 'Canada':
@@ -171,5 +170,7 @@ class CountryData {
171170
}
172171

173172
enum InputType { text, email, number, telephone }
173+
174174
enum CreditCardInputType { number, expirationDate, securityCode }
175+
175176
enum CreditCardNetwork { visa, mastercard, amex }

vignettes/plant_forms/lib/form_inputs/checkbox_input.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import '../styles.dart';
44

55
class CheckBoxInput extends StatefulWidget {
66
final String label;
7+
final Function? onChange;
78

8-
final void Function(bool? value) onChange;
9-
10-
const CheckBoxInput({Key? key, required this.label, required this.onChange}) : super(key: key);
9+
const CheckBoxInput({Key? key, required this.label, this.onChange}) : super(key: key);
1110

1211
@override
1312
_CheckBoxInputState createState() => _CheckBoxInputState();
@@ -38,7 +37,8 @@ class _CheckBoxInputState extends State<CheckBoxInput> {
3837
}
3938

4039
void _handleChange(bool? value) {
41-
setState(() => _value = value ?? false);
42-
widget.onChange.call(_value);
40+
setState(() {
41+
_value = value ?? false;
42+
});
4343
}
4444
}

vignettes/plant_forms/lib/form_inputs/credit_card_input.dart

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import 'package:flutter_masked_text2/flutter_masked_text2.dart';
33
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
44

55
import '../demo_data.dart';
6+
import 'input_validator.dart';
67
import '../styles.dart';
78

89
class CreditCardInfoInput extends StatefulWidget {
910
final String label;
1011
final String helper;
1112
final CreditCardInputType inputType;
1213
final CreditCardNetwork? cardNetwork;
13-
final void Function(String key, String value, bool isValid) onValidate;
14-
final void Function(CreditCardNetwork? cardNetwork)? onChange;
14+
final Function onValidate;
15+
final Function? onChange;
1516
final String initialValue;
1617

1718
const CreditCardInfoInput({
@@ -48,7 +49,7 @@ class _CreditCardInfoInputState extends State<CreditCardInfoInput> {
4849
set isValid(bool isValid) {
4950
if (isValid != _isValid) {
5051
_isValid = isValid;
51-
widget.onValidate(_keyValue, _value, isValid);
52+
widget.onValidate(_keyValue, _isValid, value: _value);
5253
}
5354
}
5455

@@ -73,8 +74,7 @@ class _CreditCardInfoInputState extends State<CreditCardInfoInput> {
7374
style: Styles.orderTotalLabel,
7475
onChanged: _handleChange,
7576
keyboardType: TextInputType.number,
76-
autovalidateMode: _isAutoValidating ? AutovalidateMode.always : AutovalidateMode.disabled,
77-
validator: _validateField,
77+
autovalidateMode: AutovalidateMode.onUserInteraction,
7878
decoration: Styles.getInputDecoration(helper: widget.helper),
7979
),
8080
Positioned(
@@ -125,56 +125,24 @@ class _CreditCardInfoInputState extends State<CreditCardInfoInput> {
125125
}
126126
}
127127

128-
// TODO: Consolidate the code here and in TextInput
129-
String? _validateField(String? value) {
128+
String? _validateField(String value) {
130129
// if is required
131-
if (value == null) {
130+
if (value.isEmpty) {
132131
isValid = false;
133132
_errorText = 'Required';
134133
return _errorText;
135134
}
136135
// validate when the input has a value
137-
else if (value.isNotEmpty) {
138-
if (switch (widget.inputType) {
139-
CreditCardInputType.number => _validateCreditCardNumber(value, _creditCardType),
140-
CreditCardInputType.expirationDate => _validateCreditCardExpirationDate(value),
141-
CreditCardInputType.securityCode => _validateCreditCardSecurityCode(value, _creditCardType),
142-
}) {
143-
_errorText = '';
144-
return null;
145-
}
146-
}
147-
isValid = false;
148-
_errorText = 'Not Valid';
149-
return _errorText;
150-
}
151-
152-
bool _validateCreditCardNumber(String value, CreditCardNetwork? cardNetwork) {
153-
// remove empty spaces
154-
String cardNumber = value.replaceAll(' ', '');
155-
if (cardNetwork == CreditCardNetwork.amex) {
156-
return cardNumber.length == 15;
157-
} else {
158-
return cardNumber.length == 16;
136+
else if (value.isNotEmpty && InputValidator.validate(widget.inputType, value, cardNetwork: widget.cardNetwork)) {
137+
isValid = true;
138+
_errorText = '';
139+
return null;
159140
}
160-
}
161-
162-
bool _validateCreditCardSecurityCode(String value, CreditCardNetwork? cardNetwork) {
163-
if (cardNetwork == CreditCardNetwork.amex) {
164-
return value.length == 4;
165-
} else {
166-
return value.length == 3;
167-
}
168-
}
169-
170-
bool _validateCreditCardExpirationDate(String value) {
171-
if (value.length > 3) {
172-
int month = int.parse(value.split('/').first);
173-
int year = int.parse(value.split('/').last);
174-
year += 2000;
175-
return month <= 12 && year >= DateTime.now().year;
176-
} else {
177-
return false;
141+
// the value is not valid
142+
else {
143+
isValid = false;
144+
_errorText = 'Not Valid';
145+
return _errorText;
178146
}
179147
}
180148

vignettes/plant_forms/lib/form_inputs/dropdown_menu.dart

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,39 @@ import 'package:flutter/material.dart';
22

33
import '../styles.dart';
44

5-
class FormDropdownMenu extends StatefulWidget {
6-
final void Function(String key, String value, bool isValid) onValidate;
5+
class AppDropdownMenu extends StatefulWidget {
6+
final Function onValidate;
77
final String label;
8-
final String defaultOption;
8+
final String? defaultOption;
99
final List<String> options;
1010

11-
const FormDropdownMenu({
12-
Key? key,
13-
required this.options,
14-
required this.label,
15-
required this.onValidate,
16-
required this.defaultOption,
17-
}) : super(key: key);
11+
const AppDropdownMenu(
12+
{Key? key, required this.options, required this.label, required this.onValidate, this.defaultOption})
13+
: super(key: key);
1814

1915
@override
20-
_FormDropdownMenuState createState() => _FormDropdownMenuState();
16+
_AppDropdownMenuState createState() => _AppDropdownMenuState();
2117
}
2218

23-
class _FormDropdownMenuState extends State<FormDropdownMenu> {
24-
String _selectedOption = '';
25-
bool _isValid = false;
19+
class _AppDropdownMenuState extends State<AppDropdownMenu> {
20+
String? _selectedOption = '';
21+
bool? _isValid;
2622
String get _keyValue => (widget.key as ValueKey).value as String;
2723

28-
set isValid(bool value) {
29-
if (_isValid == value) return;
30-
_isValid = value;
31-
widget.onValidate(_keyValue, _selectedOption, _isValid);
24+
set isValid(bool isValid) {
25+
_isValid = isValid;
26+
widget.onValidate(_keyValue, _isValid, value: _selectedOption);
3227
}
3328

3429
@override
3530
initState() {
3631
super.initState();
37-
_selectedOption = widget.defaultOption;
32+
_selectedOption = widget.defaultOption ?? "";
3833
}
3934

4035
@override
4136
Widget build(BuildContext context) {
42-
if (_isValid && _selectedOption.isNotEmpty) {
37+
if (_isValid == null && _selectedOption!.isNotEmpty) {
4338
isValid = true;
4439
}
4540
var items = _buildMenuItems();
@@ -102,27 +97,27 @@ class _FormDropdownMenuState extends State<FormDropdownMenu> {
10297

10398
_showOptions() async {
10499
_selectedOption = await Navigator.push(
105-
context,
106-
MaterialPageRoute(
107-
builder: (context) => DropdownOptions(
108-
title: widget.label,
109-
selectedOption: _selectedOption,
110-
options: widget.options,
111-
),
112-
)) ??
113-
_selectedOption;
114-
if (_selectedOption.isNotEmpty) isValid = true;
100+
context,
101+
MaterialPageRoute(
102+
builder: (context) => DropdownOptions(
103+
title: widget.label,
104+
selectedOption: _selectedOption ?? '',
105+
options: widget.options,
106+
),
107+
));
108+
if (_selectedOption != null && _selectedOption!.isNotEmpty) {
109+
isValid = true;
110+
}
115111
setState(() {});
116112
}
117113
}
118114

119115
class DropdownOptions extends StatefulWidget {
120116
final String title;
121117
final List<String> options;
122-
final String selectedOption;
118+
final String? selectedOption;
123119

124-
const DropdownOptions({Key? key, required this.title, required this.options, required this.selectedOption})
125-
: super(key: key);
120+
const DropdownOptions({Key? key, required this.title, required this.options, this.selectedOption}) : super(key: key);
126121

127122
@override
128123
_DropdownOptionsState createState() => _DropdownOptionsState();
@@ -133,7 +128,7 @@ class _DropdownOptionsState extends State<DropdownOptions> {
133128
String _selectedOption = '';
134129
@override
135130
void initState() {
136-
_selectedOption = widget.selectedOption;
131+
_selectedOption = widget.selectedOption ?? '';
137132
super.initState();
138133
}
139134

@@ -149,7 +144,7 @@ class _DropdownOptionsState extends State<DropdownOptions> {
149144
TextButton(
150145
child: Text('Done', style: Styles.textButton),
151146
onPressed: _sendSelectedOption,
152-
),
147+
)
153148
],
154149
),
155150
backgroundColor: Color(0xfff4f4f4),

0 commit comments

Comments
 (0)