Created
December 27, 2018 11:16
-
-
Save jawand/bd9d9c6f8f69d247930614ee79b0d616 to your computer and use it in GitHub Desktop.
Flutter DropDown Validation
This file contains 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
String _town; | |
_DropDownFormField() { | |
return FormField<String>( | |
validator: (value) { | |
if (value == null) { | |
return "Select your area"; | |
} | |
}, | |
onSaved: (value) { | |
formData['town'] = value; | |
}, | |
builder: ( | |
FormFieldState<String> state, | |
) { | |
return Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
new InputDecorator( | |
decoration: const InputDecoration( | |
contentPadding: EdgeInsets.all(0.0), | |
labelText: 'Area', | |
), | |
child: DropdownButtonHideUnderline( | |
child: DropdownButton<String>( | |
hint: new Text("Select Town"), | |
value: _town, | |
onChanged: (String newValue) { | |
state.didChange(newValue); | |
setState(() { | |
_town = newValue; | |
}); | |
}, | |
items: <String>[ | |
'Sukhchayn Garden', | |
'Canal Garden', | |
'Bahria Town', | |
].map((String value) { | |
return new DropdownMenuItem<String>( | |
value: value, | |
child: new Text(value), | |
); | |
}).toList(), | |
), | |
), | |
), | |
SizedBox(height: 5.0), | |
Text( | |
state.hasError ? state.errorText : '', | |
style: | |
TextStyle(color: Colors.redAccent.shade700, fontSize: 12.0), | |
), | |
], | |
); | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment