@@ -2,44 +2,39 @@ import 'package:flutter/material.dart';
22
33import '../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
119115class 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