dartterm/lib/assets.dart

33 lines
772 B
Dart

import 'dart:ui' as ui;
import 'package:flutter/services.dart';
class Assets {
final Map<String, ui.Image> _loaded = {};
final Set<String> _waiting = {};
Future<ui.Image> _getImageAsync(String name) async {
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);
_getImageAsync(name);
}
return _loaded[name];
}
}
final assets = Assets();
ui.Image? getImageIfAvailable(String name) {
return assets.getImageIfAvailable(name);
}