Search code examples
flutterdrawer

Open drawer in another pages


My drawer is in my main.dart file. and I want to open the drawer with another page. of course Scaffold.of(context).openDrawer(); not worked. But I have no idea.


Solution

  • Create a public drawer like this:

    import 'package:flutter/widgets.dart';
    
    class PublicDrawer extends StatefulWidget {
      PublicDrawer({Key key}) : super(key: key);
    
      @override
      _PublicDrawerState createState() => _PublicDrawerState();
    }
    
    class _PublicDrawerState extends State<PublicDrawer> {
      @override
      void initState() {
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Container();
      }
    }
    
    

    and at your all other page, use scaffold to implement your drawer:

    import 'package:flutter/material.dart';
    import 'package:flutter/widgets.dart';
    import 'package:~~~~~~~/Widgets/drawer.dart';
    
    class ProfilePage extends StatefulWidget {
      ProfilePage({Key key}) : super(key: key);
    
      @override
      _ProfilePageState createState() => _ProfilePageState();
    }
    
    class _ProfilePageState extends State<ProfilePage> {
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: Scaffold(
            drawer: PublicDrawer(), <----
            body: Container(),
          ),
        );
      }
    }