首页 > 解决方案 > 调整窗口大小时抽屉未正确关闭

问题描述

我有一个响应屏幕尺寸的网络应用程序。当屏幕类型是移动设备时,它会显示一个抽屉,而当屏幕类型是桌面设备时,它会显示一个通常的应用栏操作。当我缩小浏览器窗口以模仿移动屏幕类型时,就会出现问题。然后我打开抽屉。在不关闭抽屉的情况下,我将窗口最大化。但是会发生错误,并且应用栏上会出现一个后退按钮。如果我在最大化之前关闭我的抽屉,它工作正常。


以下是步骤。

这是主页。 主页

这是抽屉
抽屉

单击抽屉按钮然后调整窗口(浏览器)的大小后。
返回键

后退按钮突然出现,这是我得到的错误代码。


======== Exception caught by widgets library =======================================================
The following assertion was thrown while finalizing the widget tree:
setState() or markNeedsBuild() called when widget tree was locked.

This Scaffold widget cannot be marked as needing to build because the framework is locked.
The widget on which setState() or markNeedsBuild() was called was: Scaffold
  dependencies: [_ScaffoldMessengerScope, MediaQuery, _EffectiveTickerMode, UnmanagedRestorationScope, _InheritedTheme, _LocalizationsScope-[GlobalKey#69f10], Directionality]
  state: ScaffoldState#5c645(tickers: tracking 2 tickers)
When the exception was thrown, this was the stack: 
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49  throw_
packages/flutter/src/widgets/framework.dart 4222:9                                                                         <fn>
packages/flutter/src/widgets/framework.dart 4231:14                                                                        markNeedsBuild
packages/flutter/src/widgets/framework.dart 1108:5                                                                         setState
packages/flutter/src/material/scaffold.dart 2142:5                                                                         [_drawerOpenedCallback]
...
====================================================================================================

======== Exception caught by animation library =====================================================
The following assertion was thrown while notifying listeners for AnimationController:
Assertion failed: file:///C:/Flutter/packages/flutter/lib/src/widgets/framework.dart:4188:12
_lifecycleState != _ElementLifecycle.defunct
is not true

When the exception was thrown, this was the stack: 
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49  throw_
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 29:3    assertFailed
packages/flutter/src/widgets/framework.dart 4188:49                                                                        markNeedsBuild
packages/flutter/src/widgets/framework.dart 1108:5                                                                         setState
packages/flutter/src/material/drawer.dart 356:5                                                                            [_animationChanged]
...
The AnimationController notifying listeners was: AnimationController#c00ed(⏮ 0.000; paused)
====================================================================================================

这是我的抽屉代码。

Widget build(BuildContext context) {
  return Scaffold(
    extendBodyBehindAppBar: true,
    drawer: _getDrawer(context),
    appBar: appBar(context),
    body: SafeArea(
      child: ListView(
        children: [
          NameTextWidgets(),
        ],
      ),
    ),
    bottomNavigationBar: bottomAppBar(context),
  );
}
//...
static Widget? _getDrawer(BuildContext context) {
  return getDeviceType(context.mediaQuerySize) == DeviceScreenType.desktop
      ? null
      : appBarActionDrawer();
}
//...
Drawer appBarActionDrawer() {
  return Drawer(
    child: ListView(
      children: <Widget>[
            DrawerHeader(
              child: signatureText(),
            ),
          ] +
          mobileNavigationButtons(),
    ),
  );
}

这是我的应用条码

AppBar appBar(BuildContext context) {
  AppBar ret;
  var deviceType = getDeviceType(context.mediaQuerySize);
  switch (deviceType) {
    case DeviceScreenType.desktop:
      ret = appBarDesktop();
      break;
    default:
      ret = appBarMobile();
  }
  return ret;
}
//...
AppBar appBarDesktop() {
  return AppBar(
    elevation: 0.0,
    title: signatureText(),
    actions: desktopNavigationButtons()
      ..add(
        themeSwitcher(),
      ),
  );
}

AppBar appBarMobile() {
  return AppBar(
    elevation: 0.0,
    actions: [
      signatureText(),
    ],
  );
}

如何删除该后退按钮并避免这些错误?

标签: flutterdart

解决方案


推荐阅读