Flutter消息提示插件toast_msg的使用
Flutter消息提示插件toast_msg的使用
本项目是一个新的Flutter插件项目。该插件包包含针对Android和/或iOS的平台特定实现代码。
开始使用
要开始使用此Flutter插件,首先需要在你的项目中引入toast_msg
包。你可以通过在pubspec.yaml
文件中添加依赖来完成这一步:
dependencies:
toast_msg: ^1.0.0
然后运行flutter pub get
以获取该依赖。
示例代码
以下是一个完整的示例,展示了如何在Flutter应用中使用toast_msg
插件。
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:toast_msg/toast_msg_service.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
[@override](/user/override)
void initState() {
super.initState();
}
final _focusNode = FocusNode();
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: GestureDetector(
onTap: () => _focusNode.unfocus(), // 点击空白处时收起键盘
child: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'),
),
body: ToastMsgMain(
margin: const EdgeInsets.symmetric(horizontal: 16),
// positionValue: 200,
child: Column(
children: [
const Expanded(
child: Center(
child: Text('Hello!'), // 显示 "Hello!" 文字
),
),
TextField(autofocus: true, decoration: const InputDecoration(border: OutlineInputBorder()), focusNode: _focusNode,) // 显示一个TextField
],
)),
floatingActionButton: ElevatedButton(onPressed: (){
ToastMsgStream.instance.showToast(
ToastData(
// title: 'Test Msg',
customTitle: _buildCustomTitle(), // 自定义标题
backgroundColor: Colors.green, // 背景色
titleStyle: const TextStyle(color: Colors.white), // 文字样式
iconWidget: const Icon(Icons.info) // 图标
),
);
}, child: const Icon(Icons.publish)), // 显示一个按钮
),
),
);
}
Widget _buildCustomTitle() {
return Text.rich(
TextSpan(
style: const TextStyle(color: Colors.black),
children: [
const TextSpan(
text: '消息内容',
),
const TextSpan(text: ' '),
TextSpan(
text: '点击这里!',
style: const TextStyle(color: Colors.red),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint('点击这里! 被点击'); // 点击事件处理
},
)
],
),
);
}
}
代码解释
-
导入库:
import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:toast_msg/toast_msg_service.dart';
-
主函数:
void main() { runApp(const MyApp()); }
-
创建应用:
class MyApp extends StatefulWidget { const MyApp({super.key}); [@override](/user/override) State<MyApp> createState() => _MyAppState(); }
-
初始化状态:
class _MyAppState extends State<MyApp> { [@override](/user/override) void initState() { super.initState(); }
-
创建焦点节点:
final _focusNode = FocusNode();
-
构建UI:
[@override](/user/override) Widget build(BuildContext context) { return MaterialApp( home: GestureDetector( onTap: () => _focusNode.unfocus(), // 点击空白处时收起键盘 child: Scaffold( appBar: AppBar( title: const Text('插件示例应用'), ), body: ToastMsgMain( margin: const EdgeInsets.symmetric(horizontal: 16), // positionValue: 200, child: Column( children: [ const Expanded( child: Center( child: Text('Hello!'), // 显示 "Hello!" 文字 ), ), TextField(autofocus: true, decoration: const InputDecoration(border: OutlineInputBorder()), focusNode: _focusNode,) // 显示一个TextField ], )), floatingActionButton: ElevatedButton(onPressed: (){ ToastMsgStream.instance.showToast( ToastData( // title: 'Test Msg', customTitle: _buildCustomTitle(), // 自定义标题 backgroundColor: Colors.green, // 背景色 titleStyle: const TextStyle(color: Colors.white), // 文字样式 iconWidget: const Icon(Icons.info) // 图标 ), ); }, child: const Icon(Icons.publish)), // 显示一个按钮 ), ), ); }
-
自定义标题:
Widget _buildCustomTitle() { return Text.rich( TextSpan( style: const TextStyle(color: Colors.black), children: [ const TextSpan( text: '消息内容', ), const TextSpan(text: ' '), TextSpan( text: '点击这里!', style: const TextStyle(color: Colors.red), recognizer: TapGestureRecognizer() ..onTap = () { debugPrint('点击这里! 被点击'); // 点击事件处理 }, ) ], ), ); }
更多关于Flutter消息提示插件toast_msg的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter消息提示插件toast_msg的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中,toast
是一种常见的消息提示方式,用于向用户显示简短的反馈信息。虽然 Flutter 本身没有内置的 toast
组件,但你可以使用第三方插件来实现这一功能。其中一个常用的插件是 fluttertoast
。
使用 fluttertoast
插件
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 fluttertoast
插件的依赖:
dependencies:
flutter:
sdk: flutter
fluttertoast: ^8.1.1 # 请检查最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入包
在你的 Dart 文件中导入 fluttertoast
包:
import 'package:fluttertoast/fluttertoast.dart';
3. 使用 fluttertoast
你可以使用 Fluttertoast.showToast
方法来显示 toast
消息。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Toast Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Fluttertoast.showToast(
msg: "This is a toast message",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.grey[600],
textColor: Colors.white,
fontSize: 16.0,
);
},
child: Text('Show Toast'),
),
),
),
);
}
}