如何解决每个屏幕均带有FloatingActionButton的BottomNavigationBar
我有一条带有BottomNavigationBar
的路由,其中只有某些屏幕需要具有FloatingActionButton
。一个按钮可以是完整按钮,也可以是小型按钮,并附加有自定义操作(通常会打开一个弹出对话框)。
class TabScreen extends StatefulWidget {
TabScreen ({Key key}) : super(key: key);
@override
_TabScreenState createState() => _TabScreenState();
}
class _TabScreenState extends State<TabScreen> {
int _index = 0;
final _childScreens = <ChildScreen>[
ChildScreen1(),ChildScreen2(),ChildScreen3(),ChildScreen4()
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Tabs'),),bottomNavigationBar: BottomNavigationBarPart(
index: _index,onIndexChanged: (index) => setState(() {
this._index = index;
})),body: IndexedStack(
index: _index,children: _childScreens,floatingActionButton: Visibility(
child: FloatingActionButton(
child: Icon(_childScreens[_index].getFabIcon()),onPressed: () => _childScreens[_index].onFabPressed(),mini: _childScreens[_index].isFabMini()),visible: _childScreens[_index].hasFab()));
}
}
子屏幕的定义如下:
abstract class ChildScreen extends StatefulWidget {
@override
State<StatefulWidget> createState();
bool hasFab() {
return false;
}
IconData getFabIcon() {
return null;
}
bool isFabMini() {
return false;
}
void onFabPressed() {}
}
abstract class ChildScreenState<T extends StatefulWidget> extends State<T> {
@override
Widget build(BuildContext context);
}
现在问题出在该onFabPressed
方法上。它是在无状态小部件上的小部件上调用的,所以我不知道如何打开State
随播对象内部的对话框来访问上下文。
class ChildScreen1 extends ChildScreen {
@override
_ChildScreen1State createState() => _ChildScreen1State();
@override
bool hasFab() {
return true;
}
@override
IconData getFabIcon() {
return Icons.edit;
}
@override
bool isFabMini() {
return true;
}
@override
void onFabPressed() {
// How to call the showMyDialog() method?
}
}
class _ChildScreen1State extends ChildScreenState {
@override
Widget build(BuildContext context) {
// Omitted for brevity
}
Future showMyDialog() async {
return showDialog(
context: context,builder: (BuildContext context) {
return AlertDialog(
title: Text('Dialog'),content: Text('A dialog'),actions: <Widget>[
FlatButton(
child: Text(S.of(context).cancel.toUpperCase()),onPressed: () {
Navigator.of(context).pop();
},FlatButton(
child: Text(S.of(context).ok.toUpperCase()),],);
},);
}
}
我做错什么了吗?抱歉,但是我还是Flutter的新手,并试图掌握主要概念...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。