How to overlay a widget on top of a flutter App?

后端 未结 2 1282
醉话见心
醉话见心 2020-12-18 21:38

I want a widget that will sit on top of the entire application. When I have tried to do this with Overlay.of(context).insert the overlay would later disappear a

2条回答
  •  隐瞒了意图╮
    2020-12-18 22:15

    Maybe a more optimal way exists, but as an option this is an example with two pages, local navigator and Overlay.

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State {
      final _navigatorKey = GlobalKey();
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: WillPopScope(
            onWillPop: () async => !await _navigatorKey.currentState.maybePop(),
            child: LayoutBuilder(
              builder: (context, constraints) {
                WidgetsBinding.instance.addPostFrameCallback((_) => _insertOverlay(context));
                return Navigator(
                  key: _navigatorKey,
                  onGenerateRoute: (RouteSettings settings) {
                    switch (settings.name) {
                      case '/page2':
                        return MaterialPageRoute(builder: (_) => Page2());
                      default:
                        return MaterialPageRoute(builder: (_) => Page1(_navigatorKey));
                    }
                  },
                );
              },
            ),
          ),
        );
      }
    
      void _insertOverlay(BuildContext context) {
        return Overlay.of(context).insert(
          OverlayEntry(builder: (context) {
            final size = MediaQuery.of(context).size;
            print(size.width);
            return Positioned(
              width: 56,
              height: 56,
              top: size.height - 72,
              left: size.width - 72,
              child: Material(
                color: Colors.transparent,
                child: GestureDetector(
                  onTap: () => print('ON TAP OVERLAY!'),
                  child: Container(
                    decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.redAccent),
                  ),
                ),
              ),
            );
          }),
        );
      }
    }
    
    class Page1 extends StatelessWidget {
      final GlobalKey navigatorKey;
    
      Page1(this.navigatorKey);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.green[200],
          appBar: AppBar(title: Text('Page1')),
          body: Container(
            alignment: Alignment.center,
            child: RaisedButton(
              child: Text('go to Page2'),
              onPressed: () => navigatorKey.currentState.pushNamed('/page2'),
            ),
          ),
        );
      }
    }
    
    class Page2 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.yellow[200],
          appBar: AppBar(title: Text('back to Page1')),
          body: Container(
            alignment: Alignment.center,
            child: Text('Page 2'),
          ),
        );
      }
    }
    

提交回复
热议问题