Complete Flutter Project Solutions

How to Solve AutomaticKeepAliveClientMixin Is Not Working With BottomNavigationBar In Flutter?

How to Solve AutomaticKeepAliveClientMixin Is Not Working With BottomNavigationBar In Flutter?

Expanded Widget expands a child of a Row Widget, Column Widget, or Flex so that the child fills the available space. So In this article, we will go through How to Solve AutomaticKeepAliveClientMixin Is Not Working With BottomNavigationBar In Flutter?

Solve AutomaticKeepAliveClientMixin is not working with BottomNavigationBar In Flutter?

If your AutomaticKeepAliveClientMixin continues working, please use PageView to wrap the body; the code should be like this.

From the documentation on AutomaticKeepAliveClientMixin:

/// A mixin with convenience methods for clients of [AutomaticKeepAlive]. 
/// Used with [State] subclasses.
/// Subclasses must implement [wantKeepAlive], and their [build] methods must call `super.build` (the return value will always return null, and should be ignored).

So in your example, before you return the Scaffold Widget just call super.build:

Widget build(BuildContext context) {
  super.build(context);
  return Scaffold(...);
}

Tab pages in Stack Widget and now it keeps the state of the form.

class TabDemo extends StatefulWidget {
  @override
  State createState() => _TabDemoState();
}
class _TabDemoState extends State
{
  void initState() {
    super.initState();
  }
  int _currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar( title: Text("My App Demo"),),

      body: new Stack(
        children: [
          Offstage(
              offstage: _currentIndex != 0,
              child: Tab1()
          ),
          Offstage(
              offstage: _currentIndex != 1,
              child: Tab2()
          ),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        items: [
          BottomNavigationBarItem(
              icon: new Icon(Icons.create),
              title: new Text('Info')),
          BottomNavigationBarItem(
              icon: new Icon(Icons.home_outlined),
              title: new Text('Home')),
        ],
        onTap: (int index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}

class Tab1 extends StatefulWidget {
  @override
  State createState() {
    return _Tab1State();
  }
}

class _Tab1State extends State with AutomaticKeepAliveClientMixin {
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
        margin: const EdgeInsets.all(20.0),
        child: Form(
          child: ListView(
            children: const[
              TextField(
                decoration: InputDecoration(
                    hintText: 'Enter first name'
                ),
              ),
              TextField(
                decoration: InputDecoration(
                    hintText: 'Enter last name'
                ),
              ),
            ],),)
    );
  }
  @override
  bool get wantKeepAlive => true;
}

class Tab2 extends StatefulWidget {
  @override
  State createState() {
    return _Tab2State();
  }
}
class _Tab2State extends State with AutomaticKeepAliveClientMixin {
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Text("This is my 2nd screen",style: TextStyle(fontSize: 20),);
  }
  @override
  bool get wantKeepAlive => true;
}

Output

How to Solve AutomaticKeepAliveClientMixin Is Not Working With BottomNavigationBar In Flutter

Conclusion:

Thanks for being with us on a Flutter Journey !!!

In this article, We have been through how to solve AutomaticKeepAliveClientMixin is not working with BottomNavigationBar in Flutter?

Keep Learning !!! Keep Fluttering !!!

flutteragency.com a leading Flutter app development company dedicated to Flutter information, news, updates. The portal is full of cool resources from Flutter like Widget GuideFlutter Projectstemplates, themes and etc.

Request a Quote