-
Notifications
You must be signed in to change notification settings - Fork 0
A Full Form Example
Eray Erdin (&mut self) edited this page Jun 19, 2023
·
1 revision
ℹ️ The example in this page can be found in here.
Create a simple page with StatelessWidget
.
class MyFormPage extends StatelessWidget {
const MyFormPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text('my form page'),
),
);
}
}
In the body
section, we will use StreamingFormBuilder
.
class MyFormPage extends StatelessWidget {
const MyFormPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: StreamingFormBuilder(
// we have a `builder` here
builder: (context, controller, fieldFactory) {},
),
);
}
}
builder
requires a function with three parameters:
-
context
is there to access theBuildContext
of our widget. -
controller
is an instance ofStreamController
instance from Dart's standard library. It streamsMap<String, dynamic>
whereString
is the key of each field anddynamic
is the value. -
fieldFactory
is used to create aStreamController
for a specific field. Any form field inbuilder
method must usefieldFactory
to create aStreamController
. We will come back to it in the next example.
Let's populate some fields inside builder
method.
return ListView(
padding: const EdgeInsets.all(16),
children: [
// builds a `Text` to show JSON of all field values
StreamBuilder(
builder: (context, snapshot) {
final data = snapshot.data ?? {};
final raw = json.encode(data);
return Center(
child: Text(raw),
);
},
// notice how we use `controller` from `builder` method
// instead of creating our own
stream: controller.stream,
),
StreamingTextField(
// notice that we create a `StreamController` with `getFieldController` method
controller: fieldFactory.getFieldController('name'),
decoration: const InputDecoration(labelText: 'Name'),
),
StreamingTextField(
controller: fieldFactory.getFieldController('surname'),
decoration: const InputDecoration(labelText: 'Surname'),
),
StreamingTextField(
controller: fieldFactory.getFieldController('age'),
decoration: const InputDecoration(labelText: 'Age'),
),
const SizedBox(height: 8),
Row(
children: [
StreamingSwitch(
controller: fieldFactory.getFieldController('accept')),
const Text('Accept the terms and conditions'),
],
),
],
)
In this example, you should notice a few things:
- We have used a
StreamBuilder
and providedcontroller
that is provided bybuilder
method ofStreamingFormBuilder
.controller
streamsMap<String, dynamic>
whereString
is the key of each field anddynamic
is the value. - We have used
fieldFactory.getFieldController
method to createStreamController
on eachStreaming*
form widgets.getFieldController
does not only get aStreamController
, but also registers it insideStreamingFormBuilder
and listens to it. Whenever a change happens, it streams the new changes toStreamController
onStreamingFormBuilder
, which isMap<String, dynamic>
as mentioned.