dencend_toast 0.0.1
dencend_toast: ^0.0.1 copied to clipboard
一个跨平台的Flutter Toast插件,支持在Android、iOS、Web、Windows、macOS和Linux平台上显示可自定义的Toast消息。
example/lib/main.dart
import 'package:dencend_toast/dencend_toast.dart';
import 'package:dencend_toast/toast_style.dart';
import 'package:dencend_toast/toast_types.dart';
import 'package:flutter/material.dart';
import 'dart:async';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = 'Unknown platform version';
} catch (e) {
platformVersion = 'Failed to get platform version: $e';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('DencendToast 示例应用'),
backgroundColor: Colors.blue,
),
backgroundColor: Colors.grey[100],
body: SingleChildScrollView(
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'DencendToast 功能演示',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
Text(
'运行平台: $_platformVersion',
style: TextStyle(
fontSize: 16,
color: Colors.grey[600],
),
),
const SizedBox(height: 40),
// 基本Toast演示
_buildSection(
title: '基本Toast演示',
children: [
_buildButton(
text: '显示短时Toast',
onPressed: () => _showToast(Duration(seconds: 1)),
color: Colors.blue,
),
const SizedBox(width: 16),
_buildButton(
text: '显示长时Toast',
onPressed: () => _showToast(Duration(seconds: 5)),
color: Colors.green,
),
],
),
const SizedBox(height: 20),
// 顶部Toast演示
_buildSection(
title: '顶部Toast演示',
children: [
_buildButton(
text: '顶部居中',
onPressed: () => _showPositionedToast(DencendToastGravity.top),
color: Colors.orange,
),
const SizedBox(width: 16),
_buildButton(
text: '左上角',
onPressed: () => _showPositionedToast(DencendToastGravity.topLeft),
color: Colors.blue,
),
const SizedBox(width: 16),
_buildButton(
text: '右上角',
onPressed: () => _showPositionedToast(DencendToastGravity.topRight),
color: Colors.green,
),
],
),
const SizedBox(height: 20),
// 中部Toast演示
_buildSection(
title: '中部Toast演示',
children: [
_buildButton(
text: '屏幕中心',
onPressed: () => _showPositionedToast(DencendToastGravity.center),
color: Colors.purple,
),
const SizedBox(width: 16),
_buildButton(
text: '左侧中央',
onPressed: () => _showPositionedToast(DencendToastGravity.centerLeft),
color: Colors.teal,
),
const SizedBox(width: 16),
_buildButton(
text: '右侧中央',
onPressed: () => _showPositionedToast(DencendToastGravity.centerRight),
color: Colors.indigo,
),
],
),
const SizedBox(height: 20),
// 底部Toast演示
_buildSection(
title: '底部Toast演示',
children: [
_buildButton(
text: '底部居中',
onPressed: () => _showPositionedToast(DencendToastGravity.bottom),
color: Colors.red,
),
const SizedBox(width: 16),
_buildButton(
text: '左下角',
onPressed: () => _showPositionedToast(DencendToastGravity.bottomLeft),
color: Colors.yellow,
),
const SizedBox(width: 16),
_buildButton(
text: '右下角',
onPressed: () => _showPositionedToast(DencendToastGravity.bottomRight),
color: Colors.pink,
),
const SizedBox(width: 16),
_buildButton(
text: '键盘上方',
onPressed: () => _showPositionedToast(DencendToastGravity.snackbar),
color: Colors.brown,
),
],
),
const SizedBox(height: 20),
// 特殊位置演示
_buildSection(
title: '特殊位置演示',
children: [
_buildButton(
text: '无特定位置',
onPressed: () => _showPositionedToast(DencendToastGravity.none),
color: Colors.grey,
),
],
),
const SizedBox(height: 20),
// 自定义样式演示
_buildSection(
title: '自定义样式演示',
children: [
_buildButton(
text: '自定义颜色Toast',
onPressed: _showCustomColorToast,
color: Colors.teal,
),
const SizedBox(width: 16),
_buildButton(
text: '自定义大小Toast',
onPressed: _showCustomSizeToast,
color: Colors.indigo,
),
],
),
const SizedBox(height: 20),
// 带图标演示
_buildSection(
title: '带图标Toast演示',
children: [
_buildButton(
text: '成功Toast',
onPressed: () => _showIconToast('成功操作', 'success'),
color: Colors.green,
),
const SizedBox(width: 16),
_buildButton(
text: '错误Toast',
onPressed: () => _showIconToast('操作失败', 'error'),
color: Colors.red,
),
],
),
const SizedBox(height: 20),
// 自定义图标位置演示
_buildSection(
title: '自定义图标位置演示',
children: [
_buildButton(
text: '左图标',
onPressed: () => _showIconPositionToast(ToastIconPosition.left),
color: Colors.blue,
),
const SizedBox(width: 16),
_buildButton(
text: '右图图标',
onPressed: () => _showIconPositionToast(ToastIconPosition.right),
color: Colors.purple,
),
],
),
const SizedBox(height: 20),
// 自定义图标样式演示
_buildSection(
title: '自定义图标样式演示',
children: [
_buildButton(
text: '大图图标',
onPressed: () => _showIconStyleToast(32.0),
color: Colors.orange,
),
const SizedBox(width: 16),
_buildButton(
text: '彩色图标',
onPressed: () => _showIconColorToast(),
color: Colors.teal,
),
],
),
const SizedBox(height: 20),
// 控制演示
_buildSection(
title: 'Toast控制演示',
children: [
_buildButton(
text: '显示Toast',
onPressed: _showControlledToast,
color: Colors.blue,
),
const SizedBox(width: 16),
_buildButton(
text: '取消Toast',
onPressed: _cancelToast,
color: Colors.grey,
),
],
),
],
),
),
),
),
),
);
}
// 构建一个节标题
Widget _buildSection({required String title, required List<Widget> children}) {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(16.0),
margin: const EdgeInsets.only(bottom: 16.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8.0),
boxShadow: [
BoxShadow(
color: Colors.grey.withAlpha(51), // 0.2 opacity
spreadRadius: 2,
blurRadius: 5,
offset: const Offset(0, 3),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.grey[800],
),
),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: children,
),
],
),
);
}
// 构建一个按钮
Widget _buildButton({
required String text,
required VoidCallback onPressed,
required Color color,
}) {
return Expanded(
child: ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
backgroundColor: color,
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
child: Text(
text,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
),
);
}
// 显示基本Toast
void _showToast(Duration duration) {
// 确定时长文本
final String durationText = duration.inSeconds < 2 ? "短" : "长";
DencendToast.showToast(
msg: '这是一个${durationText}时间的Toast',
toastDuration: duration,
);
}
// 显示带位置的Toast
void _showPositionedToast(DencendToastGravity gravity) {
String positionText = '';
switch (gravity) {
case DencendToastGravity.top:
positionText = '顶部居中';
break;
case DencendToastGravity.center:
positionText = '屏幕中心';
break;
case DencendToastGravity.bottom:
positionText = '底部居中';
break;
case DencendToastGravity.topLeft:
positionText = '左上角';
break;
case DencendToastGravity.topRight:
positionText = '右上角';
break;
case DencendToastGravity.bottomLeft:
positionText = '左下角';
break;
case DencendToastGravity.bottomRight:
positionText = '右下角';
break;
case DencendToastGravity.centerLeft:
positionText = '左侧中央';
break;
case DencendToastGravity.centerRight:
positionText = '右侧中央';
break;
case DencendToastGravity.snackbar:
positionText = '键盘上方';
break;
case DencendToastGravity.none:
positionText = '无特定位置';
break;
default:
positionText = '未知';
}
DencendToast.showToast(
msg: '这是一个显示在$positionText的Toast',
gravity: gravity,
style: ToastStyle(
borderRadius: 12.0,
fontSize: 11.0,
),
);
}
// 显示自定义颜色的Toast
void _showCustomColorToast() {
DencendToast.showToast(
msg: '这是一个自定义颜色的Toast',
style: ToastStyle(
backgroundColor: Colors.teal,
textColor: Colors.white,
),
);
}
// 显示自定义大小的Toast
void _showCustomSizeToast() {
DencendToast.showToast(
msg: '这是一个自定义大小的Toast',
style: ToastStyle(
fontSize: 20.0,
),
);
}
// 显示带图标的Toast
void _showIconToast(String message, String type) {
// 使用Unicode图标
String icon = '📝'; // 默认图标
Color backgroundColor = Colors.blue;
Color textColor = Colors.white;
// 根据类型设置不同的图标和颜色
switch (type) {
case 'success':
icon = '✅';
backgroundColor = Colors.green;
break;
case 'error':
icon = '❌';
backgroundColor = Colors.red;
break;
case 'warning':
icon = '⚠️';
backgroundColor = Colors.orange;
break;
case 'info':
icon = 'ℹ️';
backgroundColor = Colors.blue;
break;
default:
icon = '📝';
backgroundColor = Colors.grey;
}
DencendToast.showToast(
msg: message,
style: ToastStyle(
backgroundColor: backgroundColor,
textColor: textColor,
fontSize: 12,
iconAsset: icon,
iconSize: 24.0,
iconColor: textColor,
iconPosition: ToastIconPosition.left,
iconSpacing: 8.0,
borderRadius: 12.0,
),
);
}
// 显示不同图标位置的Toast
void _showIconPositionToast(ToastIconPosition position) {
String positionText = position == ToastIconPosition.left ? '左侧' : '右侧';
DencendToast.showToast(
msg: '这是一个图标在$positionText的Toast',
style: ToastStyle(
iconAsset: '📍',
iconSize: 24.0,
iconPosition: position,
iconSpacing: 12.0,
backgroundColor: Colors.blue,
fontSize: 12,
textColor: Colors.white,
borderRadius: 12.0,
),
);
}
// 显示不同图标大小的Toast
void _showIconStyleToast(double iconSize) {
DencendToast.showToast(
msg: '这是一个图标大小为${iconSize.toInt()}px的Toast',
style: ToastStyle(
iconAsset: '🔍',
iconSize: iconSize,
iconPosition: ToastIconPosition.left,
backgroundColor: Colors.orange,
textColor: Colors.white,
borderRadius: 12.0,
),
);
}
// 显示彩色图标的Toast
void _showIconColorToast() {
DencendToast.showToast(
msg: '这是一个彩色图标的Toast',
style: ToastStyle(
iconAsset: '🎨',
iconSize: 28.0,
iconColor: Colors.yellow,
iconPosition: ToastIconPosition.left,
iconSpacing: 12.0,
backgroundColor: Colors.teal,
textColor: Colors.white,
borderRadius: 12.0,
),
);
}
// 显示可控Toast
void _showControlledToast() {
DencendToast.showToast(
msg: '这是一个可以手动取消的Toast',
toastDuration: Duration(seconds: 5), // 长时长,对应原来的ToastLength.long
);
}
// 取消Toast
void _cancelToast() {
DencendToast.cancel();
_showMessage('Toast已取消');
}
// 显示消息(使用Flutter的Toast来避免循环)
void _showMessage(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
duration: const Duration(seconds: 1),
),
);
}
}