Skip to content

Instantly share code, notes, and snippets.

@rayjadore
Forked from naumanahmed19/flutter-dropdown.dart
Created January 29, 2019 10:27
Show Gist options
  • Save rayjadore/1a3979d01d8e980424549b8b0be4583f to your computer and use it in GitHub Desktop.
Save rayjadore/1a3979d01d8e980424549b8b0be4583f to your computer and use it in GitHub Desktop.
Flutter DropdownButton With Validation
String _town;
_townField() {
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),
),
],
);
},
);
}
@mpycio
Copy link

mpycio commented Jan 4, 2020

This will show the validation error above the field (enabled)Border, remove lines 46-51 and add this line to InputDecoration config:

  decoration: InputDecoration(
    errorText: state.errorText,
  ...

@ratnapriya4g
Copy link

This will show the validation error above the field (enabled)Border, remove lines 46-51 and add this line to InputDecoration config:

  decoration: InputDecoration(
    errorText: state.errorText,
  ...

I used the same but I am getting error text but when I want to select the dropdown list item, I am unable to see and select the dropdown list.
1616005542066

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment