【发布时间】:2020-06-04 17:26:08
【问题描述】:
我想创建SnackBar 用于可重用(全局)
我已经创建了,但它只适用于 1 页,我不知道如何创建可重复使用的。
以下代码:
import 'package:flutter/material.dart';
class SnackBarMain extends StatefulWidget {
@override
_SnackBarMainState createState() => _SnackBarMainState();
}
class _SnackBarMainState extends State<SnackBarMain> {
final globalKey = GlobalKey<ScaffoldState>();
String title = 'SnackBar';
@override
Widget build(BuildContext context) {
return Scaffold(
key: globalKey,
resizeToAvoidBottomPadding: false,
appBar: AppBar(
centerTitle: true,
title: Text(title),
),
body: Center(
child: RaisedButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.purple)),
onPressed: () => snackBarMsg(context),
color: Colors.purple,
textColor: Colors.white,
child: Text("SnackBar",
style: TextStyle(fontSize: 18)),
),
),
);
}
snackBarMsg(BuildContext context) {
final sb = SnackBar(
elevation: 0.0,
//behavior: SnackBarBehavior.floating,
content: Text('SnackBar Bottom Message'),
duration: new Duration(seconds: 5000000),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(16.0), topRight: Radius.circular(16.0)),
),
//backgroundColor: Colors.redAccent,
action: SnackBarAction(
textColor: Color(0xFFFAF2FB),
label: 'OK',
onPressed: () {},
),
);
globalKey.currentState.showSnackBar(sb);
}
}
所以任何人都请给我这个例子
【问题讨论】:
-
snackBar 是否会在整个应用程序中显示相同的消息?你希望它可以在全球范围内访问。
-
不,我想在整个应用程序中显示不同的消息,并且我希望全局访问