Flutter Gridview in Column. What's solution..?

后端 未结 5 1295
小鲜肉
小鲜肉 2021-02-02 09:16

I have a problem with gridview and column. In this case, i want put an image in upper of gridview. Please give me a solution..

return new Container(
  child: new         


        
相关标签:
5条回答
  • 2021-02-02 09:47

    The answer mentioned by @RuslanLeshchenko is correct but if it is still not working for you, trying the following properties on GridView

    1. Try changing shrinkwrap to true
    2. Apply physics: BouncingScrollPhysics()
    0 讨论(0)
  • 2021-02-02 09:47

    If Column is the parent of GridView then it will give rendering issue as It happens because Column and GridView both take the entire space of the screen individually which is there default behavior(Default axis of scrolling is Vertical).

    Solution:

    To solve the above problem we have to disable scrolling of GridView, This can be possible by shrinkWrap and physics property

    shrinkWrap:true - With this GridView only occupies the space it needs

    physics: NeverScrollableScrollPhysics() - It disables scrolling functionality of GridView, which means now we have only SingleChildScrollView who provide the scrolling functionality.

    Code:

    SingleChildScrollView
       Column
            GridView(
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    //...
                    )
    
    0 讨论(0)
  • 2021-02-02 09:50

    You just need to put your grid view into Expanded widget, for example:

    body: new Column(
      children: <Widget>[
        new Expanded(
          child: GridView.count(
            // Create a grid with 2 columns. If you change the scrollDirection to
            // horizontal, this would produce 2 rows.
            crossAxisCount: 2,
            // Generate 100 Widgets that display their index in the List
            children: List.generate(10, (index) {
              return _buildCard(index);
            }),
          ),
        ),
        new Text("text")
      ],
    ),
    
    0 讨论(0)
  • 2021-02-02 09:55

    Try changing childAspectRatio property of GridView to 0.8 or lower.

    0 讨论(0)
  • 2021-02-02 09:58

    Reason for the error:

    Column expands to the maximum size in main axis direction (vertical axis), and so does the GridView (scroll direction is vertical by default)

    Solution

    You need to constrain the height of the GridView, so that it expands to fill the remaining space inside Column, there are several ways of solving this issue, use whichever suits you better.


    1. If you want to allow GridView to take up all remaining space inside Column use Flexible.

      Column(
        children: <Widget>[
          Flexible(
            child: GridView(...),
          )
        ],
      )
      

    1. If you want to limit your GridView to certain height, you can use SizedBox.

      Column(
        children: <Widget>[
          SizedBox(
            height: 200, // constrain height
            child: GridView(...),
          )
        ],
      )
      

    1. If your GridView is small, you may try shrinkWrap property on it.

      Column(
        children: <Widget>[
          GridView(
            shrinkWrap: true, // use it
          )
        ],
      )
      
    0 讨论(0)
提交回复
热议问题