2

Flutter Web

So I have a button called add tags which opens up a modal. The Modal has only one text field and two buttons called add another tag and submit.

Now what I want to do is when the user clicks the add another tag button the app will generate another text field.

I've already seen some videos and read the documentation but since I need to work on a modal and the modal has defined size I'm not sure how to handle issues like

  1. What happens if the user adds a lot of tags. How can I make the modal scrollable?
  2. I'm new to flutter_form_builder so I'm not sure if the modal can handle it or not.

Here's my code:

final _formKey = GlobalKey<FormBuilderState>();

Future buildAddTagsForm(BuildContext context,
    {Function()? notifyParent}) async {
  return await showDialog(
    barrierDismissible: false,
    barrierColor: Colors.black.withOpacity(0.5),
    context: context,
    builder: (context) {
      var screen = MediaQuery.of(context).size;
      return StatefulBuilder(
        builder: (context, setState) {
          return AlertDialog(
            content: SingleChildScrollView(
              child: Container(
                height: screen.height / 2,
                width: screen.height > 650 ? 600.00 : screen.height * 1,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: FormBuilder(
                    key: _formKey,
                    autovalidateMode: AutovalidateMode.onUserInteraction,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      mainAxisAlignment: MainAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: <Widget>[
                            IconButton(
                              onPressed: () {
                                Navigator.pop(context);
                              },
                              icon: Icon(
                                Icons.cancel_presentation_rounded,
                              ),
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        FormBuilderTextField(
                          name: 'Tag Name',
                          decoration: InputDecoration(labelText: 'Tag name'),
                          validator: FormBuilderValidators.compose([
                            FormBuilderValidators.required(context),
                          ]),
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            MaterialButton(
                              color: CustomColors.buttonColor,
                              child: Text(
                                "Add another tag",
                                style: TextStyle(
                                  color: Colors.white,
                                ),
                              ),
                              onPressed: () {},
                            )
                          ],
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            MaterialButton(
                              color: CustomColors.buttonColor,
                              child: Text(
                                "Submit",
                                style: TextStyle(
                                  color: Colors.white,
                                ),
                              ),
                              onPressed: () {},
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        },
      );
    },
  );
}

1 Answer 1

1

I'm assuming by "modal" we're talking about the AlertDialog here:

return AlertDialog(
            content: SingleChildScrollView(

By using SingleChildScrollView as the AlertDialog content:, we can have any size / any number of text fields we like in the dialog. If their number are too many for the height of dialog inside our screen, the content will scroll.

Although, its immediate child Container with height prevents the SingleChildScrollView from doing its magic:

          return AlertDialog(
            content: SingleChildScrollView(
              child: Container(
                height: screen.height / 2, 

I think the above AlertDialog would not scroll because it would never be big enough to need to scroll. Plus, any fields added that combine to be taller than that specified height (screen.height / 2) will cause an overflow warning and be cutoff visually.

So to answer question #1: "What happens if the user adds a lot of tags. How can I make the modal scrollable?"

  • using SingleChildScrollView is the right idea
  • lets swap the position of the Container with height and the SingleChildScrollView and this should allow the dialog to grow & scroll as needed as columns in FormBuilder increase

Your question #2: "I'm new to flutter_form_builder so I'm not sure if the modal can handle it or not."

  • flutter_form_builder shouldn't affect how SingleChildScrollView works

Example

Here's a partial example of an AlertDialog with scroll view content: that can grow in number.

  Widget build(BuildContext context) {
    return Container(
      height: 300,
      child: AlertDialog(
        content: SingleChildScrollView(
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: items,
              ),
            ),
          ),
        actions: [
          OutlinedButton(
            child: Text('Add Row'),
            onPressed: _incrementCounter
          )
        ]
      ),
    );
  }

The complete example runnable in DartPard is here. (Add a 6 or 7 rows and then scroll the content.)

Warning

There's a gotcha with using the above AlertDialog inside a sized Container. That Container with height is not enough to constrain the AlertDialog size.

Your showDialog builder: (that pushes the AlertDialog into existence) must provide additional constraints in order for the sized Container to have constraints to size itself within. Without these constraints, the AlertDialog will grow until it matches the device viewport size. I believe this is a quirk with how showDialog is written, since I'm guessing it's a modal layer on top of the current stack of routes. (Someone can correct me if I'm wrong.) It's only constraint is the physical device, but nothing else. By wrapping builder:'s output with a constraining widget (such as Center) the output will be able to size itself.

To see this in action, remove the Center widget from the full example above an re-run it. The dialog will grow to fill the screen when adding rows instead of being at max 300px in height.

child: OutlinedButton(
          child: Text('Open Dialog'),
          onPressed: () => showDialog(
            context: context,
            builder: (context) => Center(child: MyDialog())
          ),
        )
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the late comment. Your solution worked after some tweaking. The responsiveness was the issue. But I solved it and everything works like a charm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.