Search code examples
flutterdartadmob

How to write a widget that is always on top | Flutter


I'm developing for Android/iOS and have stumbled upon one problem.

Even if another screen displayed by Navigator.push() or something like the image below, I want the specified widget to continue to be displayed at the bottom.

enter image description here

This means that I want to apply only to the range specified by Navigator.push() instead of moving to another widget such as Hero. I just want the banner ad to always appear at the bottom of the screen.

Can someone please tell me?


Solution

  • You can wrap your whole app in a stack widget and position your ad in the bottom of your app, and design your app accordingly.

    Or you can use a custom widget for all scaffold body

    class CustomBodyWithAddBanner extends StatelessWidget {
      final Widget child;
    
      const CustomBodyWithAddBanner({Key? key, required this.child})
          : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            Expanded(child: child),
            const Text("Your add here") // Your add widget here
          ],
        );
      }
    }
    
    

    Use this widget as the body of your scaffold, and manage your ad in this widget.