如何将两个图像链接在一起以同步加载资源?

huangapple go评论75阅读模式
英文:

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&lt;void&gt; _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 &#39;package:flutter/material.dart&#39;;

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&lt;Images&gt; createState() =&gt; _ImagesState();
}

class _ImagesState extends State&lt;Images&gt; {
  bool _isImagesLoaded = false;
  final image1Path = &#39;assets/images/image1.jpg&#39;;
  final image2Path = &#39;assets/images/image2.jpg&#39;;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: _isImagesLoaded ? _buildImages() : _buildLoading(),
      ),
    );
  }

  Future&lt;void&gt; _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

huangapple
  • 本文由 发表于 2023年6月13日 11:10:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定