颤振中两条不同路线的相同表单小部件

时间:2021-06-10 10:22:13

标签: flutter dart flutter-pub

实际上我创建了一个包含表单小部件的页面 addInventoryPageformkey(GlobalKey<FormState>()) 并且有一些业务要求使用不同路由的相同页面 (routes1:/inventory,routes2:/openingInventory) 带有一些额外的参数。

但是当我从 /inventory 路由切换到 /openingInventory 路由时,我得到的 fromKey 为 null

这里是addInventoryPage的代码

class StateCommonPage extends StatelessWidget {
  InventoryTabController _inventoryTabController = Get.find();
  VariationController _variationController = Get.find();

  CommonWidget _commonWidget = CommonWidget();
  ValidationMethods _validationMethods = ValidationMethods();
  final description = TextEditingController();
  final quantity = TextEditingController();
  GlobalKey<FormState> formKey=GlobalKey<FormState>(debugLabel: 'STATE COMMON');

  final States titleText;
  double width;
  double height;

  StateCommonPage({this.titleText});

  @override
  Widget build(BuildContext context) {
    width = Get.width * 0.95;
    height = Get.height;

    return Scaffold(
      body: Center(
        child: Container(
          height: height * 0.6,
          child: Form(
            key:formKey,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Text(
                  'Fill Details of ${titleText.toString().split('.').last.capitalizeFirst} '
                  'Process',
                  style: Theme.of(context).textTheme.headline2,
                ),
                ..........

用于在路由之间切换 Get.offNamed('inventory');

这是我的自定义抽屉

    class CustomDrawer extends StatelessWidget {
 Widget buildMenuListTile(BuildContext context, Menus menu) {
        return Obx(
          () => menu.menuCode == 'INVENTORY'
              ? custom.ExpansionTile(
                  initiallyExpanded: true,
                  children: buildSubMenuTile(context, menu.menuCode),
                  leading: Icon(
                    Finals.menuIconMap[menu.menuCode],
                    size: 24,
                    color: Theme.of(context).primaryColor,
                  ),
                  title: Text(
                    menu.menuName,
                    style: menu.menuCode == selectedMenuId.value
                        ? Theme.of(context).textTheme.headline4
                        : Theme.of(context).textTheme.headline3,
                  ),
                )
              : ListTile(
                  selectedTileColor: Theme.of(context).accentColor.withOpacity(0.3),
                  leading: Icon(
                    Finals.menuIconMap[menu.menuCode],
                    size: 24,
                    color: Theme.of(context).primaryColor,
                  ),
                  selected: menu.menuCode == selectedMenuId.value,
                  hoverColor: Theme.of(context).primaryColor,
                  title: Text(
                    menu.menuName,
                    style: menu.menuCode == selectedMenuId.value
                        ? Theme.of(context).textTheme.headline4
                        : Theme.of(context).textTheme.headline3,
                  ),
                  onTap: () {
                    Get.offNamed(menu.relativePath);
                  }),
        );
      }

          }

这是我的路线

class Routes {
 
  static const INVENTORY = '/inventory';
 
  static const OPENING_INVENTORY = '/openingInventory';

  Map<String, WidgetBuilder> test;

  static final routes = [
   
    GetPage(
      name: INVENTORY,
      page: () => InventoryBottomTabBar(),
      binding: BindingsBuilder.put(() => InventoryTabController()),
    ),
    GetPage(
      name: OPENING_INVENTORY,
      page: () => InventoryBottomTabBar(),
      binding:
          BindingsBuilder.put(() => InventoryTabController()),
    )
}

调试日志

<块引用>

在完成小部件树时抛出以下断言: 在小部件树中检测到重复的 GlobalKey。

在小部件中多次指定了以下 GlobalKey 树。这将导致部分小部件树被截断 没想到,因为第二次看到一个键,之前的 实例移动到新位置。关键是:

  • [LabeledGlobalKey#0db7f] 这是通过注意到在将具有上述全局键的小部件移出其 前一个父级,前一个父级在此帧期间从未更新, 这意味着它要么根本没有更新,要么在更新之前更新 小部件被移动,在任何一种情况下都意味着它仍然认为它 应该有一个拥有该全局密钥的孩子。这样做的特定父母 由于以下原因强行移除一个或多个孩子后不更新 GlobalKey 重新父级是:
  • ConstrainedBox(BoxConstraints(0.0<=w<=Infinity, h=484.4), renderObject: RenderConstrainedBox#37684 relayoutBoundary=up2) A GlobalKey 一次只能在一个小部件上指定 树。抛出异常时,这是堆栈: #0 BuildOwner.finalizeTree。 (包:flutter/src/widgets/framework.dart:2900:15) #1 BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:2925:8) #2 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:877:19) #3 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:328:5) #4 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)

0 个答案:

没有答案