mercredi 2 juin 2021

Keep variable value even after multiple page routes unless changed in flutter

I have a drawer widget with many Menu items. I have a variable that takes care of the selected item number. I initiate that with 1 to select the first item on load. When I select another MenuItem the value changes to the corresponding item number. When I redirect/reroute to the drawer page from the Menuitem page it again shows the first Menuitem. I want the variable value to remain and show the menu item which I had selected.

  import 'package:country_code_picker/country_localizations.dart';
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:wyiin_admin_portal/admin_screens/access_management.dart';
import 'package:wyiin_admin_portal/admin_screens/brand_management_main.dart';
import 'package:wyiin_admin_portal/admin_screens/language_management_main.dart';
import 'package:wyiin_admin_portal/admin_screens/product_category_management_main.dart';
import 'package:wyiin_admin_portal/admin_screens/promotion_management_main.dart';
import 'package:wyiin_admin_portal/login/password_reset.dart';
import 'package:wyiin_admin_portal/model/BrandMgmtScreens.dart';
import 'package:wyiin_admin_portal/work_in_progress.dart';

class MyAppDrawer extends StatelessWidget {
  final appTitle = 'WYIIN';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      supportedLocales: [
        const Locale('en', 'US'),
      ],
      debugShowCheckedModeBanner: false,
      localizationsDelegates: [CountryLocalizations.delegate],
      title: appTitle,
      home: MyDrawer(title: appTitle),
      theme: ThemeData(primaryColor: Color.fromRGBO(255, 255, 255, 1)),
    );
  }
}

class MyDrawer extends StatefulWidget {
  final String title;
  final int destination;

