select_form_field 1.0.1 select_form_field: ^1.0.1 copied to clipboard
A Flutter select form field widget. It shows a list of options in a dropdown menu.
import 'package:flutter/material.dart';
import 'package:select_form_field/select_form_field.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter SelectFormField Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
GlobalKey<FormState> _oFormKey = GlobalKey<FormState>();
TextEditingController _controller;
//String _initialValue;
String _valueChanged = '';
String _valueToValidate = '';
String _valueSaved = '';
final List<Map<String, dynamic>> _items = [
{
'value': 'boxValue',
'label': 'Box Label',
'icon': Icon(Icons.stop),
},
{
'value': 'circleValue',
'label': 'Circle Label Loooooooooooooooooooong text',
'icon': Icon(Icons.fiber_manual_record),
'textStyle': TextStyle(color: Colors.red),
},
{
'value': 'starValue',
'label': 'Star Label',
'enable': false,
'icon': Icon(Icons.grade),
},
];
@override
void initState() {
super.initState();
//_initialValue = 'starValue';
_controller = TextEditingController(text: 'starValue');
_getValue();
}
/// This implementation is just to simulate a load data behavior
/// from a data base sqlite or from a API
Future<void> _getValue() async {
await Future.delayed(const Duration(seconds: 3), () {
setState(() {
//_initialValue = 'circleValue';
_controller.text = 'circleValue';
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter SelectFormField Demo'),
),
body: SingleChildScrollView(
padding: EdgeInsets.only(left: 20, right: 20, top: 10),
child: Form(
key: _oFormKey,
child: Column(
children: <Widget>[
SelectFormField(
controller: _controller,
//initialValue: _initialValue,
icon: Icon(Icons.format_shapes),
labelText: 'Shape',
items: _items,
onChanged: (val) => setState(() => _valueChanged = val),
validator: (val) {
setState(() => _valueToValidate = val);
return null;
},
onSaved: (val) => setState(() => _valueSaved = val),
),
SizedBox(height: 30),
Text(
'SelectFormField data value onChanged:',
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: 10),
SelectableText(_valueChanged),
SizedBox(height: 30),
RaisedButton(
onPressed: () {
final loForm = _oFormKey.currentState;
if (loForm.validate()) {
loForm.save();
}
},
child: Text('Submit'),
),
SizedBox(height: 30),
Text(
'SelectFormField data value validator:',
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: 10),
SelectableText(_valueToValidate),
SizedBox(height: 30),
Text(
'SelectFormField data value onSaved:',
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: 10),
SelectableText(_valueSaved),
SizedBox(height: 30),
RaisedButton(
onPressed: () {
final loForm = _oFormKey.currentState;
loForm.reset();
setState(() {
_valueChanged = '';
_valueToValidate = '';
_valueSaved = '';
});
},
child: Text('Reset'),
),
],
),
),
),
);
}
}