本文介绍了重复的GlobalKey与颤抖中的自定义支架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试按照以下github问题在Flutter中实现自定义支架:
I'm trying to implement a custom scaffold in flutter as per this 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在屏幕之间导航,则会出现 Duplicate GlobalKey
的错误。
The code works fine by itself. But if i navigate between screens with other GlobalKey the error of Duplicate GlobalKey
comes up.
如何避免这种情况?
推荐答案
尝试以下几项更改
class MyCustomScaffold extends Scaffold {
MyCustomScaffold({
AppBar appBar,
Widget body,
GlobalKey<ScaffoldState> key,
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,
}) : assert(key != null),
super(
key: key,
appBar: endDrawer != null &&
appBar.actions != null &&
appBar.actions.isNotEmpty
? _buildEndDrawerButton(appBar, key)
: 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, GlobalKey<ScaffoldState> _keyScaffold) {
myAppBar.actions.add(IconButton(
icon: Icon(Icons.menu),
onPressed: () => !_keyScaffold.currentState.isEndDrawerOpen
? _keyScaffold.currentState.openEndDrawer()
: null));
return myAppBar;
}
}
并以这种方式使用:
class YourWidget extends StatelessWidget {
GlobalKey<ScaffoldState> _key = GlobalKey();
@override
Widget build(BuildContext context) {
return MyCustomScaffold(
endDrawer: Drawer(),
key: _key,
appBar: AppBar(
...
这篇关于重复的GlobalKey与颤抖中的自定义支架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!