How can i show a modal below another modal in Flutter?

1,361

What you can do here is fetch the height of the bottom modal with LayoutBuilder, then use this value as padding for the first displayed modal. Though by design, the modal seems to appear in a single BottomSheet. Another approach that you can look into is by populating a single modal instead of using two modal on the screen.

Share:
1,361
thassio-vinicius
Author by

thassio-vinicius

Updated on December 17, 2022

Comments

  • thassio-vinicius
    thassio-vinicius over 1 year

    So, i'm currently developing a "to-do list" app using Flutter. This app has a floating button that, when pressed, shows a modal bottom sheet with a few other buttons. Some of those buttons, when pressed, also returns modal bottom sheets with options for the user to choose from. The thing is, i can't seem to find a way to place the secondary bottom sheet directly below the primary bottom sheet. In other words, i want to make the primary modal resize to avoid being overlapped by the secondary modal. Is that possible on flutter?

    Here's what the app should look like

    Here's what the app should look like

    And here's what it currently looks like

    And here's what it currently looks like

    Here's the code example for the primary modal bottom sheet:

    taskModal(BuildContext context) {
      return showModalBottomSheet(
        isScrollControlled: true,
        context: context,
        builder: (context) {
          return Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                  topRight: Radius.circular(20), topLeft: Radius.circular(20)),
              color: Colors.white,
            ),
            child: Padding(
              padding:
                  EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
              child: SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    _buildCancelButton(),
                    TaskForm(),
                    BuildBorder(),
                    PriorityButton(),
                    BuildBorder(),
                    DateButton(),
                    BuildBorder(),
                    _buildConfirmButton(context)
                  ],
                ),
              ),
            ),
          );
        },
      );
    }
    

    And here is the code example for one of the buttons i've mentioned before (the priority button, specifically):

    class PriorityButton extends StatefulWidget {
      @override
      _PriorityButtonState createState() => _PriorityButtonState();
    }
    
    class _PriorityButtonState extends State<PriorityButton> {
      
      List<String> _priorities = [
        'Nenhuma',
        'Baixa (!)',
        'Média (!!)',
        'Alta (!!!)',
      ];
    
      String _settledPriority = 'Selecionar';
       
      @override
      Widget build(BuildContext context) {
        return GestureDetector(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              Padding(
                  padding: EdgeInsets.symmetric(horizontal: 6),
                  child: Icon(
                    Icons.flag,
                    color: Color(0xff9DA1A6),
                  )),
              Text("Prioridade"),
              Expanded(
                child: Align(
                  alignment: Alignment.centerRight,
                  child: Padding(
                    padding: EdgeInsets.only(
                        right: MediaQuery.of(context).size.width * 0.04),
                    child: Text(_settledPriority,
                        maxLines: 1),
                  ),
                ),
              ),
            ],
          ),
          onTap: () async => await _buildBottomSheet(),
        );
      }
    
      _setPriority(String priority) {
        setState(() {
          _settledPriority = priority;
        });
        Navigator.pop(context);
      }
    
    
    
    _buildBottomSheet() {
        return showModalBottomSheet(
          context: context,
          builder: (context) {
                return Container(
                  child: ListView.builder(
                    shrinkWrap: true,
                    itemCount: _priorities.length,
                    itemBuilder: (context, index) => GestureDetector(
                      child: Text(
                        _priorities\[index\],
                      ),
                      onTap: () => _setPriority(_priorities\[index\]),
                    ),
                  ),
                );
              },
        );
      }
    
    }