gpt4 book ai didi

dart - 在 flutter 中使用自定义脚手架复制 GlobalKey

转载 作者:IT王子 更新时间:2023-10-29 06:54:13 27 4
gpt4 key购买 nike

我正在尝试根据这个 github 问题在 flutter 中实现自定义脚手架: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 错误。

如何避免这种情况?

最佳答案

尝试这些少量更改以避免使用静态时出现问题。

    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(
...

关于dart - 在 flutter 中使用自定义脚手架复制 GlobalKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53952989/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com