Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter tabview refresh issue

I have a TabBarView in my main.dart and every tab got a class to show the content(it's listview object), when i go between the tabs, the listview page refresh everytime, is it normal for tabbarview? I don't expect it will refresh everytime when i go between the tabs.

is it the problem my class? how to fix this? the code is something like this.

    class ListWidget extends StatefulWidget {
  final catID;

  ListWidget(this.catID);


  _ListWidgetState createState() => new _ListWidgetState(catID);
}

class _ListWidgetState extends State<ListWidget> {

  var catID;

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

  @override

  Widget build(BuildContext context) {
    // TODO: implement build

    return new Scaffold(.......
}
like image 267
Niccolo Avatar asked Jul 13 '18 07:07

Niccolo


People also ask

How do I refresh the app bar in flutter?

In Android app you can implement swipe to refresh by wrapping your ListView(or GridView) inside SwipeRefreshLayout, while in iOS UIRefreshControl is used for same. A widget that supports the Material “swipe to refresh” idiom.

How do you use TabController in flutter?

The selected tab's index can be changed with animateTo. A stateful widget that builds a TabBar or a TabBarView can create a TabController and share it directly. When the TabBar and TabBarView don't have a convenient stateful ancestor, a TabController can be shared by providing a DefaultTabController inherited widget.

How do you change the tab on a button click in flutter?

You need to get the TabBar controller and call its animateTo() method from the button onPressed() handle. Save this answer.


1 Answers

MahMoos is right, however it's good to have an example here ...

  1. Use AutomaticKeepAliveClientMixin
  2. override wantKeepAlive property and return true

`

class ListWidget extends StatefulWidget {

  @override
  _ListWidgetState createState() => _ListWidgetState();

}

class _ListWidgetState extends State<ListWidget> with 
                  AutomaticKeepAliveClientMixin<ListWidget>{ // ** here

  @override
  Widget build(BuildContext context) {
    super.build(context)
    return Container();
  }

  @override
  bool get wantKeepAlive => true; // ** and here
}
like image 180
Robert Apikyan Avatar answered Oct 03 '22 01:10

Robert Apikyan