前言
bot_toast是一个能在Flutter中显示出类似Android的Toast效果,因为Flutter没有内置相关功能,所以我们需要使用插件来完成!
开发环境
- AndroidStudio
- Flutter3
- bot_toast 4.0
使用方式
官方Github
https://github.com/MMMzq/bot_toast/#Overview
首先需要在外层(MaterialApp)中添加 BotToastInit() & navigatorObservers 中添加 BotToastNavigatorObservers
示例代码
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'components/Widget.dart';
class MyApp extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp>{
@override
Widget build(BuildContext context) {
final TransitionBuilder botToastBuilder = BotToastInit();
return MaterialApp(
title: 'BotToast Demo',
builder: (context, child) {
child = botToastBuilder(context,child);
return child;
},
navigatorObservers: [BotToastNavigatorObserver()], //2. registered route observer
home: SimpleWidget(PaperlessTableCardState()),
);
}
}
之后就可以使用BotToast中的方法啦
例如ShowText()
...widget
Container(
margin: EdgeInsets.fromLTRB(0, 0, 0, 100),
child: Container(
width: 150,
height: 50,
decoration: BoxDecoration(
color: Color.fromRGBO(43, 135, 222, 1),
borderRadius: BorderRadius.circular(10)
),
child: TextButton(onPressed: () {
setState(() {
MeetingRoomController.state.roomName = selectRoom;
BotToast.showText(text: "提交成功");
}); },child: Text("提交",style: TextStyle(color: Colors.white,fontSize: 20,letterSpacing: 10)),
),
),
),
...
感谢你的观看!