【问题标题】:How to implement a SingleChildScrollView(with horizontal listview and vertical gridview) inside a TabBarView如何在 TabBarView 内实现 SingleChildScrollView(具有水平列表视图和垂直网格视图)
【发布时间】:2019-04-20 06:27:09
【问题描述】:

我想有一个垂直滚动的小部件,上面有几个项目。在它的顶部应该有一个水平的ListView,然后是gridview。而这一切都在TabBarView 中。

我已尝试按照其他人的 StackOverflow 问题的建议使用 SingleChildScrollView 包装水平 ListView 和垂直 gridview,但没有成功。

Widget build(BuildContext context) {
    List tabs = <Tab>[
      Tab(text: "Home"),
      Tab(text: "Store"),
      Tab(text: "Favourite")
    ];

    return DefaultTabController(
        length: 3,
        child: Scaffold(
            key: _scaffoldKey,
            drawer: drawerSidebar(),
            backgroundColor: Colors.grey[100],
            appBar: AppBar(
              actions: <Widget>[
                IconButton(
                  icon: Icon(Icons.notifications),
                ),
                IconButton(
                  icon: Icon(Icons.message),
                ),
              ],
              title: TextField(
                controller: _filter,
                decoration: new InputDecoration(
                    fillColor: Colors.white, filled: true, hintText: 'Search'),
              ),
              bottom: TabBar(

                // indicatorSize: TabBarIndicatorSize.tab,
                tabs: tabs,
                controller: _tabController,
              ),
            ),
            body: TabBarView(
              children: <Widget>[
SingleChildScrollView(
        child:
               Column(
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[

        Explore(),                            //horizontal listview
        Expanded(
          child: FreshFinds(),              //vertical gridview.builder
        ),
      ],
    )),
       PageTwo(),
              ],
   )));
  }

这是我在使用SingleChildScrollView时遇到的错误:

I/flutter (18049): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter (18049): The following assertion was thrown during performLayout():
I/flutter (18049): RenderFlex children have non-zero flex but incoming height constraints are unbounded.
I/flutter (18049): When a column is in a parent that does not provide a finite height constraint, for example if it is
I/flutter (18049): in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a
I/flutter (18049): flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
I/flutter (18049): space in the vertical direction.
I/flutter (18049): These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child
I/flutter (18049): cannot simultaneously expand to fit its parent.
I/flutter (18049): Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible
I/flutter (18049): children (using Flexible rather than Expanded). This will allow the flexible children to size
I/flutter (18049): themselves to less than the infinite remaining space they would otherwise be forced to take, and
I/flutter (18049): then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum
I/flutter (18049): constraints provided by the parent.
I/flutter (18049): The affected RenderFlex is:
I/flutter (18049):   RenderFlex#46825 relayoutBoundary=up1 NEEDS-LAYOUT NEEDS-PAINT
I/flutter (18049): The creator information is set to:
I/flutter (18049):   Column ← _SingleChildViewport ← IgnorePointer-[GlobalKey#653c5] ← Semantics ← Listener ←
I/flutter (18049):   _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#61b8a] ←
I/flutter (18049):   _ScrollableScope ← _ScrollSemantics-[GlobalKey#7275d] ← RepaintBoundary ← CustomPaint ←
I/flutter (18049):   RepaintBoundary ← ⋯
I/flutter (18049): The nearest ancestor providing an unbounded width constraint is:
I/flutter (18049):   _RenderSingleChildViewport#72ad5 NEEDS-LAYOUT NEEDS-PAINT
I/flutter (18049):   creator: _SingleChildViewport ← IgnorePointer-[GlobalKey#653c5] ← Semantics ← Listener ←
I/flutter (18049):   _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#61b8a] ←
I/flutter (18049):   _ScrollableScope ← _ScrollSemantics-[GlobalKey#7275d] ← RepaintBoundary ← CustomPaint ←
I/flutter (18049):   RepaintBoundary ← NotificationListener<ScrollNotification> ← ⋯
I/flutter (18049):   parentData: <none> (can use size)
I/flutter (18049):   constraints: BoxConstraints(w=454.7, h=680.4)
I/flutter (18049):   size: MISSING
I/flutter (18049): See also: https://flutter.io/layout/
I/flutter (18049): If this message did not help you determine the problem, consider using debugDumpRenderTree():
I/flutter (18049):   https://flutter.io/debugging/#rendering-layer
I/flutter (18049):   http://docs.flutter.io/flutter/rendering/debugDumpRenderTree.html
I/flutter (18049): If none of the above helps enough to fix this problem, please don't hesitate to file a bug:
I/flutter (18049):   https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (18049): 

My result without using SingleChildScrollView

what I want

【问题讨论】:

  • 如果你使用SingleChildScrollView,你必须指定所有孩子的高度,所以包裹在容器内并指定高度。

标签: dart flutter


【解决方案1】:

所以我使用CustomScrollViewSlivers 解决了这个问题

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text("Settings"),
      ),
      body: Container(
        child: CustomScrollView(
          slivers: <Widget>[
            SliverList(
                delegate: SliverChildListDelegate(<Widget>[
              Container(
                child: new Image.asset(
                  "images/product.jpg",
                  fit: BoxFit.fitWidth,
                ),
              ),
            ])),
            FreshFinds()
          ],
        ),
      ),
    );
  }
//Freshfinds.dart

 Widget build(BuildContext context) {
    return SliverGrid(
      gridDelegate:
          SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
      delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
        return FutureBuilder(
          future: fetchdata(index),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (!snapshot.hasData)
              return buildfake(index);
            else
              return buildcard(snapshot.data, index);
          },
        );
      }, childCount: 20),
    );
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2020-01-26
    相关资源
    最近更新 更多