简体   繁体   English

ListView.builder 小部件不滚动

[英]ListView.builder widget not scrolling

I have a ListView.builder widget on my page.我的页面上有一个 ListView.builder 小部件。 The problem is the widget is not scrolling when it display its results.问题是小部件在显示其结果时没有滚动。 Please what am i doing wrong?.请问我做错了什么? The following code show what i have tried so far.以下代码显示了我到目前为止所尝试的内容。

I have tried putting only the ListView.builder Widget on a page, the widget scrolls in that case, but once i add another widget the Listview stop scrollng.我曾尝试只将 ListView.builder 小部件放在页面上,在这种情况下小部件会滚动,但是一旦我添加了另一个小部件,Listview 就会停止滚动。

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( @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),

I want to be able to make my ListView.builder scrolls using the code displayed我希望能够使用显示的代码使我的 ListView.builder 滚动

Based on the suggestions of @VidorVistrom in another thread, this is how i solved the problem.根据@VidorVistrom 在另一个线程中的建议,这就是我解决问题的方法。 I wrapped the ListView.builder inside a container widget and simply gave it a height of 200 and removed the SingleChildScrollView around the ListView.builder, and boom that solved the problem.我将 ListView.builder 包裹在一个容器小部件中,并简单地将其高度设置为 200,然后移除了 ListView.builder 周围的 SingleChildScrollView,并解决了问题。 In case this help other people.如果这有助于其他人。

The complete code is as shown below:完整代码如下所示:

@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