Reputation:
I have designed a flutter screen, in which I have button upon pressing that Button A
I get a popup window, which has Button 1
to add new textfield, and we can add any number of textfield, for this I have used flutter_form_bloc
dependency example. Then in the popup window there is another button, ie Button 2
, which upon pressed process the data entered into the textfields and exits the popup window.
Now when again I press the Button A
to open the popup window all the textfields are gone and so the data. I don't want that to happen. I want that those should there until the main flutter screen is there or not exited.
Moreover, upon pressing the Button 2
in the popup window the data should be passed to the class of the main flutter screen in which Button A
is there and should stored in a instance so that the data passed could be processed further.
Here are the screenshots to get the idea [Image 1]1 [Image 2]2
CODE
FormBlocListener<ListFieldFormBloc2, String, String>(
onSubmitting: (context, state) {
},
onSuccess: (context, state) {
String name1;
var parsedData = json.decode(state.successResponse);
List members = parsedData['members'];
members.forEach((member){
name1 = member['step'];
List<String> _step = [];
_step.add(member["step"]);
_AddStepsState().getsteps(members);
});
_AddStepsState(steps: members);
Navigator.pop(context);
},
onFailure: (context, state) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(state.failureResponse)));
},
child: SingleChildScrollView(
physics: ClampingScrollPhysics(),
child: Column(
children: <Widget>[
BlocBuilder<ListFieldBloc<MemberFieldBloc2>,
ListFieldBlocState<MemberFieldBloc2>>(
bloc: formBloc.members,
builder: (context, state) {
if (state.fieldBlocs.isNotEmpty) {
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: state.fieldBlocs.length,
itemBuilder: (context, i) {
return MemberCard2(
memberIndex: i,
memberField: state.fieldBlocs[i],
onRemoveMember: () =>
formBloc.removeMember(i),
);
},
);
}
return Container();
},
),
RaisedButton(
color: Colors.blue[100],
onPressed: formBloc.addMember,
child: Text('ADD STEP'),
),
],
),
),
),
I tried to pass the LIST generated to another class in these ways _AddStepsState(steps: members);
and _AddStepsState().getsteps(members);
but both time it failed.
I want to the the list of the values in the text field generated to be passed to another class
And Also I want that while the user is in Screen1 as in Image 1
if the fields are edited as in image 2
and if the user opens the popup screen again the fields should remain there and not removed.
How should I achieve it?
if any more information is required , please let me know
the link to the dependency used is here flutter form bloc
Upvotes: 0
Views: 904
Reputation: 729
I have just recently created an app that deals with a lot of forms and the solution I've gone with is as follows:
data_center.dart
where you can define classes to represent data created by a form / to be displayed in a "review" page.class MyFormData{
String firstField;
String secondField;
int quantity;
// more attributes
// optional constructor
MyFormData({this.firstField, this.secondField, this.quantity});
}
data_center.dart
MyFormData currentMyFormDataInstance; // assign an instance every time a new form starts
import 'data_center.dart' as DataCenter;
// code removed for brevity
return showDialog(
context: context,
builder: (BuildContext context) {
// create an instance to hold the current form data
currentMyFormDataInstance = DataCenter.MyFormData();
return AlertDialog(...);
]);
});
TextFormField
) value in instanceTextFormField(
controller: _myController,
onChanged: (String _incomingValue){
DataCenter.currentMyFormDataInstance.firstField = _incomingValue;
}
)
I don't know if there are any critical flaws or inefficiencies that might come along but so far, it has worked very well for me as it allows me to easily manage all the different kinds of data groups I am collecting from the UI.
Moreover, storing these data as objects rather than data types such as Map
s has allowed me to easily transform them by adding named constructors or extra methods that easily allows me to do common and frequent operations on my data.
For example, if you are using Cloud Firestore. I can add the following named constructor to easily map DocumentSnapshot
s to the class attributes.
MyFormData.fromDocumentSnapshot(){...}
Upvotes: 1