Search code examples
flutterdartflutter-layoutflutter-web

Navigation drawer is not working in admin panel flutter


I'm building an admin panel and now when I run the code there is no error but the drawer is not working

I looked for the problem and I can't find it

These are the controllers where I create the with multiple scaffold key

class MenuController extends ChangeNotifier {
   final GlobalKey<ScaffoldState> _scaffoldkey = GlobalKey<ScaffoldState>();
  final GlobalKey<ScaffoldState> _gridScaffoldkey = GlobalKey<ScaffoldState>();
  final GlobalKey<ScaffoldState> _addProductsScaffoldkey =
      GlobalKey<ScaffoldState>();

  GlobalKey<ScaffoldState> get getScaffoldkey => _scaffoldkey;

  GlobalKey<ScaffoldState> get getGridScaffoldkey => _gridScaffoldkey;

  GlobalKey<ScaffoldState> get getAddProductsScaffoldkey =>
      _addProductsScaffoldkey;

  void controlDashboardMenu() {
    if (_scaffoldkey.currentState!.isDrawerOpen) {
      _scaffoldkey.currentState!.openDrawer();
    }
  }

  void controlProductsMenu() {
    if (_gridScaffoldkey.currentState!.isDrawerOpen) {
      _gridScaffoldkey.currentState!.openDrawer();
    }
  }

  void controlAddProductsMenu() {
    if (_addProductsScaffoldkey.currentState!.isDrawerOpen) {
      _addProductsScaffoldkey.currentState!.openDrawer();
    }
  }
}

and this is where I call it inside a function that is inside the drawer widget and i passed it to this widget

class DashboardScreen extends StatelessWidget {
  const DashboardScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: SingleChildScrollView(
        child: Column(
          children: [
            Header(fuc: () {
              context.read<MenuController>().controlDashboardMenu();
            }),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child: Column(
                    children: [ProductsWidget()],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

and here I call the key inside the scaffold for this is my main screen

class MainScreen extends StatelessWidget {
  const MainScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        key: context.read<MenuController>().getScaffoldkey,
        drawer: const SideMenu(),
      body: SafeArea(
        child: Row(
          children: [
            if (Responsive.isDesktop(context))
              const Expanded(
                child: SideMenu(),
              ),
            const Expanded(
              flex: 5,
              child: DashboardScreen(),
            ),
          ],
        ),
      ),
    );
  }
}

Solution

  • Add App bar to open the navigation drawer,using this code Scaffold.of(context).openDrawer(); fixed that

    return Scaffold(
      key: context.read<MenuController>().getScaffoldkey,
        drawer: const SideMenu(),
       appBar: AppBar(
        backgroundColor: colors_yellow,
        elevation: 0,
        leading: Builder(
          builder: (BuildContext context) {
            return IconButton(
              icon: const Icon(Icons.menu),
              onPressed: () {
                Scaffold.of(context).openDrawer();
              },
              tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
            );
          },
        ),
        title: const Text(
          'Title ',
          style: TextStyle(color: Colors.white),
        ),
      ),