Home > Net >  How to call init method or specific function again when we click on already activated bottom menu
How to call init method or specific function again when we click on already activated bottom menu

Time:07-20

I have implemented following BottomNavigation

class AppMenu extends StatefulWidget {
  const AppMenu({Key? key}) : super(key: key);

  @override
  State<AppMenu> createState() => _AppMenuState();
}

class _AppMenuState extends State<AppMenu> {
  int current = 0;
  final List<String> titles = [
    "Home 1",
    "Home 2"
  ];

  final List<Widget> views = [
    const HomeView1(),
    const HomeView2(),
  ];
  final List<String> icons = [
    "icon_1",
    "icon_2",
  ];

  final List<String> barTitles = ["Home1", "Home2"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HomeAppBar(
        title: titles[current],
      ),
      bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          onTap: (index) {
            setState(() {
              current = index;
            });
          },
          selectedItemColor: const Color(0xff6B6B6B),
          showUnselectedLabels: true,
          showSelectedLabels: true,
          unselectedItemColor: const Color(0xff6B6B6B),
          selectedLabelStyle: const TextStyle(fontSize: 12),
          unselectedLabelStyle: const TextStyle(fontSize: 12),
          items: views.map((e) {
            final itemIndex = views.indexOf(e);
            return BottomNavigationBarItem(
              icon: Padding(
                padding: const EdgeInsets.only(bottom: 4),
                child: Image.asset(
                  "assets/images/${icons[itemIndex]}${itemIndex == current ? "" : "_disabled"}.png",
                  width: 25,
                ),
              ),
              label: barTitles[itemIndex],
            );
          }).toList()),
      body: Column(
        children: [
          Expanded(child: views[current]),
        ],
      ),
    );
  }
}

Now it works perfect when I click on home1 and home2 bottom menu and it shows respected widget and load all the content which I have wrote on initState of home1 and home2 but now assume that I am on home1 and if I click again home1 then it is not calling initState again.

I want to call initState or specific function if user click on that menu even if it is selected.

Is there any way to do it?

CodePudding user response:

To handle the case in a simple way. You can add your method in onTap of BottomNavigationBar and then pass your data down to the widget tree.

It's only a demonstration to handle your case, you can adjust it with your own liking

For example

bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          onTap: (index) {
            if(current == index){
                foo = yourMethodHere();
                }
            setState(() {
              current = index;
            });
          },

Pass the variable in the tree

  List<Widget> get views => [
    HomeView1(foo),
    HomeView2(foo),
  ];

CodePudding user response:

You can create a initialize or initXXX function to initialize something in initState or somewhere. If parent widget call setState(), then child widget will call didUpdateWidget().

void initialize() {
  // do something
}

Call initialize() in initState().

void initState() {
  super.initState();
  initialize();
}

Call initialize() in didUpdateWidget() of page(child widget).

@override
  void didUpdateWidget(covariant PageTest oldWidget) {
  super.didUpdateWidget(oldWidget);
  initialize();
}
  • Related