简体   繁体   English

ListView.builder 里面的 futurebuilder 里面 Listview

[英]ListView.builder inside futurebuilder inside Listview

I wanted to to make my home page scrollable i mean i want to scroll everthing on the body so i made a list view and inside the list view there are other widgets and under those widgets i want to show a future builder that has another listview.builder in it but i dont want it to be scrolled alone i want it to be scrolled with the other widgets in the home screen我想让我的主页可滚动我的意思是我想滚动正文上的所有内容,所以我创建了一个列表视图,在列表视图中还有其他小部件,在这些小部件下,我想展示一个具有另一个列表视图的未来构建器。构建器在其中,但我不希望它单独滚动我希望它与主屏幕中的其他小部件一起滚动

this is my home screen body:这是我的主屏幕主体:

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

OnSaleProducts() is the widget that has a futurebuilder in it this is the code 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));
                  });
            }));
  }

Then you should not use a ListView inside OnSalesProduct() but a simple Column!那么你不应该在 OnSalesProduct() 中使用 ListView,而应该使用一个简单的 Column!

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

Hopefully, what you are trying to know:希望您想知道的是:

  1. Scroll everthing on the body滚动身体上的一切
  2. Inside the scrollable view, want to show a future builder that has another listview.builder在可滚动视图中,想要显示具有另一个 listview.builder 的未来构建器
  3. Don't want it to be scrolled alone it to be scrolled with the other widgets in the home screen不希望它单独滚动它与主屏幕中的其他小部件一起滚动

Let's try this code, hopefully you will get your answerers and solution together:让我们试试这段代码,希望你能把你的回答者和解决方案放在一起:

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

Now do this for OnSaleProducts() widget:现在为 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());
              })

    );
  }

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

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