【问题标题】:ListView.builder inside futurebuilder inside ListviewListView.builder 里面的 futurebuilder 里面 Listview
【发布时间】:2020-11-10 13:54:08
【问题描述】:

我想让我的主页可滚动我的意思是我想滚动正文上的所有内容,所以我创建了一个列表视图,在列表视图中还有其他小部件,在这些小部件下我想展示一个未来的构建器另一个listview.builder,但我不希望它单独滚动我希望它与主屏幕中的其他小部件一起滚动

这是我的主屏幕主体:

 body: SafeArea(
        child: ListView(
          children: <Widget>[
            Search(),
            FeaturedProducts(),
            OnSaleProducts(),
          ],
        ),
      ),

OnSaleProducts() 是其中包含 futurebuilder 的小部件,这是代码

Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height,
        child: FutureBuilder(
            future: getOnSaleProduct(),
            builder: (_, snapshot){
              if (snapshot.connectionState == ConnectionState.waiting) {
                return ListView.builder(
                    itemCount: 4,
                    itemBuilder: (_, index) {
                      return Column(
                      );
                    });
              }else return ListView.builder(
                  scrollDirection: Axis.vertical,
                  itemCount: 9,
                  itemBuilder: (_, index) {
                    return InkWell(child: ProductCard(name: "new", price: 123, picture: '', onSale: true));
                  });
            }));
  }

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    那么你不应该在 OnSalesProduct() 中使用 ListView 而是一个简单的 Column!

    Column(children: List.generate(count, (int index) => widget(index))
    

    【讨论】:

    • 我的产品卡放在哪里
    • 用 InkWell(child: ProductCard(name: "new $index", price: 123, picture: '', onSale: true)) 替换小部件(索引)
    【解决方案2】:

    希望你想知道的:

    1. 在身体上滚动一切
    2. 在可滚动视图中,想要显示一个未来的构建器 另一个 listview.builder
    3. 不希望它单独滚动它与另一个滚动 主屏幕中的小部件

    让我们试试这个代码,希望你能得到你的答案和解决方案:

    SingleChildScrollView(
            child: Column(
              children: <Widget>[
                Search(),
                FeaturedProducts(),
                OnSaleProducts(),
              ],
            ),
          ),
    

    现在对 OnSaleProducts() 小部件执行此操作:

      Widget build(BuildContext context) {
        return Container(
          height: MediaQuery.of(context).size.height, // better for fixed size 
          width: double.infinity,
          child: FutureBuilder<PopularFoodList>(
                  future: getOnSaleProduct(),
                  builder: (BuildContext context, snapshot) {
                    if (snapshot.hasData) {
                      return ListView.builder(
                          scrollDirection: Axis.vertical,
                          itemCount: 9,
                          itemBuilder: (context, index) {
                             var item = snapshot.data[index]; // snapshot.data.anotherProperty[index]; // If your model has anotherProperty
                             return InkWell(child: ProductCard(name: item.name, price: item.price, picture: '', onSale: true));
                          });
                    } else if (snapshot.hasError) {
                      return Center(child: Text(snapshot.error.toString()));
                    }
                    return Center(child: CircularProgressIndicator());
                  })
    
        );
      }
    

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2020-11-12
      • 2015-01-19
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      相关资源
      最近更新 更多