  MyDrawer({Key key, this.title, this.destination}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyDrawer> {
  var _selectedDestination;

  @override
  Widget build(BuildContext context) {
    print(_selectedDestination);
    setState(() {
      _selectedDestination =
          _selectedDestination == null ? 1 : _selectedDestination;
    });
    print(_selectedDestination);
    return ScopedModelDescendant<Screen>(builder: (_, child, model) {
      return Row(
        children: [
          Container(
            width: 255,
            child: Drawer(
              child: Container(
                color: Color.fromRGBO(42, 48, 66, 1),
                child: ListView(
                  padding: EdgeInsets.zero,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: Text('WYiiN',
                          style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                              color: Colors.white)),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 15, top: 4),
                      child: Text('Menu',
                          style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.w600,
                              color: Color.fromRGBO(106, 113, 135, 1))),
                    ),
                    ListTile(
                      visualDensity: VisualDensity(horizontal: -4, vertical: 0),
                      contentPadding: const EdgeInsets.only(left: 15, top: 4),
                      leading: _selectedDestination == 1
                          ? Image.asset('assets/images/dashboard_active.png')
                          : Image.asset('assets/images/dashboard.png'),
                      title: Text('Dashboard',
                          style: TextStyle(
                            fontSize: 13,
                            fontWeight: FontWeight.w300,
                            color: _selectedDestination == 1
                                ? Colors.white
                                : Color.fromRGBO(106, 113, 135, 1),
                          )),
                      selected: _selectedDestination == 1,
                      onTap: () => selectDestination(1),
                    ),
                    ListTile(
                      visualDensity: VisualDensity(horizontal: -4, vertical: 0),
                      contentPadding: const EdgeInsets.only(left: 15, top: 4),
                      leading: _selectedDestination == 2
                          ? Image.asset(
                              'assets/images/warranty_management_active.png')
                          : Image.asset(
                              'assets/images/warranty_management.png'),
                      title: Text('Warranty management',
                          style: TextStyle(
                            fontSize: 13,
                            fontWeight: FontWeight.w300,
                            color: _selectedDestination == 2
                                ? Colors.white
                                : Color.fromRGBO(106, 113, 135, 1),
                          )),
                      selected: _selectedDestination == 2,
                      onTap: () => selectDestination(2),
                    ),
                    ListTile(
                      visualDensity: VisualDensity(horizontal: -4, vertical: 0),
                      contentPadding: const EdgeInsets.only(left: 15, top: 4),
                      leading: _selectedDestination == 3
                          ? Image.asset(
                              'assets/images/user_management_active.png')
                          : Image.asset('assets/images/user_management.png'),
                      title: Text('User management',
                          style: TextStyle(
                            fontSize: 13,
                            fontWeight: FontWeight.w300,
                            color: _selectedDestination == 3
                                ? Colors.white
                                : Color.fromRGBO(106, 113, 135, 1),
                          )),
                      selected: _selectedDestination == 3,
                      onTap: () => selectDestination(3),
                    ),
                    ListTile(
                      visualDensity: VisualDensity(horizontal: -4, vertical: 0),
                      contentPadding: const EdgeInsets.only(left: 15, top: 4),
                      leading: _selectedDestination == 4
                          ? Image.asset(
                              'assets/images/token_management_active.png')
                          : Image.asset('assets/images/token_management.png'),
                      title: Text('Token Management',
                          style: TextStyle(
                            fontSize: 13,
                            fontWeight: FontWeight.w300,
                            color: _selectedDestination == 4
                                ? Colors.white
                                : Color.fromRGBO(106, 113, 135, 1),
                          )),
                      selected: _selectedDestination == 4,
                      onTap: () => selectDestination(4),
                    ),
                    ListTile(
                        visualDensity:
                            VisualDensity(horizontal: -4, vertical: 0),
                        contentPadding: const EdgeInsets.only(left: 15, top: 4),
                        leading: _selectedDestination == 5
                            ? Image.asset(
                                'assets/images/promotion_management_active.png')
                            : Image.asset(
                                'assets/images/promotion_management.png'),
                        title: Text('Promotion Management',
                            style: TextStyle(
                              fontSize: 13,
                              fontWeight: FontWeight.w300,
                              color: _selectedDestination == 5
                                  ? Colors.white
                                  : Color.fromRGBO(106, 113, 135, 1),
                            )),
                        selected: _selectedDestination == 5,
                        onTap: () {
                          if (model.showPromoMgmtScreen == true) {
                            model.screenDecidePromoMgmt();
                          }
                          selectDestination(5);
                        }),
                    ListTile(
                      visualDensity: VisualDensity(horizontal: -4, vertical: 0),
                      contentPadding: const EdgeInsets.only(left: 15, top: 4),
                      leading: _selectedDestination == 6
                          ? Image.asset(
                              'assets/images/Brand Management_active.png')
                          : Image.asset('assets/images/Brand Management.png'),
                      title: Text('Brand management',
                          style: TextStyle(
                            fontSize: 13,
                            fontWeight: FontWeight.w300,
                            color: _selectedDestination == 6
                                ? Colors.white
                                : Color.fromRGBO(106, 113, 135, 1),
                          )),
                      selected: _selectedDestination == 6,
                      onTap: () {
                        if (model.showScreen == true) {
                          model.screenDecide();
                        }
                        selectDestination(6);
                      },
                    ),
                    ListTile(
                        visualDensity:
                            VisualDensity(horizontal: -4, vertical: 0),
                        contentPadding: const EdgeInsets.only(left: 15, top: 4),
                        leading: _selectedDestination == 8
                            ? Image.asset(
                                'assets/images/product_category_management_active.png')
                            : Image.asset(
                                'assets/images/product_category_management.png'),
                        title: Text('Product Category management',
                            style: TextStyle(
                              fontSize: 13,
                              fontWeight: FontWeight.w300,
                              color: _selectedDestination == 8
                                  ? Colors.white
                                  : Color.fromRGBO(106, 113, 135, 1),
                            )),
                        selected: _selectedDestination == 8,
                        onTap: () {
                          if (model.showProdCatScreen == true) {
                            model.screenDecideProdCat();
                          }
                          selectDestination(8);
                        }),
                    ListTile(
                        visualDensity:
                            VisualDensity(horizontal: -4, vertical: 0),
                        contentPadding: const EdgeInsets.only(left: 15, top: 4),
                        leading: _selectedDestination == 9
                            ? Image.asset(
                                'assets/images/language_management_active.png')
                            : Image.asset(
                                'assets/images/language_management.png'),
                        title: Text('Language Management',
                            style: TextStyle(
                              fontSize: 13,
                              fontWeight: FontWeight.w300,
                              color: _selectedDestination == 9
                                  ? Colors.white
                                  : Color.fromRGBO(106, 113, 135, 1),
                            )),
                        selected: _selectedDestination == 9,
                        onTap: () {
                          if (model.showLangMgmtScreen == true) {
                            model.screenDecideLangMgmt();
                          }
                          selectDestination(9);
                        }),
                  ],
                ),
              ),
            ),
          ),
          VerticalDivider(
            width: 1,
            thickness: 1,
          ),
          Expanded(
            child: Scaffold(
                body: _selectedDestination == 6
                    ? BrandMgmtMain()
                    : _selectedDestination == 9
                        ? LanguageMgmtMain()
                        : _selectedDestination == 7
                            ? AccessMgmt()
                            : _selectedDestination == 8
                                ? ProdCatMgmtMain()
                                : _selectedDestination == 5
                                    ? PromotionMgmtMain()
                                    : _selectedDestination == 2
                                        ? WorkInProgress()
                                        : _selectedDestination == 10
                                            ? PassReset()
                                            : _selectedDestination == 1
                                                ? WorkInProgress()
                                                : WorkInProgress()),
          ),
        ],
      );
    });
  }

  void selectDestination(int index) {
    setState(() {
      _selectedDestination = index;
      print(_selectedDestination);
    });
  }
}

void body(int selection) {}



Aucun commentaire:

Enregistrer un commentaire