dartterm/lib/assets.dart

33 lines
772 B
Dart
Raw Normal View History

2023-09-06 03:11:15 +00:00
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
class Assets {
final Map<String, ui.Image> _loaded = {};
final Set<String> _waiting = {};
2023-09-06 03:12:33 +00:00
Future<ui.Image> _getImageAsync(String name) async {
2023-09-06 03:11:15 +00:00
final assetImageByteData = await rootBundle.load(name);
final codec =
await ui.instantiateImageCodec(assetImageByteData.buffer.asUint8List());
final image = (await codec.getNextFrame()).image;
_loaded[name] = image;
return image;
}
ui.Image? getImageIfAvailable(String name) {
if (!_waiting.contains(name)) {
_waiting.add(name);
2023-09-06 03:12:33 +00:00
_getImageAsync(name);
2023-09-06 03:11:15 +00:00
}
return _loaded[name];
}
}
final assets = Assets();
ui.Image? getImageIfAvailable(String name) {
return assets.getImageIfAvailable(name);
}