Home > Software engineering >  Navigation drawer is not working in admin panel flutter
Navigation drawer is not working in admin panel flutter

Time:10-21

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(),
            ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

Add App bar to open the navigation drawer,I had a problem like this but 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),
    ),
  ),

CodePudding user response:

Missing AppBar for the drawer?

Maybe add the following to your Scaffold definition?

appBar: AppBar(title: Text(title)),
  • Related