繁体   English   中英

ListView.builder 小部件不滚动

[英]ListView.builder widget not scrolling

我的页面上有一个 ListView.builder 小部件。 问题是小部件在显示其结果时没有滚动。 请问我做错了什么? 以下代码显示了我到目前为止所尝试的内容。

我曾尝试只将 ListView.builder 小部件放在页面上,在这种情况下小部件会滚动,但是一旦我添加了另一个小部件,Listview 就会停止滚动。

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(

          title: Text(widget.title,style: TextStyle(fontSize: 14),),
        ),
        body: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          physics: AlwaysScrollableScrollPhysics(),
          child:Column(
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    child:Expanded(
                      child: Image.asset("assets/images/1frenchToast.webp"),
                    ),
                    // ),
                  ),
                ],
              ),
              SingleChildScrollView(
                child: ListView.builder(
                  //scrollDirection: Axis.vertical,
                  physics: AlwaysScrollableScrollPhysics(),
                  itemCount:  foodCategory != null?foodCategory.length:1,
                  itemBuilder: (context,index){
                    return ListTile(
                      //dense: true,
                      leading: Container(
                        margin: EdgeInsets.only(bottom: 10),
                        child:Image.asset('assets/images/3poached-egg.webp',),
                      ),
                      title: Text(foodCategory != null?foodCategory[index].foodType:1),
                      onTap: (){
                        _navigate(dropDownSelectedItemState, foodCategory[index].foodType);
                      },
                    );
                  },shrinkWrap: true,
                  // physics: AlwaysScrollableScrollPhysics(),

                ),
              ),

            ],
          ),

        ),

        floatingActionButton: FloatingActionButton(
          onPressed: null,
          tooltip: 'Increment',
          child: Icon(Icons.add),

我希望能够使用显示的代码使我的 ListView.builder 滚动

根据@VidorVistrom 在另一个线程中的建议,这就是我解决问题的方法。 我将 ListView.builder 包裹在一个容器小部件中,并简单地将其高度设置为 200,然后移除了 ListView.builder 周围的 SingleChildScrollView,并解决了问题。 如果这有助于其他人。

完整代码如下所示:

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title,style: TextStyle(fontSize: 14),),
        ),
        body: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          physics: AlwaysScrollableScrollPhysics(),
          child:Column(
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    child:Expanded(
                      child: Image.asset("assets/images/1frenchToast.webp"),
                    ),
                    // ),
                  ),
                ],
              ),
              Container(//child:SingleChildScrollView(
                                  height: 200,
                                  margin: EdgeInsets.only(top: 20),
                                  //scrollDirection: Axis.vertical,
                                  // physics: AlwaysScrollableScrollPhysics(),
                                  child: ListView.builder(
                                    scrollDirection: Axis.vertical,
                                    physics: AlwaysScrollableScrollPhysics(),
                                    itemCount:  foodCategory != null?foodCategory.length:1,
                                    itemBuilder: (context,index){
                                      return ListTile(
                                        dense: true,
                                        leading: Container(
                                          margin: EdgeInsets.only(bottom: 10),
                                          child:Image.asset('assets/images/3poached-egg.webp',),
                                        ),
                                        title: Text(foodCategory != null?foodCategory[index].foodType:1),
                                        onTap: (){
                                          _navigate(dropDownSelectedItemState, foodCategory[index].foodType);
                                        },
                                      );
                                    },shrinkWrap: true,
                                    // physics: AlwaysScrollableScrollPhysics(),

                                  ),//)

                                ),

            ],
          ),

        ),

        floatingActionButton: FloatingActionButton(
          onPressed: null,
          tooltip: 'Increment',
          child: Icon(Icons.add),

暂无
暂无

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

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