Skip to content

fix: #1474 improve form state updates #1487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 37 additions & 54 deletions example/lib/minimal_code_example.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:form_builder_validators/form_builder_validators.dart';

void main() => runApp(const MyApp());

Expand All @@ -13,12 +11,6 @@ class MyApp extends StatelessWidget {
return MaterialApp(
title: 'Flutter FormBuilder Example',
debugShowCheckedModeBanner: false,
localizationsDelegates: const [
FormBuilderLocalizations.delegate,
...GlobalMaterialLocalizations.delegates,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: FormBuilderLocalizations.supportedLocales,
home: const _ExamplePage(),
);
}
Expand All @@ -37,56 +29,47 @@ class _ExamplePageState extends State<_ExamplePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Minimal code example')),
body: Padding(
padding: const EdgeInsets.all(16),
child: FormBuilder(
key: _formKey,
child: Column(
children: [
FormBuilderFilterChips<String>(
decoration: const InputDecoration(
labelText: 'The language of my people',
),
name: 'languages_filter',
selectedColor: Colors.red,
options: const [
FormBuilderChipOption(
value: 'Dart',
avatar: CircleAvatar(child: Text('D')),
body: SafeArea(
child: Column(
children: [
FormBuilder(
key: _formKey,
child: Column(
children: [
FormBuilderTextField(
name: 'full_name',
decoration: const InputDecoration(labelText: 'Full Name'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your full name';
}
return null;
},
),
FormBuilderChipOption(
value: 'Kotlin',
avatar: CircleAvatar(child: Text('K')),
),
FormBuilderChipOption(
value: 'Java',
avatar: CircleAvatar(child: Text('J')),
),
FormBuilderChipOption(
value: 'Swift',
avatar: CircleAvatar(child: Text('S')),
),
FormBuilderChipOption(
value: 'Objective-C',
avatar: CircleAvatar(child: Text('O')),
Builder(
builder: (innerContext) {
return Text(
FormBuilder.of(innerContext).isValid
? 'OK Valid'
: 'X Invalid',
);
},
),
const SizedBox(height: 10),
Builder(builder: (innerContext) {
return ElevatedButton(
onPressed: () {
FormBuilder.of(innerContext).saveAndValidate();
debugPrint(
FormBuilder.of(innerContext).value.toString());
},
child: const Text('Print'),
);
}),
],
validator: FormBuilderValidators.compose([
FormBuilderValidators.minLength(1),
FormBuilderValidators.maxLength(3),
]),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
_formKey.currentState?.saveAndValidate();
debugPrint(_formKey.currentState?.value.toString());
},
child: const Text('Print'),
)
],
),
),
],
),
),
);
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ packages:
dependency: transitive
description:
name: async
sha256: d2872f9c19731c2e5f10444b14686eb7cc85c76274bd6c16e1816bff9a3bab63
sha256: "758e6d74e971c3e5aceb4110bfd6698efc7f501675bcfe0c775459a8140750eb"
url: "https://pub.dev"
source: hosted
version: "2.12.0"
version: "2.13.0"
boolean_selector:
dependency: transitive
description:
Expand Down
31 changes: 29 additions & 2 deletions lib/src/form_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,35 @@
this.canPop,
});

static FormBuilderState? of(BuildContext context) =>
context.findAncestorStateOfType<FormBuilderState>();
static FormBuilderState of(BuildContext context, [bool listen = false]) {
final FormBuilderState? formState = maybeOf(context, listen);
assert(() {
if (formState == null) {
throw FlutterError(

Check warning on line 117 in lib/src/form_builder.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/form_builder.dart#L117

Added line #L117 was not covered by tests
'FormBuilder.of() was called with a context that does not contain a FormBuilder widget.\n'
'No FormBuilder widget ancestor could be found starting from the context that '
'was passed to FormBuilder.of(). This can happen because you are using a widget '
'that looks for a FormBuilder ancestor, but no such ancestor exists.\n'
'The context used was:\n'
' $context',
);
}
return true;
}());
return formState!;
}

static FormBuilderState? maybeOf(
BuildContext context, [
bool listen = false,
]) {
if (listen) {
return context
.dependOnInheritedWidgetOfExactType<_FormBuilderScope>()
?._formState;

Check warning on line 138 in lib/src/form_builder.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/form_builder.dart#L137-L138

Added lines #L137 - L138 were not covered by tests
}
return context.findAncestorStateOfType<FormBuilderState>();
}

@override
FormBuilderState createState() => FormBuilderState();
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ packages:
dependency: transitive
description:
name: async
sha256: d2872f9c19731c2e5f10444b14686eb7cc85c76274bd6c16e1816bff9a3bab63
sha256: "758e6d74e971c3e5aceb4110bfd6698efc7f501675bcfe0c775459a8140750eb"
url: "https://pub.dev"
source: hosted
version: "2.12.0"
version: "2.13.0"
boolean_selector:
dependency: transitive
description:
Expand Down
Loading