Skip to content

Commit 7342b9f

Browse files
committed
Added more options for FormBuilderRate
1 parent 671ed72 commit 7342b9f

File tree

4 files changed

+79
-26
lines changed

4 files changed

+79
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ Made with [contributors-img](https://contributors-img.firebaseapp.com).
360360
### Dependencies
361361
This package is dependent on the following packages and plugins:
362362
* [flutter_typeahead](https://pub.dev/packages/flutter_typeahead) by [AbdulRahmanAlHamali](https://github.com/AbdulRahmanAlHamali)
363-
* [sy_flutter_widgets](https://pub.dev/packages/sy_flutter_widgets) by [Li Shuhao](https://github.com/lishuhao)
363+
* [rating_bar](https://pub.dev/packages/rating_bar) by [Joshua Matta](https://github.com/joshmatta)
364364
* [datetime_picker_formfield](https://pub.dev/packages/datetime_picker_formfield) by [Jacob Phillips](https://github.com/jifalops)
365365
* [date_range_picker](https://github.com/anicdh/date_range_picker) by [anicdh](https://github.com/anicdh)
366366
* [validators](https://pub.dev/packages/validators) by [dart-league](https://github.com/dart-league)

example/lib/main.dart

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ class MyApp extends StatelessWidget {
1616
theme: ThemeData(
1717
primarySwatch: Colors.blue,
1818
inputDecorationTheme: InputDecorationTheme(
19-
labelStyle: TextStyle(color: Colors.purple),
19+
// labelStyle: TextStyle(color: Colors.purple),
20+
border: OutlineInputBorder(
21+
gapPadding: 10,
22+
borderSide: BorderSide(color: Colors.purple),
23+
),
2024
),
2125
),
2226
home: MyHomePage(),
@@ -57,9 +61,9 @@ class MyHomePageState extends State<MyHomePage> {
5761
FormBuilder(
5862
// context,
5963
key: _fbKey,
60-
autovalidate: true,
64+
// autovalidate: true,
6165
initialValue: {
62-
'movie_rating': 5,
66+
'movie_rating': 3,
6367
},
6468
readOnly: false,
6569
child: Column(
@@ -121,7 +125,8 @@ class MyHomePageState extends State<MyHomePage> {
121125
height: 200,
122126
child: CupertinoPicker(
123127
itemExtent: 30,
124-
children: allCountries.map((c) => Text(c)).toList(),
128+
children:
129+
allCountries.map((c) => Text(c)).toList(),
125130
onSelectedItemChanged: (index) {
126131
print(index);
127132
field.didChange(allCountries[index]);
@@ -277,7 +282,8 @@ class MyHomePageState extends State<MyHomePage> {
277282
FormBuilderTextField(
278283
attribute: "age",
279284
decoration: InputDecoration(
280-
labelText: "This value is passed along to the [Text.maxLines] attribute of the [Text] widget used to display the hint text.",
285+
labelText:
286+
"This value is passed along to the [Text.maxLines] attribute of the [Text] widget used to display the hint text.",
281287
),
282288
onChanged: _onChanged,
283289
valueTransformer: (text) => num.tryParse(text),
@@ -292,6 +298,12 @@ class MyHomePageState extends State<MyHomePage> {
292298
attribute: "gender",
293299
decoration: InputDecoration(
294300
labelText: "Gender",
301+
border: UnderlineInputBorder(
302+
borderSide: BorderSide(
303+
color: Colors.green,
304+
width: 20,
305+
),
306+
),
295307
),
296308
// initialValue: 'Male',
297309
hint: Text('Select Gender'),
@@ -435,9 +447,13 @@ class MyHomePageState extends State<MyHomePage> {
435447
decoration: InputDecoration(labelText: "Rate this form"),
436448
attribute: "rate",
437449
iconSize: 32.0,
438-
initialValue: 1,
439-
max: 5,
450+
initialValue: 1.0,
451+
max: 5.0,
440452
onChanged: _onChanged,
453+
// readOnly: true,
454+
filledColor: Colors.red,
455+
emptyColor: Colors.pink[100],
456+
isHalfAllowed: true,
441457
),
442458
FormBuilderCheckboxList(
443459
decoration: InputDecoration(

lib/src/fields/form_builder_rate.dart

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ class FormBuilderRate extends StatefulWidget {
1010
final bool readOnly;
1111
final InputDecoration decoration;
1212
final ValueChanged onChanged;
13+
final FormFieldSetter onSaved;
1314
final ValueTransformer valueTransformer;
1415

15-
final num max;
1616
final IconData icon;
1717
final num iconSize;
18-
final FormFieldSetter onSaved;
18+
final num max;
19+
final Color filledColor;
20+
final IconData emptyIcon;
21+
final Color emptyColor;
22+
final bool isHalfAllowed;
23+
final IconData halfFilledIcon;
24+
final Color halfFilledColor;
1925

2026
FormBuilderRate({
2127
Key key,
@@ -30,6 +36,12 @@ class FormBuilderRate extends StatefulWidget {
3036
this.onChanged,
3137
this.valueTransformer,
3238
this.onSaved,
39+
this.filledColor,
40+
this.emptyIcon = Icons.star,
41+
this.emptyColor,
42+
this.isHalfAllowed = false,
43+
this.halfFilledIcon = Icons.star_half,
44+
this.halfFilledColor,
3345
}) : super(key: key);
3446

3547
@override
@@ -91,23 +103,44 @@ class _FormBuilderRateState extends State<FormBuilderRate> {
91103
enabled: !_readOnly,
92104
errorText: field.errorText,
93105
),
94-
child: RatingBar(
95-
initialRating: field.value,
96-
maxRating: widget.max.toInt(),
97-
filledIcon: widget.icon,
98-
emptyIcon: widget.icon,
99-
size: widget.iconSize,
100-
onRatingChanged: _readOnly
101-
? null
102-
: (value) {
103-
FocusScope.of(context).requestFocus(FocusNode());
104-
field.didChange(value);
105-
if (widget.onChanged != null) widget.onChanged(value);
106-
return value;
107-
},
108-
),
106+
child: _buildRatingBar(field),
109107
);
110108
},
111109
);
112110
}
111+
112+
Widget _buildRatingBar(FormFieldState<dynamic> field){
113+
if(_readOnly){
114+
return RatingBar.readOnly(
115+
initialRating: field.value,
116+
maxRating: widget.max.toInt(),
117+
filledIcon: widget.icon,
118+
filledColor: widget.filledColor,
119+
emptyIcon: widget.emptyIcon,
120+
emptyColor: widget.emptyColor,
121+
isHalfAllowed: widget.isHalfAllowed,
122+
halfFilledIcon: widget.halfFilledIcon,
123+
halfFilledColor: widget.halfFilledColor,
124+
size: widget.iconSize,
125+
);
126+
}
127+
return RatingBar(
128+
initialRating: field.value,
129+
maxRating: widget.max.toInt(),
130+
filledIcon: widget.icon,
131+
filledColor: widget.filledColor,
132+
emptyIcon: widget.emptyIcon,
133+
emptyColor: widget.emptyColor,
134+
isHalfAllowed: widget.isHalfAllowed,
135+
halfFilledIcon: widget.halfFilledIcon,
136+
halfFilledColor: widget.halfFilledColor,
137+
size: widget.iconSize,
138+
onRatingChanged: (value) {
139+
FocusScope.of(context).requestFocus(FocusNode());
140+
field.didChange(value);
141+
if (widget.onChanged != null) widget.onChanged(value);
142+
return value;
143+
},
144+
);
145+
}
113146
}

lib/src/form_builder.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ class FormBuilderState extends State<FormBuilder> {
9696
}
9797

9898
void reset() {
99-
_formKey.currentState.reset();
99+
// _formKey.currentState.reset();
100+
_fieldKeys.forEach((mapKey, fieldKey){
101+
print("Reseting $mapKey");
102+
fieldKey.currentState.reset();
103+
});
100104
}
101105

102106
@override

0 commit comments

Comments
 (0)