英文:
How can I chain two images to load asset synchronized together?
问题
我有两张图片并加载本地资源。
但是 `assets/images/ic_x.png` 将比 `assets/images/ic_$giftCount.png` 加载得更快。
我想让它们同时出现。
如何实现?
英文:
I have two images and load local asset.
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/images/ic_x.png',
height: size,
),
Image.asset(
'assets/images/ic_$giftCount.png',
height: size,
),
],
)
but assets/images/ic_x.png
will load faster than assets/images/ic_$giftCount.png
I want to make them appear at the same time
How to achieve it?
答案1
得分: 1
I think what you're looking for is precacheImage
which also returns a Future
, hence you can await
multiple images at the same time:
首先,在你的 State
类中创建一个变量:
bool _isImagesLoaded = false;
然后,创建一个方法来 await
图片:
Future<void> _loadImages() async {
await Future.wait([
precacheImage(
AssetImage(image1Path),
context,
),
precacheImage(
AssetImage(
image2Path,
),
context,
),
]);
setState(() {
_isImagesLoaded = true;
});
}
在你的 build
方法中,检查图片是否已加载。
以下是一个完整可运行的示例(显然,你需要更改图片路径):
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Images(),
);
}
}
class Images extends StatefulWidget {
const Images({Key? key}) : super(key: key);
@override
State<Images> createState() => _ImagesState();
}
class _ImagesState extends State<Images> {
bool _isImagesLoaded = false;
final image1Path = 'assets/images/image1.jpg';
final image2Path = 'assets/images/image2.jpg';
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _isImagesLoaded ? _buildImages() : _buildLoading(),
),
);
}
Future<void> _loadImages() async {
await Future.wait([
precacheImage(
AssetImage(image1Path),
context,
),
precacheImage(
AssetImage(
image2Path,
),
context,
),
]);
setState(() {
_isImagesLoaded = true;
});
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadImages();
}
Widget _buildImages() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
image1Path,
),
Image.asset(
image2Path,
),
],
);
}
Widget _buildLoading() {
return const CircularProgressIndicator();
}
}
另请参阅
英文:
I think what your looking for is precacheImage
which also returns a Future
, hence you can await
multiple images at the same time:
First, create a variable in your State
class:
bool _isImagesLoaded = false;
then, create a method to await
the images:
Future<void> _loadImages() async {
await Future.wait([
precacheImage(
AssetImage(image1Path),
context,
),
precacheImage(
AssetImage(
image2Path,
),
context,
),
]);
setState(() {
_isImagesLoaded = true;
});
}
and in your build
, check whether the images were loaded or not.
Here is a complete runnable snippet (obviously you'll have to change the image paths):
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Images(),
);
}
}
class Images extends StatefulWidget {
const Images({Key? key}) : super(key: key);
@override
State<Images> createState() => _ImagesState();
}
class _ImagesState extends State<Images> {
bool _isImagesLoaded = false;
final image1Path = 'assets/images/image1.jpg';
final image2Path = 'assets/images/image2.jpg';
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _isImagesLoaded ? _buildImages() : _buildLoading(),
),
);
}
Future<void> _loadImages() async {
await Future.wait([
precacheImage(
AssetImage(image1Path),
context,
),
precacheImage(
AssetImage(
image2Path,
),
context,
),
]);
setState(() {
_isImagesLoaded = true;
});
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadImages();
}
Widget _buildImages() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
image1Path,
),
Image.asset(
image2Path,
),
],
);
}
Widget _buildLoading() {
return const CircularProgressIndicator();
}
}
See also
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论