Skip to content

Instantly share code, notes, and snippets.

@haidar786
Created October 7, 2019 06:55
Show Gist options
  • Save haidar786/1db8b4489676b0e5bab5a0e0e6ad746e to your computer and use it in GitHub Desktop.
Save haidar786/1db8b4489676b0e5bab5a0e0e6ad746e to your computer and use it in GitHub Desktop.
dropDown.dart
import 'package:flutter/material.dart';
class DropDownFormField extends FormField<dynamic> {
final String titleText;
final String hintText;
final bool required;
final String errorText;
final dynamic value;
final List dataSource;
final String textField;
final String valueField;
final Function onChanged;
final IconData iconData;
DropDownFormField(
{FormFieldSetter<dynamic> onSaved,
FormFieldValidator<dynamic> validator,
bool autovalidate = false,
this.titleText = 'Title',
this.hintText = 'Select one option',
this.required = false,
this.errorText = 'Please select one option',
this.value,
this.dataSource,
this.textField,
this.valueField,
this.onChanged,
this.iconData})
: super(
onSaved: onSaved,
validator: validator,
autovalidate: autovalidate,
builder: (FormFieldState<dynamic> state) {
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
InputDecorator(
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(12, 12, 8, 0),
labelText: titleText,
filled: false,
icon: Icon(iconData)
),
child: DropdownButtonHideUnderline(
child: DropdownButton<dynamic>(
hint: Text(
hintText,
style: TextStyle(color: Colors.grey.shade500),
),
value: value == '' ? null : value,
onChanged: (dynamic newValue) {
state.didChange(newValue);
onChanged(newValue);
},
items: dataSource.map((item) {
return DropdownMenuItem<dynamic>(
value: item[valueField],
child: Text(item[textField]),
);
}).toList(),
),
),
),
SizedBox(height: state.hasError ? 5.0 : 0.0),
Text(
state.hasError ? state.errorText : '',
style: TextStyle(color: Colors.redAccent.shade700, fontSize: state.hasError ? 12.0 : 0.0),
),
],
),
);
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment