多选项卡/页面视图混乱 [英] Multi tab / page view in flutter

查看:93
本文介绍了多选项卡/页面视图混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在flutter中创建多页面视图,其中页面与底部导航栏中的选项卡相对应,从而使与页面相对应的窗口小部件只能按需构建一次.

How do i create a multi page view in flutter where the pages correspond to tabs in a bottomnavigationbar such that the widgets corresponding to the pages are built only once and on demand.

例如,考虑一种简单的Facebook应用程序用户界面,它具有两个选项卡-提要和具有以下行为的通知:

For eg., consider a simple facebook app kind of UI with two tabs - the feed and the notification with the following behavior:

  1. 提要和通知都是通过网络获取的项目的列表.
  2. 假设原始标签为供稿,则仅当用户单击通知标签时才应获取通知
  3. 如果用户滚动供稿,然后单击通知图标,然后再次单击供稿图标,则应该记住滚动位置.

如果我使用TabBarView,则每次更改选项卡时都会重新构建小部件,因此不会保留滚动位置.

If i use a TabBarView, it rebuilds the widgets every time the tab is changed, so the scroll position is not retained.

推荐答案

要为TabBarView的页面提供唯一的滚动位置存储容器,请使用PageStorageKey.

To give the pages of the TabBarView a unique scroll position storage container, use a PageStorageKey.

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  TabController _controller;
  int _tab = 0;

  @override
  void initState() {
    _controller = new TabController(length: 2, vsync: this);
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text('Example App')),
      body: new TabBarView(
        controller: _controller,
        children: <Widget>[
          new ListView.builder(
            key: new PageStorageKey('feed'),
            itemBuilder: (BuildContext context, int index) {
              return new ListTile(
                title: new Text('Feed Item $index'),
              );
            },
          ),
          new ListView.builder(
            key: new PageStorageKey('notifications'),
            itemBuilder: (BuildContext context, int index) {
              return new ListTile(
                title: new Text('Notification $index'),
              );
            },
          ),
        ],
      ),
      bottomNavigationBar: new BottomNavigationBar(
        onTap: (int value) {
          _controller.animateTo(value);
          setState(() {
            _tab = value;
          });
        },
        currentIndex: _tab,
        items: <BottomNavigationBarItem>[
          new BottomNavigationBarItem(
            icon: new Icon(Icons.home),
            title: new Text('Home'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.notifications),
            title: new Text('Notifications'),
          ),
        ],
      ),
    );
  }
}

这篇关于多选项卡/页面视图混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