skip to Main Content

How to position a widget outside ModalBottomSheet in flutter?

I have an icon and I want to put it outside the ModalBottomSheet as shown in the picture below

enter image description here

please provide me code to achieve such a widget

2

Answers


  1. What you can do is create the bottom sheet as a column of two containers, and the first (bottom) container with transparent background. This way you can overcome the problem because the transparent container will be a part of the bottom sheet, and since you can put the icon (circle avatar etc.) "above" this container with Stack, the icon will be visible.

    So combine Stack and Positioned to achieve the result:

    import 'package:flutter/material.dart';
    
    void main() => runApp(const BottomSheetApp());
    
    class BottomSheetApp extends StatelessWidget {
      const BottomSheetApp({super.key});
    
      @override
      Widget build(BuildContext context) => MaterialApp(
            home: Scaffold(
              appBar: AppBar(title: const Text('Bottom Sheet Sample')),
              body: const BottomSheetExample(),
            ),
          );
    }
    
    class BottomSheetExample extends StatelessWidget {
      const BottomSheetExample({super.key});
    
      @override
      Widget build(BuildContext context) => Center(
            child: ElevatedButton(
              child: const Text('Show ModalBottomSheet'),
              onPressed: () {
                showModalBottomSheet<void>(
                  context: context,
                  backgroundColor: Colors.transparent,
                  builder: (BuildContext context) {
                    return SizedBox(
                        height: 264,
                        child: Stack(children: [
                          Column(children: [
                            Container(
                              height: 64,
                              color: Colors.transparent,
                            ),
                            Container(
                                height: 200,
                                width: double.infinity,
                                color: Colors.white,
                                child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      const Center(child: Text('Well done')),
                                      const SizedBox(height: 8.0),
                                      ElevatedButton(
                                        child: const Text('Close BottomSheet'),
                                        onPressed: () => Navigator.pop(context),
                                      ),
                                    ]))
                          ]),
                          Positioned(
                              left: MediaQuery.of(context).size.width / 2 - 32,
                              top: 32,
                              child: const CircleAvatar(
                                radius: 32,
                                backgroundColor: Colors.green,
                                child: Icon(Icons.check, size: 24),
                              )),
                        ]));
                  },
                );
              },
            ),
          );
    }
    
    
    Login or Signup to reply.
  2. showModalBottomSheet(
              context: context,
              builder: (BuildContext context) {
                return CustomBottomSheet();
              },
            );
    
    class CustomBottomSheet extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
       return Container(
         color: Colors.white,
        child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Stack(
            alignment: Alignment.center,
            children: [
              Container(
                height: 200,
                width: double.infinity,
                color: Colors.grey,
              ),
              Positioned(
                top: -50,
                child: ClipOval(
                  child: Container(
                    color: Colors.white,
                    width: 100,
                    height: 100,
                    Icon(Icons.arrow_circle_right_rounded,
                    size: 50
                    ),
                  ),
                ),
              ),
            ],
          ),
          //add more widgets you need.
        ],
      ),
    );
      }
    }
    

    Here call the bottomsheet and I have given an icon. Use whatever Icon or image you wish to add. Just added a stack in the bottomsheet.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search