Flutter动态图片展示插件flutter_gif的使用

Flutter动态图片展示插件flutter_gif的使用

在Flutter中,要实现GIF图片的展示,我们可以使用Image组件,但是它无法对GIF进行操作,例如改变播放速度、控制当前帧等。这些功能可以通过flutter_gif插件来实现,并且还可以帮助我们缓存GIF图片,避免每次加载时重新加载每一帧。

截图

GIF演示

简单使用

添加依赖

首先,在pubspec.yaml文件中添加flutter_gif插件的依赖:

dependencies:
  flutter_gif: any # 或者使用最新版本

示例代码

import 'package:flutter/material.dart';
import 'package:flutter_gif/flutter_gif.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  late FlutterGifController controller;

  @override
  void initState() {
    controller = FlutterGifController(vsync: this);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            GifImage(
              controller: controller,
              image: AssetImage("images/animate.gif"),
            ),
            ElevatedButton(
              onPressed: () {
                controller.repeat(
                  min: 0,
                  max: 53,
                  period: const Duration(milliseconds: 200),
                );
              },
              child: Text('开始播放'),
            ),
            ElevatedButton(
              onPressed: () {
                controller.stop();
              },
              child: Text('暂停播放'),
            ),
            ElevatedButton(
              onPressed: () {
                controller.reset();
              },
              child: Text('重置播放'),
            ),
            Slider(
              value: controller.value,
              min: 0,
              max: 53,
              divisions: 53,
              label: '${controller.value.round()}',
              onChanged: (double value) {
                controller.value = value;
              },
            ),
          ],
        ),
      ),
    );
  }
}

常见操作

以下是一些常用的FlutterGifController操作方法:

// 从第0帧循环到第29帧
controller.repeat(min: 0, max: 29, period: const Duration(milliseconds: 300));

// 跳转到第3帧(索引从0开始)
controller.value = 2;

// 从当前帧过渡到第26帧
controller.animateTo(26);

如果需要预加载GIF图片,可以使用以下方法:

// 预加载图片
fetchGif(AssetImage("images/animate.gif"));

支持插件(可选)

如果你觉得这个插件对你有帮助,可以通过以下方式支持它:

![Buy Me A Coffee](https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=saytoonz&button_colour=5F7FFF&font_colour=ffffff&font_family=Cookie&outline_colour=000000&coffee_colour=FFDD00)

感谢

特别感谢:

许可证

MIT License

Copyright (c) 2019 Jpeng

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

更多关于Flutter动态图片展示插件flutter_gif的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter动态图片展示插件flutter_gif的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个使用 flutter_gif 插件在 Flutter 应用中展示动态图片的示例代码。这个插件允许你轻松地在 Flutter 应用中显示 GIF 图片。

首先,你需要在你的 pubspec.yaml 文件中添加 flutter_gif 依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_gif: ^2.0.0  # 请注意版本号,这里使用的是2.0.0,实际使用时请检查最新版本

然后,运行 flutter pub get 命令来获取依赖。

接下来,在你的 Dart 文件中,你可以使用 GifImage 组件来显示 GIF 图片。以下是一个完整的示例代码:

import 'package:flutter/material.dart';
import 'package:flutter_gif/flutter_gif.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter GIF Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: GifScreen(),
    );
  }
}

class GifScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GIF Image Display'),
      ),
      body: Center(
        child: GifImage(
          image: NetworkImage('https://example.com/path/to/your/gif/image.gif'), // 替换为你的GIF图片URL
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的 Flutter 应用,包含一个显示 GIF 图片的屏幕。GifImage 组件用于加载和显示网络上的 GIF 图片。你可以将 NetworkImage 的 URL 替换为你自己的 GIF 图片 URL。

如果你希望从本地资产中加载 GIF 图片,你可以使用 AssetImage 而不是 NetworkImage。首先,你需要将 GIF 图片添加到你的 Flutter 项目的 assets 文件夹中,并在 pubspec.yaml 文件中声明它:

flutter:
  assets:
    - assets/my_gif.gif  # 假设你的GIF图片放在assets文件夹下

然后,修改 GifScreen 类中的 GifImage 组件以使用本地资产:

child: GifImage(
  image: AssetImage('assets/my_gif.gif'), // 使用本地资产路径
),

这样,你就可以在 Flutter 应用中显示本地或网络上的 GIF 图片了。希望这个示例对你有所帮助!

回到顶部