flutter how to give height to the childrens of GridView.Builder

前端 未结 1 1435
时光说笑
时光说笑 2021-01-01 10:47

I am tring to give height to the childrens of gridview.builder but it\'s not accepting. I tried by using container but its not working... please help

         


        
相关标签:
1条回答
  • 2021-01-01 11:08

    You want to use the childAspectRatio property of the SliverGridDelegate preferably with MediaQuery:

    class HomePage extends StatelessWidget {
      final List<String> items = <String>[
        "Item 1",
        "Item 2",
        "Item 3",
        "Item 4",
        "Item 5",
        "Item 6",
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Home')),
          body: GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              childAspectRatio: MediaQuery.of(context).size.width /
                  (MediaQuery.of(context).size.height / 4),
            ),
            itemCount: items.length,
            itemBuilder: (context, index) {
              return GridTile(child: Text(items[index]));
            },
          ),
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题