Flutter显示GIF对话框插件giff_dialog的使用
Flutter显示GIF对话框插件giff_dialog的使用
👏 Giff Dialogs
giff_dialog
是一个美丽且高度可定制的 Flutter 对话框插件,灵感来源于 FancyAlertDialog-Android。
源代码是 100% Dart,所有内容都位于 GitHub 仓库的 /lib 文件夹 中。
💻 安装
在 pubspec.yaml
文件的 dependencies:
部分添加以下行:
dependencies:
giff_dialog: <latest version>
❔ 使用
导入此类
import 'package:giff_dialog/giff_dialog.dart';
网络 GIF 对话框
onPressed: () {
showDialog(
context: context,
builder: (_) => NetworkGiffDialog(
imageUrl: "https://raw.githubusercontent.com/Shashank02051997/FancyGifDialog-Android/master/GIF's/gif14.gif",
title: Text('Granny Eating Chocolate',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.w600)),
description: Text('This is a granny eating chocolate dialog box. This library helps you easily create fancy giff dialog',
textAlign: TextAlign.center,
),
entryAnimation: EntryAnimation.topLeft,
onOkButtonPressed: () {},
)
);
}
Flare GIF 对话框
onPressed: () {
showDialog(
context: context,
builder: (_) => FlareGiffDialog(
flarePath: 'assets/example.flr',
flareAnimation: 'loading',
title: Text('Space Reloading',
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.w600),
),
description: Text('This is a space reloading dialog box. This library helps you easily create fancy flare dialog.',
textAlign: TextAlign.center,
style: TextStyle(),
),
entryAnimation: EntryAnimation.normal,
onOkButtonPressed: () {},
)
);
}
资源 GIF 对话框
onPressed: () {
showDialog(
context: context,
builder: (_) => AssetGiffDialog(
imagePath: 'assets/example.gif',
title: Text('Men Wearing Jackets',
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.w600),
),
description: Text('This is a men wearing jackets dialog box. This library helps you easily create fancy giff dialog.',
textAlign: TextAlign.center,
style: TextStyle(),
),
entryAnimation: EntryAnimation.bottomRight,
onOkButtonPressed: () {},
)
);
}
📃 许可证
Copyright (c) 2021 Muntadher Al-Baghdadi
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.
Getting Started
对于如何开始使用 Flutter,可以查看我们的在线 文档。
对于如何编辑包代码,可以查看 文档。
示例代码
import 'package:flutter/material.dart';
import 'package:giff_dialog/giff_dialog.dart';
void main() => runApp(const MyApp());
const List<Key> keys = [
Key("Network"),
Key("NetworkDialog"),
Key("Flare"),
Key("FlareDialog"),
Key("Asset"),
Key("AssetDialog"),
];
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Giff Dialog Demo',
theme: ThemeData(primarySwatch: Colors.teal, fontFamily: 'Nunito'),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Giff Dialog Example"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
key: keys[0],
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.teal),
foregroundColor: MaterialStateProperty.all<Color>(Colors.teal),
),
child: const Text(
"Network Giff",
style: TextStyle(
color: Colors.white,
),
),
onPressed: () {
showDialog(
context: context,
builder: (_) => NetworkGiffDialog(
key: keys[1],
image: Image.network(
"https://raw.githubusercontent.com/Shashank02051997/FancyGifDialog-Android/master/GIF's/gif14.gif",
fit: BoxFit.cover,
),
entryAnimation: EntryAnimation.topLeft,
title: const Text(
'Granny Eating Chocolate',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w600),
),
description: const Text(
'This is a granny eating chocolate dialog box. This library helps you easily create fancy giff dialog.',
textAlign: TextAlign.center,
),
onOkButtonPressed: () {},
),
);
},
),
ElevatedButton(
key: keys[2],
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.teal),
foregroundColor: MaterialStateProperty.all<Color>(Colors.teal),
),
child: const Text('Flare Giff', style: TextStyle(color: Colors.white)),
onPressed: () {
showDialog(
context: context,
builder: (_) => FlareGiffDialog(
key: keys[3],
flarePath: 'assets/example.flr',
flareAnimation: 'loading',
title: const Text(
'Space Reloading',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w600),
),
entryAnimation: EntryAnimation.normal,
description: const Text(
'This is a space reloading dialog box. This library helps you easily create fancy flare dialog.',
textAlign: TextAlign.center,
style: TextStyle(),
),
onOkButtonPressed: () {},
),
);
},
),
ElevatedButton(
key: keys[4],
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.teal),
foregroundColor: MaterialStateProperty.all<Color>(Colors.teal),
),
child: const Text(
'Asset Giff',
style: TextStyle(
color: Colors.white,
),
),
onPressed: () {
showDialog(
context: context,
builder: (_) => AssetGiffDialog(
key: keys[5],
image: Image.asset(
'assets/example.gif',
fit: BoxFit.cover,
),
title: const Text(
'Men Wearing Jackets',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w600),
),
entryAnimation: EntryAnimation.bottomRight,
description: const Text(
'This is a men wearing jackets dialog box. This library helps you easily create fancy giff dialog.',
textAlign: TextAlign.center,
style: TextStyle(),
),
onOkButtonPressed: () {},
),
);
},
),
],
),
),
);
}
}
以上是一个完整的示例,展示了如何使用 giff_dialog
插件在 Flutter 应用中显示不同类型的 GIF 对话框。希望对你有所帮助!
更多关于Flutter显示GIF对话框插件giff_dialog的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter显示GIF对话框插件giff_dialog的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter中使用giff_dialog
插件来显示GIF对话框的一个示例代码案例。giff_dialog
是一个允许你在Flutter应用中显示带有GIF图像的对话框的插件。
首先,你需要在你的pubspec.yaml
文件中添加giff_dialog
依赖:
dependencies:
flutter:
sdk: flutter
giff_dialog: ^最新版本号 # 请替换为实际的最新版本号
然后运行flutter pub get
来安装依赖。
接下来是一个完整的示例代码,展示了如何使用giff_dialog
插件来显示一个带有GIF图像的对话框:
import 'package:flutter/material.dart';
import 'package:giff_dialog/giff_dialog.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter GIF Dialog Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void _showGifDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return GiffDialog(
title: Text('这是一个GIF对话框'),
description: Text('这个对话框显示了一个GIF图像。'),
gif: NetworkImage('https://example.com/path/to/your/gif/image.gif'), // 替换为实际的GIF URL
onPress: () {
// 用户点击按钮后的处理逻辑
Navigator.of(context).pop();
},
buttonText: '关闭',
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter GIF Dialog Demo'),
),
body: Center(
child: ElevatedButton(
onPressed: _showGifDialog,
child: Text('显示GIF对话框'),
),
),
);
}
}
在这个示例中:
- 我们创建了一个简单的Flutter应用,包含一个主页面
MyHomePage
。 - 在
MyHomePage
中,我们定义了一个_showGifDialog
方法,该方法使用showDialog
函数来显示一个GiffDialog
。 GiffDialog
接收多个参数,包括标题(title
)、描述(description
)、GIF图像(gif
,这里使用了NetworkImage
来加载网络上的GIF)、按钮点击后的处理逻辑(onPress
)和按钮文本(buttonText
)。- 在页面上放置了一个
ElevatedButton
,当用户点击该按钮时,会调用_showGifDialog
方法来显示GIF对话框。
请注意,你需要将https://example.com/path/to/your/gif/image.gif
替换为实际的GIF图像的URL。
这个示例展示了如何使用giff_dialog
插件在Flutter应用中显示一个带有GIF图像的对话框。希望这对你有所帮助!