重复的GlobalKey与颤抖中的自定义支架

时间:2018-12-28 02:19:18

标签: dart flutter

我正在尝试按照以下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的错误。

如何避免这种情况?

1 个答案:

答案 0 :(得分:1)

请尝试以下几处更改,以避免使用静态方法出现问题。

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