Flutter: Default Tab Bar Controller does not maintain state after swipe

后端 未结 4 1921
北恋
北恋 2021-01-03 09:55

I am writing an application in flutter that has 4 tabbed views, kinda like the stock android phone app or clock app. One of those views hash a floating action button that wh

4条回答
  •  没有蜡笔的小新
    2021-01-03 10:18

    You need to use AutomaticKeepAliveClientMixin over your stateful widget and implement override method called wantKeepAlive

    class MyApp extends StatefulWidget {
    @override
    createState() => new MyAppState();
    }
    

    use AutomaticKeepAliveClientMixin with your class extending state and ov

    class MyAppState extends State with AutomaticKeepAliveClientMixin{
    
    //your existing code.....
    
    @override
    bool get wantKeepAlive => true;   //by default it will be null, change it to true.
    
    //your existing code......
    
    }
    

    on setting wantKeepAlive to true, initState method will run only once, at the time of creation.

提交回复
热议问题