myDrawer function

Widget? myDrawer(
  1. BuildContext context,
  2. bool isPhone,
  3. List<MenuOption> menu
)

Implementation

Widget? myDrawer(BuildContext context, bool isPhone, List<MenuOption> menu) {
  ThemeBloc themeBloc = context.read<ThemeBloc>();
  AuthBloc authBloc = context.read<AuthBloc>();
  Authenticate? auth = authBloc.state.authenticate;
  List<MenuOption> options = [];
  for (var option in menu) {
    {
      options.add(option);
    }
  }
  options.add(MenuOption(route: 'theme', title: 'Theme', userGroups: []));
  bool loggedIn = auth?.apiKey != null;
  if (loggedIn && isPhone) {
    return Drawer(
      width: 200,
      key: const Key('drawer'),
      child: ListView.builder(
        key: const Key('listView'),
        itemCount: options.length + 1,
        itemBuilder: (context, i) {
          if (i == 0) {
            return DrawerHeader(
                child: InkWell(
                    key: const Key('tapUser'),
                    onTap: () => Navigator.pushNamed(context, '/user',
                        arguments: auth?.user),
                    child: Column(children: [
                      CircleAvatar(
                          radius: 40,
                          child: auth?.user?.image != null
                              ? Image.memory(auth!.user!.image!)
                              : Text(
                                  auth?.user?.firstName?.substring(0, 1) ?? '',
                                  style: const TextStyle(fontSize: 30))),
                      const SizedBox(height: 10),
                      Text("${auth?.user!.firstName} ",
                          style: const TextStyle(fontSize: 15)),
                      Text("${auth?.user!.lastName}",
                          style: const TextStyle(
                            fontSize: 15,
                          )),
                    ])));
          }
          if (options[i - 1].route == "theme") {
            return Padding(
              padding: const EdgeInsets.all(5.0),
              child: InkWell(
                  key: const Key('theme'),
                  onTap: () => themeBloc.add(ThemeSwitch()),
                  child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        const SizedBox(width: 5),
                        Icon(
                            size: 40,
                            themeBloc.state.themeMode == ThemeMode.light
                                ? Icons.light_mode
                                : Icons.dark_mode),
                        const SizedBox(width: 20),
                        const Text(
                          "Theme",
                          style: TextStyle(fontSize: 16),
                        ),
                      ])),
            );
          }
          return ListTile(
              key: Key('tap${options[i - 1].route}'),
              contentPadding: const EdgeInsets.all(5.0),
              title: Text(
                options[i - 1].title,
                style: const TextStyle(fontSize: 16),
              ),
              leading: Image.asset(
                options[i - 1].selectedImage ??
                    'packages/growerp_core/images/select.png',
              ),
              onTap: () {
                if (options[i - 1].route == "/") {
                  Navigator.of(context).pushNamedAndRemoveUntil(
                      '/', (Route<dynamic> route) => false,
                      arguments: options[i - 1].arguments);
                } else {
                  Navigator.pushNamed(context, options[i - 1].route!,
                      arguments: options[i - 1].arguments);
                }
              });
        },
      ),
    );
  }
  return const Text('error: should not arrive here');
}