WIP: Implement Streams for Forms#1162
WIP: Implement Streams for Forms#1162erayerdin wants to merge 10 commits intoflutter-form-builder-ecosystem:mainfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1162 +/- ##
==========================================
+ Coverage 84.93% 84.96% +0.02%
==========================================
Files 19 19
Lines 697 705 +8
==========================================
+ Hits 592 599 +7
- Misses 105 106 +1
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
|
I stylized the example app. You can see the changes in realtime in a table. Peek.2022-12-06.01-10.mp4 |
|
@erayerdin take a look in this article. Help me a lot when I've implemented this tests |
|
Tests are done. Added a section to readme. There are still things to do, though. Currently, I will also try how Submit and Reset buttons behave in the example app. |
|
So, I've changed The Minimum Ideal Usecaseclass CompleteForm extends StatelessWidget {
final _formKey = GlobalKey<FormBuilderState>();
CompleteForm({super.key});
@override
Widget build(BuildContext context) {
return FormBuilder(
key: _formKey,
child: Scaffold(
body: Padding(
padding: const EdgeInsets.all(8),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
FormBuilderTextField(name: 'text1'),
StreamBuilder(
stream: _formKey.currentState!.onChanged,
builder:
(context, AsyncSnapshot<FormBuilderFields> snapshot) {
return Text(
snapshot.data?.values.first.value.toString() ?? 'null');
},
),
],
),
),
),
),
);
}
}Why do I consider this as ideal?
This doesn't work though. The reason is This can be avoided by hot reloading or hot refreshing the app, but a real world user won't be able to do that. What Worksclass CompleteForm extends StatefulWidget {
const CompleteForm({super.key});
@override
State<CompleteForm> createState() => _CompleteFormState();
}
class _CompleteFormState extends State<CompleteForm> {
final _formKey = GlobalKey<FormBuilderState>();
@override
Widget build(BuildContext context) {
return FormBuilder(
key: _formKey,
onChanged: () {
setState(() {});
},
child: Scaffold(
body: Padding(
padding: const EdgeInsets.all(8),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
FormBuilderTextField(name: 'text1'),
StreamBuilder(
stream: _formKey.currentState?.onChanged,
builder:
(context, AsyncSnapshot<FormBuilderFields> snapshot) {
return Text(
snapshot.data?.values.first.value.toString() ?? 'null');
},
),
],
),
),
),
),
);
}
}This one works because we do Why do I not find this ideal usecase?
So, from this point on, I need to figure out a way to make sure that |
|
These days, I'm busy with work, so I need to have a hold on this. Will come back to it tho. |
|
It's been almost a month. I've asked this question on SO. Let's see if someone can figure it out. This comment from 1157 does not do anything good in our case. |
|
Hi! I think onChanged is not a good name for creating a stream subscription. Developers are used to seeing onChanged as ValueChanged from the foundation library. |
Connection with issue(s)
Close #1155
Solution description
FormBuilderStatenow has aStreamSubscriptionnamedonChangedso that people can subscribe to the stream of changes and react to the changes.This PR is a WIP (work-in-progress) and should not be merged yet.
_formKey.currentState!.onChanged.listen((FormBuilderFields fields) {})to listen to the form changes._formKey.currentState!.onChanged.cancel()to dispose of the stream.Screenshots or Videos
Peek.2022-12-05.15-29.mp4
This is what I added in example project, which is not very elegant to look at. I will refactor this later on. This is only for demo purposes.
As you can see,
StreamBuilderreacts to the changes live and re-renders each time the form is updated.To Do