-
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:
-
contextis there to access theBuildContextof our widget. -
controlleris an instance ofStreamControllerinstance from Dart's standard library. It streamsMap<String, dynamic>whereStringis the key of each field anddynamicis the value. -
fieldFactoryis used to create aStreamControllerfor a specific field. Any form field inbuildermethod must usefieldFactoryto 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
StreamBuilderand providedcontrollerthat is provided bybuildermethod ofStreamingFormBuilder.controllerstreamsMap<String, dynamic>whereStringis the key of each field anddynamicis the value. - We have used
fieldFactory.getFieldControllermethod to createStreamControlleron eachStreaming*form widgets.getFieldControllerdoes not only get aStreamController, but also registers it insideStreamingFormBuilderand listens to it. Whenever a change happens, it streams the new changes toStreamControlleronStreamingFormBuilder, which isMap<String, dynamic>as mentioned.