如何在TabBarView中集成SliverGrid?

时间:2019-06-27 03:41:54

标签: android ios flutter customscrollview

我正在尝试使用CustomScrollView和TabBar,但不确定为什么我的实现无法正常工作。

我将TabBar包裹在SliverToBoxAdapter中,但还是没有运气。 以前,我尝试过GridView,但是这样做会产生不必要的行为。

Widget build(BuildContext context) {
    return Scaffold(
        body: CustomScrollView(
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: CarouselSlider(
            items: imgList.map((i) {
              return Builder(
                builder: (BuildContext context) {
                  return Container(
                    child: ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(30.0)),
                      child: Image.network(
                        i,
                        fit: BoxFit.cover,
                        width: 1000.0,
                      ),
                    ),
                  );
                },
              );
            }).toList(),
            viewportFraction: 1.0,
            height: 300.0,
          ),
        ),
        SliverToBoxAdapter(
          child: Container(
            margin: EdgeInsets.all(10.0),
            child: Padding(
              padding: EdgeInsets.only(top: 10.0),
              child: Container(
                margin: EdgeInsets.only(left: 15.0, bottom: 15.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      child: CircleAvatar(
                        backgroundImage: AssetImage(
                            'assets/images/cut1.png'), // put image here
                        radius: 50,
                      ),
                    ),
                    Expanded(
                        child: Column(
                      children: <Widget>[
                        Text('Frank Palma',
                            style: TextStyle(
                              fontSize: 25,
                              fontWeight: FontWeight.bold,
                            )),
                        IconTheme(
                          data: IconThemeData(
                            color: Colors.amber,
                            size: 25,
                          ),
                          child: StarDisplay(value: 4),
                        ),
                        Text(
                          '546 Cuts',
                        ),
                      ],
                    ))
                  ],
                ),
              ),
            ),
          ),
        ),
        SliverToBoxAdapter(
            child: TabBar(
              controller: _tabcontroller,
              unselectedLabelColor: Colors.blue,
              indicator: BubbleTabIndicator(
                  indicatorHeight: 25.0,
                  indicatorColor: Colors.blue,
                  tabBarIndicatorSize: TabBarIndicatorSize.tab),
              tabs: <Widget>[
                Tab(icon: Icon(Icons.location_on)),
                Tab(icon: Icon(Icons.calendar_view_day)),
                Tab(icon: Icon(Icons.picture_in_picture))
              ],
            ),
        ),
        SliverToBoxAdapter(
          child: TabBarView(
            controller: _tabcontroller,
            children: [
              Center(
                  child: Card(
                    elevation: 15.0,
                    margin: EdgeInsets.all(10.0),
                    child: Container(
                      width: MediaQuery.of(context).size.width,
                      height: 200.0,
                      child: GoogleMap(
                          onMapCreated: _onMapCreated,
                          initialCameraPosition: CameraPosition(
                            target: _center,
                            zoom: 11.0,
                          )),
                    ),
                  ),
                ),
                Calendar(
                      isExpandable: true,
                      onSelectedRangeChange: (range) =>
                          print("Range is ${range.item1}, ${range.item2}"),
                      onDateSelected: (date) => print(date)),    
              SliverGrid(
                delegate: SliverChildBuilderDelegate((context, index) {
                  return Container(
                    child: FadeInImage.memoryNetwork(
                      placeholder: kTransparentImage,
                      image: 'https://picsum.photos/${300}/${300}/',
                    ),
                  );
                }, childCount: 10),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3),
              ),
            ],
          ),
        )
      ],
    ));
  }

这些是我的错误:

  

flutter:引发了另一个异常:提供了水平视口   无限的高度。扑:引发了另一个异常:RenderBox是   未布局:RenderViewport#fee96 NEEDS-LAYOUT NEEDS-PAINT抖动:   引发了另一个异常:RenderBox未被布置:   RenderViewport#fee96 NEEDS-PAINT抖动:另一个例外是   引发:未布置RenderBox:RenderIgnorePointer#ab99e   relayoutBoundary = up7 NEEDS-PAINT抖动:另一个例外是   抛出:未布置RenderBox:RenderSemanticsAnnotations#4a391   relayoutBoundary = up6 NEEDS-PAINT抖动:另一个例外是   抛出:未布置RenderBox:RenderPointerListener#c322e   relayoutBoundary = up5 NEEDS-PAINT抖动:另一个例外是   抛出:RenderBox未布置:   RenderSemanticsGestureHandler#4f759 relayoutBoundary = up4 NEEDS-PAINT   颤抖:引发了另一个异常:RenderBox未被布置:   RenderPointerListener#fa8f5 relayoutBoundary = up3 NEEDS-PAINT抖动:   引发了另一个异常:RenderBox未被布置:   _RenderScrollSemantics#c84ee relayoutBoundary = up2 NEEDS-PAINT抖动:引发了另一个异常:NoSuchMethodError:该方法   在null上调用了'debugAssertIsValid'。          ⣽flutter:引发了另一个异常:NoSuchMethodError:在null上调用了getter“ visible”。

1 个答案:

答案 0 :(得分:0)

当嵌套两个可滚动小部件时,我需要限制内部小部件的可滚动性。当我遇到此问题时,解决方案是将代码添加到内部小部件中:

ShrinkWrap: true, // Solve the problem of infinite height

Physics: NeverScrollable ScrollPhysics (), // Disable Sliding Events

希望能为您提供帮助