【问题标题】:Flutter - Widgets not scrolling in NestedScrollViewFlutter - 小部件在 NestedScrollView 中不滚动
【发布时间】:2020-09-10 11:33:11
【问题描述】:

我正在尝试制作一个包含SliverAppBar、一些带有文本的小部件和一个List 的页面。

我使用了一个包含 Sliver 标头的 NestedScrollView 和一个 Column,它本身包含带有文本和列表的小部件。

这里是build 函数:

@override Widget build(BuildContext context) {

var _bar = widget.bar ;

_screenWidth = MediaQuery.of(context).size.width ;

return Scaffold(
  body: NestedScrollView(
    headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
      return <Widget>[
        SliverAppBar(
          expandedHeight: 200.0,
          floating: false,
          pinned: true,
          flexibleSpace: FlexibleSpaceBar(
              centerTitle: true,
              title: Text(_bar.name,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16.0,
                  )),
              background: Image.network(
                "https://s1.qwant.com/thumbr/0x380/1/2/2a39f1f558f2cbbec11a08e43493bde861d612add6be643dbc5ad6fe0b16fc/BDBE5B56-B1B4-586D-27C8A70A4A54E50A.jpg?u=https%3A%2F%2Fimages.bwwstatic.com%2Fcolumnpic6%2FBDBE5B56-B1B4-586D-27C8A70A4A54E50A.jpg&q=0&b=1&p=0&a=1",
                fit: BoxFit.cover,
              )),
        ),
      ];
    },
    body: _offer(_bar),
  ),
);
}

_offer 函数是主要问题,这里是:

Widget _offer(Bar bar) {
return Column(
  children: <Widget>[
    Row(
      children: <Widget>[
        Container(
          width: _screenWidth / 4,
          decoration: BoxDecoration(
              shape: BoxShape.circle,
              image: DecorationImage(
                  fit: BoxFit.fill,
                  image: Image.network("https://s1.qwant.com/thumbr/0x380/3/9/60c4de7be57ee1b7d24d07dde941c3027588bc313699cba9ef9ef8fb6c7fda/1280px-Hard_Rock_Cafe_Logo.svg.png?u=https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F2%2F2c%2FHard_Rock_Cafe_Logo.svg%2F1280px-Hard_Rock_Cafe_Logo.svg.png&q=0&b=1&p=0&a=1").image
              )
          ),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              bar.name.toUpperCase(),
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            Text(
              "Bar category" + " - " + "Bar address",
              style: TextStyle(
                  fontWeight: FontWeight.bold
              ),
            ),
            Text(
              "Opening hours" + " - " + "01.12.12.12.12",
              style: TextStyle(
                  fontWeight: FontWeight.bold
              ),
            ),
            Text(
              "Happy Hour de 20h a 23h",
              style: TextStyle(
                color: Colors.deepOrange,
                fontWeight: FontWeight.bold,
              ),
            )
          ],
        ),
      ],
    ),
    Image.asset("assets/favorites.png"),
    Padding(padding: EdgeInsets.all(20),),
    Center(
      child: Text(
        "Menu".toUpperCase(),
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 25
        ),
      ),
    ),
    Padding(padding: EdgeInsets.all(20)),
    Flexible(
      fit: FlexFit.loose,
      child: CustomScrollView(
        slivers: <Widget>[
          SliverList(
            delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
                return productItem(context, _productsList[index]);
              },
              childCount: _productsList.length,
            ),
          ),
        ],
      ),
    )
  ],
);
}

结果如下:

gif链接:https://ibb.co/x8j6vvm

(如果有人知道如何在问题中集成 gif,请随时编辑它!)

我希望从第二个“TestName1”到心脏图像的数据与页面的其余部分一起完全滚动。我怎样才能做到这一点?

【问题讨论】:

    标签: flutter nestedscrollview flutter-sliver sliverappbar


    【解决方案1】:

    使用CustomScrollView 消除了添加NestedScrollView 的问题。您可以将SilverAppBar 添加到CustomScrollView 以实现此功能。

    CustomScrollView(
     slivers: <Widget>[
      SliverAppBar(
          expandedHeight: 200.0,
          floating: false,
          pinned: true,
          flexibleSpace: FlexibleSpaceBar(
              centerTitle: true,
              title: Text(_bar.name,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16.0,
                  )),
              background: Image.network(
                "https://s1.qwant.com/thumbr/0x380/1/2/2a39f1f558f2cbbec11a08e43493bde861d612add6be643dbc5ad6fe0b16fc/BDBE5B56-B1B4-586D-27C8A70A4A54E50A.jpg?u=https%3A%2F%2Fimages.bwwstatic.com%2Fcolumnpic6%2FBDBE5B56-B1B4-586D-27C8A70A4A54E50A.jpg&q=0&b=1&p=0&a=1",
                fit: BoxFit.cover,
              )),
        ),
      SliverFixedExtentList(
        itemExtent: 150.0,
        delegate: SliverChildListDelegate(
          [
           // Your Can Add you body items here.
          ],
        ),
       ),
     ],
    ),
    

    希望这会有所帮助。

    【讨论】:

    • 它变得更糟了,现在我的产品列表在一个小的高度区域单独滚动。我尝试将它包装在 Expanded 中,或者从其 CustomScrollView 中解开它,但它不起作用。我还从 Column 小部件中解开我的项目,将它们直接放在 SliverChildListDelegate 列表中
    • @Elynad 你能分享一张图片吗?
    • @Elynad 尝试将所有项目(列项目)包装在不同的容器中并减少 itemExtent。
    • 我做到了,当我减少 itemExtent 时,我的 Containers 高度似乎在减少,但是产品列表仍然在一个小的高度区域中
    猜你喜欢
    • 2019-07-16
    • 2021-07-24
    • 2019-07-08
    • 2023-01-28
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多