Flutter 如何在gridview中加载所有五月图像?使用颤振

Flutter 如何在gridview中加载所有五月图像?使用颤振,flutter,dart,Flutter,Dart,如何在Gridview中加载我的资产/衬衫的所有图像?我五月份有10张照片 这是我创建gridview的现有代码 Widget build(BuildContext context) { final colorScheme = Theme.of(context).colorScheme; final textTheme = Theme.of(context).textTheme; return Scaffold( backgroundColor: Colo

如何在Gridview中加载我的资产/衬衫的所有图像?我五月份有10张照片

这是我创建gridview的现有代码

  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;
    final textTheme = Theme.of(context).textTheme;
    return Scaffold(
      backgroundColor: Colors.white,
      body: GridView.count(
        crossAxisCount: 2,
        children: List.generate(1, (index) {
          return new Card(
            elevation: 9.0,
            shape: new RoundedRectangleBorder(
                borderRadius: new BorderRadius.circular(20.0)),
            child: new Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage("assets/shirt/white-shirt1.jpg"),
                  fit: BoxFit.cover,
                ),
              ),

            ),
          );
          
        }),
      ),
    );
  }
公开发行

  assets:
     - assets/shirt/white-shirt1.jpg
     - assets/shirt/yellow-shirt2.jpg
     - assets/shirt/navyblue-shirt3.jpg
     - assets/shirt/blue-shirt4.png
     - assets/shirt/red-shirt5.jpg
     - assets/shirt/brown-shirt6.jpg
     - assets/shirt/maroon-shirt7.jpg
     - assets/shirt/lime-shirt8.jpg
     - assets/shirt/green-shirt9.jpg
     - assets/shirt/gray-shirt10.png

在发布文件中,不需要相同文件夹中的文件名,您可以

 assets:
     - assets/shirt/
创建路径列表:

final paths = [
    'assets/shirt/white-shirt1.jpg',
    'assets/shirt/yellow-shirt2.jpg',
    'assets/shirt/navyblue-shirt3.jpg',
    'assets/shirt/blue-shirt4.png',
    'assets/shirt/red-shirt5.jpg',
    'assets/shirt/brown-shirt6.jpg',
    'assets/shirt/maroon-shirt7.jpg',
    'assets/shirt/lime-shirt8.jpg',
    'assets/shirt/green-shirt9.jpg',
    'assets/shirt/gray-shirt10.png'
];
现在,不用List.generate,只需在列表上使用.map即可

children: paths.map((path) {
    return new Card(
            elevation: 9.0,
            shape: new RoundedRectangleBorder(
                borderRadius: new BorderRadius.circular(20.0)),
            child: new Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(path), // Use the path here.
                  fit: BoxFit.cover,
                ),
              ),

            ),
          );
}).toList(),

以下是完整的代码:

final paths = [
  'assets/shirt/white-shirt1.jpg',
  'assets/shirt/yellow-shirt2.jpg',
  'assets/shirt/navyblue-shirt3.jpg',
  'assets/shirt/blue-shirt4.png',
  'assets/shirt/red-shirt5.jpg',
  'assets/shirt/brown-shirt6.jpg',
  'assets/shirt/maroon-shirt7.jpg',
  'assets/shirt/lime-shirt8.jpg',
  'assets/shirt/green-shirt9.jpg',
  'assets/shirt/gray-shirt10.png'
];

Widget build(BuildContext context) {
  final colorScheme = Theme.of(context).colorScheme;
  final textTheme = Theme.of(context).textTheme;
  return Scaffold(
    backgroundColor: Colors.white,
    body: GridView.count(
      crossAxisCount: 2,
      children: paths.map((path) {
        return new Card(
          elevation: 9.0,
          shape: new RoundedRectangleBorder(
              borderRadius: new BorderRadius.circular(20.0)),
          child: new Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage(path), // Use the path here.
                fit: BoxFit.cover,
              ),
            ),
          ),
        );
      }).toList(),
    ),
  );
}

在您的
children:path.map((path)…
中,我应该将其粘贴到
主体:GridView.count
中吗?只需更改GridView.count的children属性。用此代码替换List.generate:)您的意思是什么<代码>网格视图。计数更改为什么?添加了完整代码。