Skip to content

Commit

Permalink
2.1.5 Validator in form: The form field validator is called by the pa…
Browse files Browse the repository at this point in the history
…rent form validator. Thanks @martin-labanic #68
  • Loading branch information
lcuis committed Oct 28, 2022
1 parent 6f4f8c4 commit f8f35f2
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 51 deletions.
4 changes: 3 additions & 1 deletion .idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.5

* Validator in form: The form field validator is called by the parent form validator. Thanks @martin-labanic https://github.com/lcuis/search_choices/issues/68

## 2.1.4

* Updated kotlin version from 1.3.50 to 1.7.20 . Thanks @wutsi https://github.com/lcuis/search_choices/issues/91
Expand Down
171 changes: 128 additions & 43 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions example/integration_test/example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void main() async {
86,
88,
90,
96,
];
List<int> failedTests = [];
app.main(testing: true);
Expand Down
95 changes: 94 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class MyAppState extends State<MyApp> {
List<DropdownMenuItem> editableItems = [];
List<DropdownMenuItem> futureItems = [];
final _formKey = GlobalKey<FormState>();
final _formKeyForValidator = GlobalKey<FormState>();
String inputString = "";
TextFormField? input;
List<DropdownMenuItem<ExampleNumber>> numberItems =
Expand All @@ -127,6 +128,9 @@ class MyAppState extends State<MyApp> {
String widgetSearchString = "";

TextEditingController widgetSearchController = TextEditingController();
final _messengerKey = GlobalKey<ScaffoldMessengerState>();

String? formResult;

@override
void initState() {
Expand Down Expand Up @@ -2253,7 +2257,88 @@ class MyAppState extends State<MyApp> {
return (menuWidget(searchTerms: searchTerms));
});
},
)
),
"Validator in form": Form(
key: _formKeyForValidator,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextFormField(
validator: (String? value) {
if (value == "ok") {
return (null);
}
return ("Not the expected value");
},
),
SearchChoices.single(
items: items,
value: selectedValueSingleDialog,
onChanged: (value) {
setState(() {
selectedValueSingleDialog = value;
});
},
isExpanded: true,
validator: (dynamic value) {
if (value == null) {
return ("Must select a value");
}
if (!(value is String)) {
return ("Selected value must be a String");
}
if (value.startsWith("l")) {
return (null);
}
return ("Must start with 'l'");
},
),
SearchChoices.multiple(
items: items,
selectedItems: selectedItemsMultiDialog,
onChanged: (value) {
setState(() {
selectedItemsMultiDialog = value;
});
},
isExpanded: true,
validator: (dynamic value) {
if (value == null) {
return ("Must select some values");
}
if (!(value is List<int>)) {
return ("Selection is of unexpected type");
}
if (value.length < 3) {
return ("Must select at least 3");
}
return (null);
},
),
TextButton(
onPressed: () {
if (_formKeyForValidator.currentState?.validate() ?? false) {
setState(() {
formResult = "All good";
});
} else {
setState(() {
formResult = "Form is not valid!";
});
}
},
child: const Text("Ok"),
),
formResult == null
? SizedBox.shrink()
: Text(formResult ?? "",
style: TextStyle(
color:
formResult == "All good" ? Colors.black : Colors.red,
)),
],
),
),
};

List<Widget> exampleWidgets = [];
Expand Down Expand Up @@ -2288,6 +2373,7 @@ class MyAppState extends State<MyApp> {
}

return MaterialApp(
scaffoldMessengerKey: _messengerKey,
theme: ThemeData(
textSelectionTheme: TextSelectionThemeData(
cursorColor: FlutsterTestRecord.defaultRecord.active
Expand Down Expand Up @@ -2441,4 +2527,11 @@ class MyAppState extends State<MyApp> {
),
))));
}

void snack(BuildContext context, dynamic content) {
if (content is String) {
content = Text(content);
}
_messengerKey.currentState?.showSnackBar(SnackBar(content: content));
}
}
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.1.3"
version: "2.1.5"
share_plus:
dependency: transitive
description:
Expand Down
7 changes: 4 additions & 3 deletions lib/search_choices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,8 @@ class SearchChoices<T> extends FormField<T> {
super(
key: key,
builder: (FormFieldState<dynamic> state) {
return (Text("TODO"));
_SearchChoicesState<T> sCState = state as _SearchChoicesState<T>;
return (sCState.buildWidget(sCState.context));
},
onSaved: onSaved,
validator: validator,
Expand Down Expand Up @@ -1290,6 +1291,7 @@ class _SearchChoicesState<T> extends FormFieldState<T> {
}

sendSelection(dynamic selection, [BuildContext? onChangeContext]) {
didChange(selection);
try {
widget.onChanged!(selection);
} catch (e) {
Expand Down Expand Up @@ -1448,8 +1450,7 @@ class _SearchChoicesState<T> extends FormFieldState<T> {
}
}

@override
Widget build(BuildContext context) {
Widget buildWidget(BuildContext context) {
if (widget.setOpenDialog != null) {
widget.setOpenDialog!(showDialogOrMenu);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: search_choices
description: Highly versatile Widget to search through a single or multiple choices list in a dialog box or a menu. Supports pagination and future/API/webservice searches with sort and filter.
version: 2.1.4
version: 2.1.5
homepage: https://github.com/lcuis/search_choices
repository: https://github.com/lcuis/search_choices
issue_tracker: https://github.com/lcuis/search_choices/issues
Expand Down
1 change: 0 additions & 1 deletion search_choices.iml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Flutter Plugins" level="project" />
<orderEntry type="library" name="Dart Packages" level="project" />
</component>
</module>

0 comments on commit f8f35f2

Please sign in to comment.