Same Form widget for two different routes in flutter

263 views Asked by At

Actually I had created a page that contain form widget addInventoryPage with formkey(GlobalKey<FormState>()) and there is some business requirement to use the same page with different routes (routes1:/inventory,routes2:/openingInventory) with some additional params.

But when I switch from /inventory routes to /openingInventory routes I got fromKey as null

Here is the code of 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,
                ),
                ..........

for switching between routes Get.offNamed('inventory');

Here is my customDrawer

    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);
                  }),
        );
      }

          }

Here is my Routes

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()),
    )
}

debug Logs

The following assertion was thrown while finalizing the widget tree: Duplicate GlobalKey detected in widget tree.

The following GlobalKey was specified multiple times in the widget tree. This will lead to parts of the widget tree being truncated unexpectedly, because the second time a key is seen, the previous instance is moved to the new location. The key was:

  • [LabeledGlobalKey#0db7f] This was determined by noticing that after the widget with the above global key was moved out of its previous parent, that previous parent never updated during this frame, meaning that it either did not update at all or updated before the widget was moved, in either case implying that it still thinks that it should have a child with that global key. The specific parent that did not update after having one or more children forcibly removed due to GlobalKey reparenting is:
  • ConstrainedBox(BoxConstraints(0.0<=w<=Infinity, h=484.4), renderObject: RenderConstrainedBox#37684 relayoutBoundary=up2) A GlobalKey can only be specified on one widget at a time in the widget tree. When the exception was thrown, this was the stack: #0 BuildOwner.finalizeTree. (package: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

There are 0 answers