OKToast 是一款 在 flutter 上 使用的 toast 插件,使用簡單, 可定製性強, 純 flutter, 調用不用 context.bash
插件文檔:pub.dev/packages/ok…less
dependencies:
oktoast: ^2.2.0
複製代碼
flutter pub get
複製代碼
import 'package:oktoast/oktoast.dart';
複製代碼
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return OKToast(
dismissOtherOnShow: true,
child: MaterialApp(
title: 'FlutterUI學習',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.red,
),
home: DemoApp(),
),
);
}
}
複製代碼
class MyOkToast extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
RaisedButton(onPressed: (){showToast("hello");},child: Text("showToast")),
RaisedButton(onPressed: (){showToastWidget(CorrectToast());},child: Text("自定義Toast")),
],
),
);
}
}複製代碼
class CorrectToast extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Container(
width: 140,
height: 140,
color: Colors.green,
child: Image.asset("images/right.png"),
);
}
}複製代碼
dismissAllToast();
複製代碼
showToast("hello", dismissOtherToast: true);
複製代碼
OKToast(
dismissOtherOnShow: true,
...
)
複製代碼
var future = showToast("hello");
future.dismiss(); // 隱藏指定的toast
複製代碼
backgroundColor: 背景顏色
duration: 延遲隱藏時間
onDismiss: 隱藏時的回調
position: toast 的位置
radius: 圓角的尺寸
textAlign: 文字在內部的對齊方式
textDirection: ltr 或 rtl
textPadding: 文本距離邊框的 padding
textStyle: 文本的樣式
複製代碼