我正在尝试按照这个Github issue实现自定义脚手架:https://github.com/flutter/flutter/issues/19606
import 'package:flutter/material.dart';
class MyCustomScaffold extends Scaffold {
static GlobalKey<ScaffoldState> _keyScaffold = GlobalKey();
MyCustomScaffold({
AppBar appBar,
Widget body,
Widget floatingActionButton,
FloatingActionButtonLocation floatingActionButtonLocation,
FloatingActionButtonAnimator floatingActionButtonAnimator,
List<Widget> persistentFooterButtons,
Widget drawer,
Widget endDrawer,
Widget bottomNavigationBar,
Widget bottomSheet,
Color backgroundColor,
bool resizeToAvoidBottomPadding = true,
bool primary = true,
}) : super(
key: _keyScaffold,
appBar: endDrawer != null &&
appBar.actions != null &&
appBar.actions.isNotEmpty
? _buildEndDrawerButton(appBar)
: appBar,
body: body,
floatingActionButton: floatingActionButton,
floatingActionButtonLocation: floatingActionButtonLocation,
floatingActionButtonAnimator: floatingActionButtonAnimator,
persistentFooterButtons: persistentFooterButtons,
drawer: drawer,
endDrawer: endDrawer,
bottomNavigationBar: bottomNavigationBar,
bottomSheet: bottomSheet,
backgroundColor: backgroundColor,
resizeToAvoidBottomPadding: resizeToAvoidBottomPadding,
primary: primary,
);
static AppBar _buildEndDrawerButton(AppBar myAppBar) {
myAppBar.actions.add(IconButton(
icon: Icon(Icons.menu),
onPressed: () => !_keyScaffold.currentState.isEndDrawerOpen
? _keyScaffold.currentState.openEndDrawer()
: null));
return myAppBar;
}
}
这段代码本身没有问题。但是如果我在其他GlobalKey的屏幕之间导航,就会出现“重复的GlobalKey”错误。
我该如何避免这种情况?