Flutter GIF图片选择插件modal_gif_picker的使用

Flutter GIF图片选择插件modal_gif_picker的使用

Flutter版本

Flutter版本 3.0.5

描述

此包是Giphy_Picker的更新版本。该插件的想法是创建不同的UI解决方案,以便每个开发人员可以根据项目需求进行自定义或适应。

开始使用

首先,您需要创建自己的(Giphy API Key)[https://developers.giphy.com/]

  1. 使用ModalGifPicker.pickModalSheetGif作为异步函数从modal_bottom_bottom_sheet中挑选gif。
final gif = await ModalGifPicker.pickModalSheetGif(
    apiKey: '[HERE YOUR API KEY]',
    context: context,
    rating: GiphyRating.g,
    sticker: true,
    backDropColor: Colors.black,
    crossAxisCount: 3,
    childAspectRatio: 1.2,
    topDragColor: Colors.white.withOpacity(0.2),
    previewType: GiphyPreviewType.previewWebp,
);
  1. 使用GiphyRenderImage.original渲染挑选到的gif,并传递gif参数。
SafeArea(
    child: Center(
      child: GiphyRenderImage.original(gif: _gif!),
    ),
),

示例演示

Demo GIF视频

示例代码

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Giphy Picker Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  GiphyG? _gif;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_gif?.title ?? 'Giphy Picker Demo'),
      ),
      body: SafeArea(
        child: Center(
          child: _gif == null
              ? const Text('Pick a gif..')
              : GiphyRenderImage.original(gif: _gif!),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.blueAccent,
        child: const Icon(Icons.search),
        onPressed: () async {
          /// 请求您的Giphy API密钥在https://developers.giphy.com/
          final gif = await ModalGifPicker.pickModalSheetGif(
            apiKey: '[HERE YOUR API KEY]',
            context: context,
            rating: GiphyRating.g,
            sticker: true,
            backDropColor: Colors.black,
            crossAxisCount: 3,
            childAspectRatio: 1.2,
            topDragColor: Colors.white.withOpacity(0.2),
            previewType: GiphyPreviewType.previewWebp,
          );

          if (gif != null) {
            setState(() => _gif = gif);
          }
        },
      ),
    );
  }
}

更多关于Flutter GIF图片选择插件modal_gif_picker的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter GIF图片选择插件modal_gif_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter应用中使用modal_gif_picker插件来选择GIF图片的示例代码。这个插件允许用户从设备的相册或网络上选择GIF图片。

首先,确保你已经在pubspec.yaml文件中添加了modal_gif_picker依赖:

dependencies:
  flutter:
    sdk: flutter
  modal_gif_picker: ^x.y.z  # 请替换为最新版本号

然后运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter项目中实现GIF图片选择功能。以下是一个完整的示例:

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

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

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String? _selectedGifPath;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GIF Picker Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            _selectedGifPath == null
                ? Text('No GIF selected.')
                : Image.file(File(_selectedGifPath!)),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () async {
                final result = await ModalGifPicker.pickGif(context);
                if (result != null) {
                  setState(() {
                    _selectedGifPath = result.path;
                  });
                }
              },
              child: Text('Select GIF'),
            ),
          ],
        ),
      ),
    );
  }
}

代码解释:

  1. 依赖引入:在pubspec.yaml文件中添加modal_gif_picker依赖。
  2. 主应用入口MyApp类作为应用的根组件。
  3. 主页面MyHomePage是一个有状态的组件,用于显示选择的GIF图片。
  4. GIF图片选择:使用ModalGifPicker.pickGif(context)方法打开GIF选择器。这个方法返回一个GifResult对象,其中包含GIF图片的路径。
  5. 显示GIF图片:如果选择了GIF图片,使用Image.file(File(_selectedGifPath!))来显示它。

注意:

  • 确保你已经授予应用访问存储的权限(特别是Android平台)。
  • 在实际项目中,你可能需要处理更多的错误情况,比如用户取消选择等。

这个示例展示了如何使用modal_gif_picker插件来选择并显示GIF图片。根据你的需求,你可以进一步自定义和扩展这个示例。

回到顶部