Listview 颤振中的自定义高度栅格视图

Listview 颤振中的自定义高度栅格视图,listview,flutter,gridview,flutter-layout,Listview,Flutter,Gridview,Flutter Layout,我做我的颤振应用程序。如何在颤振中使用自定义高度和宽度进行栅格视图?栅格视图中的项目应为矩形,而不是正方形 您必须与childAspectRatio一起玩。我已经用下面的例子说明了横向和纵向模式。如果你想要一个正方形网格,那就考虑使用1次。 class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageStat

我做我的颤振应用程序。如何在颤振中使用自定义高度和宽度进行栅格视图?栅格视图中的项目应为矩形,而不是正方形


您必须与childAspectRatio一起玩。我已经用下面的例子说明了横向和纵向模式。如果你想要一个正方形网格,那就考虑使用1次。
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<String> cities = ['Kathmandu', 'Baglung', 'Pokhara'];
  var width = 100;
  var height = 200;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        body: Container(
          color: Colors.blueAccent,
          child: GridView.builder(
              itemCount: cities.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: MediaQuery.of(context).orientation ==
                          Orientation.landscape ? 3: 2,
                  crossAxisSpacing: 8,
                  mainAxisSpacing: 8,
                  childAspectRatio: width / height),
              itemBuilder: (context, position) {
                return Container(
                  alignment: Alignment.center,
                  color: Colors.black,
                  child: Text(
                    cities[position],
                    style: TextStyle(color: Colors.white),
                  ),
                );
              }),
        ));
  }
}
类主页扩展StatefulWidget{
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
列出城市=[‘加德满都’、‘巴隆’、‘博卡拉’];
var宽度=100;
var高度=200;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(),
主体:容器(
颜色:Colors.blueAccent,
子项:GridView.builder(
itemCount:cities.length,
gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount:MediaQuery.of(context.orientation)==
方向。景观?3:2,
横轴间距:8,
平均间距:8,
childAspectRatio:宽度/高度),
itemBuilder:(上下文、位置){
返回容器(
对齐:对齐.center,
颜色:颜色,黑色,
子:文本(
城市[位置],
样式:TextStyle(颜色:Colors.white),
),
);
}),
));
}
}

您必须与childAspectRatio一起玩。我已经用下面的例子说明了横向和纵向模式。如果你想要一个正方形网格,那就考虑使用1次。
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<String> cities = ['Kathmandu', 'Baglung', 'Pokhara'];
  var width = 100;
  var height = 200;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        body: Container(
          color: Colors.blueAccent,
          child: GridView.builder(
              itemCount: cities.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: MediaQuery.of(context).orientation ==
                          Orientation.landscape ? 3: 2,
                  crossAxisSpacing: 8,
                  mainAxisSpacing: 8,
                  childAspectRatio: width / height),
              itemBuilder: (context, position) {
                return Container(
                  alignment: Alignment.center,
                  color: Colors.black,
                  child: Text(
                    cities[position],
                    style: TextStyle(color: Colors.white),
                  ),
                );
              }),
        ));
  }
}
类主页扩展StatefulWidget{
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
列出城市=[‘加德满都’、‘巴隆’、‘博卡拉’];
var宽度=100;
var高度=200;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(),
主体:容器(
颜色:Colors.blueAccent,
子项:GridView.builder(
itemCount:cities.length,
gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount:MediaQuery.of(context.orientation)==
方向。景观?3:2,
横轴间距:8,
平均间距:8,
childAspectRatio:宽度/高度),
itemBuilder:(上下文、位置){
返回容器(
对齐:对齐.center,
颜色:颜色,黑色,
子:文本(
城市[位置],
样式:TextStyle(颜色:Colors.white),
),
);
}),
));
}
}
使用插件

代码:

输出:

使用插件

代码:

输出:


添加您尝试过的示例代码,以便我们可以帮助您,或者添加您尝试过的示例代码,以便我们可以帮助您,或者
class ItemDetailsModel{
  String name;
  Color color;
  ItemDetailsModel({this.name, this.color});
}