带ListViews的NestedScrollView中的粘滞选项卡 [英] Sticky tabs in NestedScrollView with ListViews

查看:106
本文介绍了带ListViews的NestedScrollView中的粘滞选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除以下内容外,布局均按要求工作:

The layout works as desired, except this:

当我在一页上滚动时,第二页也会滚动.数量不多,但足以掩盖第一项.

我可以想象这与NestedScrollView有关,但是我不知道该怎么做.

I could imagine it'd have something to do with the NestedScrollView but I don't know how to go on.

import 'package:flutter/material.dart';

main(){
  runApp(new MaterialApp(
    home: new MyHomePage(),
  ));
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new DefaultTabController(
      length: 2,
      child: new Scaffold(
        body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              new SliverAppBar(
                title: const Text('Tabs and scrolling'),
                forceElevated: innerBoxIsScrolled,
                pinned: true,
                floating: true,
                bottom: new TabBar(
                  tabs: <Tab>[
                    new Tab(text: 'Page 1'),
                    new Tab(text: 'Page 2'),
                  ],
                ),
              ),
            ];
          },
          body: new TabBarView(
            children: <Widget>[
              _list(),
              _list(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _list(){
    return ListView.builder(
      padding: EdgeInsets.zero,
      itemCount: 250,
      itemBuilder: (context, index){
        return Container(
          color: Colors.grey[200].withOpacity((index % 2).toDouble()),
          child: ListTile(
            title: Text(index.toString()),
          ),
        );
      }
    );
  }
}

推荐答案

要能够使两个ListView滚动而不互相影响,它们需要具有已定义的控制器.

To be able to keep the two ListViews to scroll without affecting each other they need to have defined controllers.

要使ListView在选项卡切换之间保持其滚动位置,您需要将它们放置在带有AutomaticKeepAliveClientMixin的小部件中.

To have the ListViews maintain their scroll position between tab switching you need to have them in a Widget with AutomaticKeepAliveClientMixin.

这是您可以代替_list方法执行的示例.定义了一个有状态的小部件,该状态小部件可以同时使用控制器和AutomaticKeepAliveClientMixin返回您的列表:

Here's an example of what you can do instead of your _list method. Defined a Stateful Widget that returns your lists using both controllers and the AutomaticKeepAliveClientMixin:

class ItemList extends StatefulWidget {
  @override
  _ItemListState createState() => _ItemListState();
}

class _ItemListState extends State<ItemList> with AutomaticKeepAliveClientMixin{
  ScrollController _scrollController = ScrollController();
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ListView.builder(
      controller: _scrollController,
      padding: EdgeInsets.zero,
      itemCount: 250,
      itemBuilder: (context, index){
        return Container(
          color: Colors.grey[200].withOpacity((index % 2).toDouble()),
          child: ListTile(
            title: Text(index.toString()),
          ),
        );
      }
    );
  }

  @override
  bool get wantKeepAlive => true;
}

您可以像在TabBarView中的其他任何小部件一样正常调用它:

You can just call it normally like any other widget inside your TabBarView:

TabBarView(
  children: <Widget>[
    ItemList(),
    ItemList(),
  ],
),

这篇关于带ListViews的NestedScrollView中的粘滞选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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