如何在颤振中显示多个阵列图像?

时间:2021-07-15 16:34:57

标签: flutter

我有一个屏幕,可以在列表构建器中显示数组中的图像。我有以下代码,但我试图根据索引显示图像但没有显示图像。

List<String> imageNames = [
  'a1.jpg',
  'a2.jpg',
  'a3.jpg',
  'a4.jpg',
  'a5.png',
  'a6.jpg',
  'a7.jpg',
  'a8.jpg',
  'a9.jpg',
  'a10.jpg',
  'a11.jpg'
];

return Scaffold(
  body: new ListView.builder(
    itemCount: imageNames.length,
    itemBuilder: (context, index) {
      return new Padding(
        padding: new EdgeInsets.symmetric(vertical: 8.0, horizontal: 10.0),
        child: new Card(
          shape: new RoundedRectangleBorder(
            borderRadius: new BorderRadius.circular(16.0),
          ),
          child: new Column(
            children: <Widget>[
              new Image.asset("assets/images/" + imageNames[index]),
              new Text('a'),
            ],
          ),
        ),
      );
    },
  ),
);

这是 .yaml 文件

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/icons/
    - assets/images/a1.jpg
    - assets/images/a2.jpg
    - assets/images/a3.jpg
    - assets/images/a4.jpg
    - assets/images/a5.png
    - assets/images/a6.jpg
    - assets/images/a7.jpg
    - assets/images/a9.jpg
    - assets/images/a10.jpg
    - assets/images/a11.jpg


  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

如何显示图像?仅文本 a 显示 10 次。 当我调试应用程序时,我被带到 image_provider.dart 中的这段代码。

image_provider.dart

 @protected
  Future<ui.Codec> _loadAsync(AssetBundleImageKey key, DecoderCallback decode) async {
    ByteData? data;
    // Hot reload/restart could change whether an asset bundle or key in a
    // bundle are available, or if it is a network backed bundle.
    try {
      data = await key.bundle.load(key.name);
    } on FlutterError {
      PaintingBinding.instance!.imageCache!.evict(key);
      rethrow;//breakpoint
    }
    if (data == null) {
      PaintingBinding.instance!.imageCache!.evict(key);
      throw StateError('Unable to read data');
    }
    return decode(data.buffer.asUint8List());
  }
}

1 个答案:

答案 0 :(得分:0)

尝试更改为:

  assets:
    - assets/icons/
    - assets/images/

更具体的例子:

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
   assets:
    - assets/icons/
    - assets/images/