【问题标题】:Must ListView.Builder widget be placed on a separate page or not是否必须将 ListView.Builder 小部件放置在单独的页面上
【发布时间】:2019-11-29 07:10:45
【问题描述】:

我在使用 ListView.Builder 小部件显示结果时遇到问题。当我将它放在 main.dart 页面上时,无论我做什么,页面都会不断出现渲染错误。我立即将它放在单独的页面中,它正确显示而没有错误。没有listView.builder,页面(main.dart)显示完美,没有任何错误。 我的问题是可以或以其他方式在同一页面上与其他小部件一起显示 ListView.Builder 还是必须在单独的页面上显示? 任何帮助将不胜感激。

body: TabBarView(
            children: <Widget>[
              SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    Container(
                      // child: SingleChildScrollView(
                      // scrollDirection: Axis.vertical,
                      child:Container(
                        child: Column(
                          //crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Container(

                                  child:Expanded(
                                    child: Image.asset("assets/images/1frenchToast.webp"),
                                  ),
                                ),
                              ],
                            ),

                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: <Widget>[
                                Text("Select Cuisine",style: TextStyle(color: Colors.black),),
                                GestureDetector(
                                  child: DropdownButton(
                                    //isDense: true,
                                    style: TextStyle(fontSize: 14,color: Colors.black,),
                                    value: dropDownSelectedItemState,
                                    items: items,
                                    onChanged: (String selectValue){
                                      ClassHub().mySharedPreference("cuisineChoice", "set", selectValue);

                                      ClassHub().getFoodCategory(selectValue).then((onValue){
                                        if(onValue !=null){
                                          setState(() {
                                            foodCategory = onValue;
                                          });
                                        }
                                      });
                                      setState(() {
                                        dropDownSelectedItemState = selectValue;
                                      });
                                    },
                                  ),
                                  onLongPress: (){

                                  },
                                ),
                              ],
                            ),

 // This is the portion causing issues
                            Column(
                              //mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: <Widget>[

                                //myListView(),

                               Container(
                                 child: ListView.builder(scrollDirection: Axis.vertical,
                                  itemCount: foodCategory.length
                                  itemBuilder: (context,index){
                                    return ListTile(
                                      title: Text(foodCategory[index].foodType),
                                    );
                                  },
                                ),
                            ),

                              ]
                            ),
                            Container(
                              //height: 20,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: <Widget>[
                                Container(
                                  margin: EdgeInsets.only(top: 20,),
                                  height: 60,
                                  width: MediaQuery.of(context).size.width-20,
                                  child: ListTile(
                                    leading: CircleAvatar(
                                      minRadius: 10,
                                      maxRadius: 30,
                                          //child:Image.asset("assets/images/Capture.JPG",height: 60,width: 60,),
                                      //borderRadius: BorderRadius.circular(30),
                                      backgroundImage: ExactAssetImage('assets/images/Capture.JPG'),
                                    ),
                                    title: Text("Western Food Recipes"),
                                  ),
                                ),
                              ],
                            ),
                            Container(
                              height: 10,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: <Widget>[
                                Container(
                                  height: 60,
                                  width: MediaQuery.of(context).size.width-20,
                                  child:ListTile(
                                    leading: Image.asset("assets/images/Capture.JPG"),
                                     // borderRadius: BorderRadius.circular(30),
                                   // ),
                                    title: Text("African Food Recipes"),
                                  ),
                                ),

                              ],
                            ),
                            Container(
                              height: 10,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: <Widget>[
                                Container(
                                  height: 60,
                                  width: MediaQuery.of(context).size.width-20,
                                  child:ListTile(
                                    leading: Image.asset("assets/images/Capture.JPG"),
                                    // borderRadius: BorderRadius.circular(30),
                                    // ),
                                    title: Text("African Food Recipes"),
                                  ),
                                ),

                              ],
                            ),
                          ],
                        ),
                      ),

                      // ),
                    ),
                  ],
                ),
              ),

我希望能够在与其他小部件相同的页面上显示 ListView.Builder 小部件,而不会出现任何渲染错误。这可能吗?

【问题讨论】:

  • 它是一个小部件,无需将其移至另一页。

标签: android listview flutter dart


【解决方案1】:

您需要的是 ListView.builder 中的shrinkWrap = true

否则孩子们将不知道在可滚动小部件中占用多少空间。

【讨论】:

  • BouncingScrollPhysics() 不适用于shrinkWrap = true。有什么解决方法吗?
  • @Vidor Vistrom,感谢您的有用回复。 shrinkwrap:是的,使 ListView.builder 正确显示,但请多做一件事。 listview 内容不滚动,但整个页面滚动。页面有 SingleChildScrollView,ListView.builder 有另一个。我怎样才能使 Listview.builder 内容也可以滚动。是否可以在一个页面上放置多个 singlechildscrollview。再次感谢。
  • @defemz 尝试将物理添加到列表视图中作为 AlwaysScrollablePhysics()
  • @VidorVistrom 再次感谢。将物理添加到 ListView 作为 AlwaysScrollablePhysics() 没有任何效果。
  • @defemz 我明白了.. 那是因为您的所有元素都在空间中膨胀并处于适当的高度。那时没有单独滚动它们的感觉。您可以做的是,将您的 listview 包装在一个容器内并给该容器一个固定的高度。休息也是一样的。您将有一个大于容器高度的列表视图,因此将涉及滚动
【解决方案2】:

您必须通过builder_context 并使用这是ListView.Builder 函数,它可能对您有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-19
    • 2012-11-18
    • 1970-01-01
    • 2021-11-02
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    相关资源
    最近更新 更多