Skip to content

Commit 3c18fe6

Browse files
committed
Fixed use of widget with empty initial value, and improve documentation
1 parent 581a478 commit 3c18fe6

File tree

11 files changed

+127
-53
lines changed

11 files changed

+127
-53
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
.pub-cache/
3030
.pub/
3131
build/
32-
#example/
33-
#doc/
3432
pubspec.lock
3533

3634
# Android related

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 1.0.0 - 2020-07-17
2+
3+
* Fixed use of widget with empty initial value
4+
* Improved README.md
5+
* Improved testing
6+
* Improved example app
7+
* Final validation
8+
19
## 0.1.1 - 2020-07-16
210

311
* Example page published

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![pub package](https://img.shields.io/pub/v/select_form_field.svg)](https://pub.dartlang.org/packages/select_form_field)
44

5-
A Flutter select field widget. It shows a list of options in a dropdown menu.
5+
A Flutter select field widget. It shows a list of options in a dropdown menu.\
66
This widget extend TextField and has a similar bihavior as TextFormField
77

88
## Usage
@@ -12,7 +12,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency:
1212
```yaml
1313
dependencies:
1414
...
15-
select_form_field: "^0.1.1"
15+
select_form_field: "^1.0.0"
1616
```
1717
1818
In your library add the following import:
@@ -35,19 +35,19 @@ Set items using a List map passing:
3535
``` dart
3636
final List<Map<String, dynamic>> _items = [
3737
{
38-
'value': 'box',
39-
'label': 'Box',
38+
'value': 'boxValue',
39+
'label': 'Box Label',
4040
'icon': Icon(Icons.stop),
4141
},
4242
{
43-
'value': 'circle',
44-
'label': 'Circle',
43+
'value': 'circleValue',
44+
'label': 'Circle Label',
4545
'icon': Icon(Icons.fiber_manual_record),
4646
'textStyle': TextStyle(color: Colors.red),
4747
},
4848
{
49-
'value': 'star',
50-
'label': 'Star',
49+
'value': 'starValue',
50+
'label': 'Star Label',
5151
'enable': false,
5252
'icon': Icon(Icons.grade),
5353
},
@@ -65,4 +65,8 @@ SelectFormField(
6565
);
6666
```
6767

68-
![Overview](https://raw.githubusercontent.com/m3uzz/select_form_field/master/doc/images/select_form_field.png)
68+
The result of val in `onChanged`, `validator` and `onSaved` will be a String.\
69+
So, if you tap on Box Label item on select menu the result will be `boxValue`.
70+
71+
## Preview
72+
![Overview](https://raw.githubusercontent.com/m3uzz/select_form_field/master/doc/images/select_form_field.gif)

doc/images/select_form_field.gif

64.3 KB
Loading

doc/images/select_form_field.png

-42.9 KB
Binary file not shown.

example/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# The .vscode folder contains launch configuration and tasks you configure in
1919
# VS Code which you may wish to be included in version control, so this line
2020
# is commented out by default.
21-
#.vscode/
21+
.vscode/
2222

2323
# Flutter/Dart/Pub related
2424
**/doc/api/
@@ -29,6 +29,7 @@
2929
.pub-cache/
3030
.pub/
3131
/build/
32+
pubspec.lock
3233

3334
# Web related
3435
lib/generated_plugin_registrant.dart

example/lib/main.dart

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,21 @@ class MyApp extends StatelessWidget {
88
Widget build(BuildContext context) {
99
return MaterialApp(
1010
title: 'Flutter SelectFormField Demo',
11-
theme: ThemeData(
12-
primarySwatch: Colors.blue,
13-
),
14-
home: MyHomePage(title: 'Flutter SelectFormField Demo'),
11+
home: MyHomePage(),
1512
);
1613
}
1714
}
1815

1916
class MyHomePage extends StatefulWidget {
20-
MyHomePage({Key key, this.title}) : super(key: key);
21-
22-
final String title;
17+
MyHomePage({Key key}) : super(key: key);
2318

2419
@override
2520
_MyHomePageState createState() => _MyHomePageState();
2621
}
2722

2823
class _MyHomePageState extends State<MyHomePage> {
24+
TextEditingController _controller;
25+
//String _initialValue;
2926
String _value = '';
3027
final List<Map<String, dynamic>> _items = [
3128
{
@@ -47,25 +44,53 @@ class _MyHomePageState extends State<MyHomePage> {
4744
},
4845
];
4946

47+
@override
48+
void initState() {
49+
super.initState();
50+
51+
//_initialValue = 'starValue';
52+
_controller = TextEditingController(text: 'starValue');
53+
54+
_getValue();
55+
}
56+
57+
/// This implementation is just to simulate a load data behavior
58+
/// from a data base sqlite or from a API
59+
Future<void> _getValue() async {
60+
await Future.delayed(const Duration(seconds: 3), () {
61+
setState(() {
62+
//_initialValue = 'circleValue';
63+
_controller.text = 'circleValue';
64+
});
65+
});
66+
}
67+
5068
@override
5169
Widget build(BuildContext context) {
5270
return Scaffold(
5371
appBar: AppBar(
54-
title: Text(widget.title),
72+
title: Text('Flutter SelectFormField Demo'),
5573
),
5674
body: SingleChildScrollView(
5775
padding: EdgeInsets.only(left: 20, right: 20, top: 10),
5876
child: Form(
5977
child: Column(
6078
children: <Widget>[
6179
SelectFormField(
62-
initialValue: 'circleValue',
80+
controller: _controller,
81+
//initialValue: _initialValue,
6382
icon: Icon(Icons.format_shapes),
6483
labelText: 'Shape',
6584
items: _items,
6685
onChanged: (val) => setState(() => _value = val),
6786
onSaved: (val) => setState(() => _value = val),
6887
),
88+
SizedBox(height: 30),
89+
Text(
90+
'SelectFormField data value:',
91+
style: TextStyle(fontWeight: FontWeight.bold),
92+
),
93+
SizedBox(height: 10),
6994
Text(_value),
7095
],
7196
),

example/pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ dependencies:
2828
# The following adds the Cupertino Icons font to your application.
2929
# Use with the CupertinoIcons class for iOS style icons.
3030
cupertino_icons: ^0.1.3
31-
select_form_field:
32-
path: ../
31+
select_form_field: ^1.0.0
3332

3433
dev_dependencies:
3534
flutter_test:

lib/select_form_field.dart

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -365,30 +365,35 @@ class SelectFormField extends FormField<String> {
365365

366366
class _SelectFormFieldState extends FormFieldState<String> {
367367
TextEditingController _labelController = TextEditingController();
368-
TextEditingController _valueController;
368+
TextEditingController _stateController;
369369
Map<String, dynamic> _item;
370370

371371
@override
372372
SelectFormField get widget => super.widget as SelectFormField;
373373

374374
TextEditingController get _effectiveController =>
375-
widget.controller ?? _valueController;
375+
widget.controller ?? _stateController;
376376

377377
@override
378378
void initState() {
379379
super.initState();
380380

381381
if (widget.controller == null) {
382-
_valueController = TextEditingController(text: widget.initialValue);
382+
_stateController = TextEditingController(text: widget.initialValue);
383383
} else {
384384
widget.controller.addListener(_handleControllerChanged);
385385
}
386386

387-
_item = widget.items.firstWhere(
388-
(lmItem) => lmItem['value'] == _effectiveController.text,
389-
orElse: () => widget.items[0],
390-
);
391-
_labelController.text = _item['label'];
387+
if (_effectiveController.text != null && _effectiveController.text != '') {
388+
_item = widget.items.firstWhere(
389+
(lmItem) => lmItem['value'] == _effectiveController.text,
390+
orElse: () => null,
391+
);
392+
393+
if (_item != null) {
394+
_labelController.text = _item['label'];
395+
}
396+
}
392397
}
393398

394399
@override
@@ -400,24 +405,29 @@ class _SelectFormFieldState extends FormFieldState<String> {
400405
widget.controller?.addListener(_handleControllerChanged);
401406

402407
if (oldWidget.controller != null && widget.controller == null) {
403-
_valueController =
408+
_stateController =
404409
TextEditingController.fromValue(oldWidget.controller.value);
405410
}
406411

407412
if (widget.controller != null) {
408413
setValue(widget.controller.text);
409414

410415
if (oldWidget.controller == null) {
411-
_valueController = null;
416+
_stateController = null;
412417
}
413418
}
414419
}
415420

416-
_item = widget.items.firstWhere(
417-
(lmItem) => lmItem['value'] == _effectiveController.text,
418-
orElse: () => widget.items[0],
419-
);
420-
_labelController.text = _item['label'];
421+
if (_effectiveController.text != null && _effectiveController.text != '') {
422+
_item = widget.items.firstWhere(
423+
(lmItem) => lmItem['value'] == _effectiveController.text,
424+
orElse: () => null,
425+
);
426+
427+
if (_item != null) {
428+
_labelController.text = _item['label'];
429+
}
430+
}
421431
}
422432

423433
@override
@@ -459,12 +469,16 @@ class _SelectFormFieldState extends FormFieldState<String> {
459469
);
460470

461471
if (lvItemPicked != null && lvItemPicked != value) {
462-
_item = widget.items
463-
.firstWhere((lmItem) => lmItem['value'] == _effectiveController.text);
464-
_labelController.text = _item['label'];
472+
_item = widget.items.firstWhere(
473+
(lmItem) => lmItem['value'] == lvItemPicked,
474+
orElse: () => null,
475+
);
465476

466-
_effectiveController.text = lvItemPicked;
467-
onChangedHandler(lvItemPicked);
477+
if (_item != null) {
478+
_labelController.text = _item['label'];
479+
_effectiveController.text = lvItemPicked;
480+
onChangedHandler(lvItemPicked);
481+
}
468482
}
469483
}
470484

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: select_form_field
22
description: A Flutter select form field widget. It shows a list of options in a dropdown menu.
3-
version: 0.1.1
3+
version: 1.0.0
44
homepage: https://github.com/m3uzz/select_form_field
55

66
environment:

0 commit comments

Comments
 (0)