将 Gridview 添加到列中会使页面为空。扑

Kri*_*fer 4 dart flutter

我正在尝试添加一个具有 2 列和 3 行的 gridview。我的问题是,当我尝试将它添加到我的行时,它会使整个页面消失。这是我的代码

class HomePage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        elevation: 0.5,
        title: Text("Home", style: TextStyle(fontSize: 15, color: Colors.black45)),
        centerTitle: true,
        backgroundColor: Colors.white,
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            new Container(
              margin: const EdgeInsets.all(25),
              child: Text("Shop Category", style: new TextStyle(fontSize: 18, color: Colors.black, fontFamily: 'Raleway'),textAlign: TextAlign.left,),
            ),
            new GridView.count(
              primary: true,
              crossAxisCount: 2,
              children: <Widget>[
                new Image.asset('assets/images/meat.jpg')
              ],
            )
          ],
        )
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

die*_*per 19

只需将该shrinkWrap属性添加到您的GridView

new GridView.count(
        shrinkWrap: true,
Run Code Online (Sandbox Code Playgroud)

  • 因为您使用的是 SingleChildScrollView + Column ,它不知道它必须扩展多少,所以只需设置shrink true 就可以了! (2认同)