简体   繁体   English

如何在Flutter中实现水平可滚动header的垂直滚动列表

[英]How to achieve vertical scrolling list with horizontal scrollable header in Flutter

在此处输入图像描述 Thanks in advance,提前致谢,

The project(Using Flutter) I'm working on how to show a list of categories under the respective services.该项目(使用 Flutter)我正在研究如何在相应服务下显示类别列表。

Where categories have to be shown in vertical scrolling list and services have to be shown in the horizontal scrolling list.其中类别必须显示在垂直滚动列表中,服务必须显示在水平滚动列表中。

Both the list(categories and services) should be scrolled respective to each other.列表(类别和服务)都应该相互滚动。

It's something like multiple tabs with a single vertical scrolling list.它类似于带有单个垂直滚动列表的多个选项卡。 Having trouble while achieving this.在实现这一点时遇到了麻烦。 Kindly share the idea to overcome this.请分享克服这一点的想法。

I think what you are looking for is a NestedScrollView .我认为您正在寻找的是NestedScrollView It works well with TabBarViews.它适用于 TabBarViews。

Your code would be something like this:您的代码将是这样的:

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  var _tabController;

  @override
  void initState() {
    super.initState();

    _tabController = TabController(length: 8, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    var tabBar = TabBar(
      controller: _tabController,
      //This property will allow your tabs to be horizontally scrollable
      isScrollable: true,
      indicatorColor: Colors.black,
      labelColor: Colors.black,
      tabs: [
        //Tab 1
        //Tab 2
        //Tab 3
        //etc
      ],
    );

    return Scaffold(
      body: NestedScrollView(
        headerSliverBuilder: (context, isScrolled) => [
          SliverAppBar(
            expandedHeight: 300,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              title: Text('Title'),
              //Without this padding the title appears behind the tabs.
              //This is the whole reason why the tabBar is in a local variable, to
              //be able to use its height here.
              titlePadding: EdgeInsetsDirectional.only(
                  start: 72, bottom: tabBar.preferredSize.height + 16),
              background: //Your image
            ),
            // I did this this way to have a white bottom bar like in your photo,
            // however, you could just pass the tabBar. The background image would
            //be behind it.
            bottom: PreferredSize(
              child: Container(
                //This will keep your tabs centered if you have not enough to fill
                //the display's width 
                alignment: Alignment.center,
                width: double.infinity,
                color: Colors.white,
                child: tabBar,
              ),
              preferredSize: Size(double.infinity, tabBar.preferredSize.height),
            ),
          ),
        ],
        body: TabBarView(
          controller: _tabController,
          children: <Widget>[
            //Child 1
            //Child 2
            //Child 3
            //etc
          ],
        ),
      ),
    );
  }
}

This would be the end result:这将是最终结果:

结果

I hope this is what you were looking for:D我希望这就是你要找的东西:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM