如何有条件地为flutter tabBarview传递多个小部件

问题描述 投票:0回答:1

enter image description here我有一个选项卡视图,我想根据用户导航到哪个屏幕将多个小部件传递给它。第一个屏幕仅包含两个孩子,而另一个屏幕包含四个孩子。我有条件地将四个小部件传递给另一个小部件,但它只选择了三个,这意味着我的条件是错误的,并且它也给了我这个错误。控制器的长度属性 (4) 与 TabBarView 的子属性中存在的子项数量 (3) 不匹配。

From the attached image, you will see that I tapped on the Archdiocese text and it's returning me the national screen which supposed not to be so.
I really need help on how to navigate them all sequentially
The Paytype there is just an enum

 Expanded(
                child: Container(
                  clipBehavior: Clip.hardEdge,
                  margin: EdgeInsets.symmetric(
                    horizontal: context.smallPadding,
                  ),
                  decoration: const BoxDecoration(
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(cornerRadius),
                      topRight: Radius.circular(cornerRadius),
                    ),
                    color: white,
                  ),
                  child: DefaultTabController(
                    length: 4,
                    child: Scaffold(
                      extendBodyBehindAppBar: true,
                      resizeToAvoidBottomInset: false,
                      backgroundColor: white,
                      appBar: AppBar(
                        elevation: 0,
                        toolbarHeight: 0,
                        backgroundColor: white,
                        bottom: TabBar(
                          indicatorColor: AppColor.kColor700,
                          labelColor: AppColor.kColor700,
                          isScrollable: true,
                          padding: const EdgeInsets.only(
                            bottom: 10.0,
                          ),
                          labelPadding:
                              const EdgeInsets.symmetric(horizontal: 10),
                          indicatorPadding: EdgeInsets.zero,
                          indicatorSize: TabBarIndicatorSize.label,
                          unselectedLabelColor: Colors.grey,
                          labelStyle: TextStyles.t3.copyWith(
                            fontSize: FontSizes.s14,
                          ),
                          unselectedLabelStyle: TextStyles.t3.copyWith(
                            fontSize: FontSizes.s14,
                          ),
                          tabs: [
                            const Text('All parishes'),
                            if (widget.payType != PayType.paygroup) ...[
                              const Text('Favorite parishes'),
                            ] else ...[
                              const Text('Favorite groups'),
                              const Text('ArchDiocese'),
                              const Text("National")
                            ],
                          ],
                        ),
                      ),
                      body: TabBarView(
                        children: [
                          //All parishes
                          BlocProvider(
                            create: (context) => sl<BeneficiaryBloc>(),
                            child: PageParishes(
                              payType: widget.payType,
                              onPressed: (parish) {
                                if (widget.payType == PayType.payparish) {
                                  //Navigate to a payparish screen
                                  Navigator.push(
                                    context,
                                    CustomRoute(
                                      child: MultiBlocProvider(
                                        providers: [
                                          BlocProvider(
                                            create: (context) =>
                                                sl<BeneficiaryBloc>(),
                                          ),
                                          BlocProvider(
                                            create: (context) => sl<HomeBloc>(),
                                          ),
                                        ],
                                        child: PagePayParish(
                                          parish: parish,
                                        ),
                                      ),
                                    ),
                                  );
                                } else {
                                  //Navigate to a parish groups screen
                                  Navigator.push(
                                    context,
                                    CustomRoute(
                                      child: PageGroups(
                                        parish: parish,
                                      ),
                                    ),
                                  );
                                }
                              },
                            ),
                          ),
                          //Groups
                          widget.payType != PayType.paygroup
                              ? PageRecentParishes(
                                  onPressed: (parish) {
                                    //check if it's parish
                                    if (widget.payType == PayType.payparish) {
                                      //Navigate to a payparish screen
                                      Navigator.push(
                                        context,
                                        CustomRoute(
                                          child: MultiBlocProvider(
                                            providers: [
                                              BlocProvider(
                                                create: (context) =>
                                                    sl<BeneficiaryBloc>(),
                                              ),
                                              BlocProvider(
                                                create: (context) =>
                                                    sl<HomeBloc>(),
                                              ),
                                            ],
                                            child: PagePayParish(
                                              parish: parish,
                                            ),
                                          ),
                                        ),
                                      );
                                    }
                                    else {
                                      //Navigate to a parish groups screen
                                      Navigator.push(
                                        context,
                                        CustomRoute(
                                          child: PageGroups(
                                            parish: parish,
                                          ),
                                        ),
                                      );
                                    }
                                  },
                                )
                              :PageRecentGroups(
                            onPressed: (parishGroup) {
                              //navigate to paygroups
                              Navigator.push(
                                context,
                                CustomRoute(
                                  child: MultiBlocProvider(
                                    providers: [
                                      BlocProvider(
                                        create: (context) =>
                                            sl<BeneficiaryBloc>(),
                                      ),
                                      BlocProvider(
                                        create: (context) =>
                                            sl<HomeBloc>(),
                                      ),
                                    ],
                                    child: PagePayGroup(
                                      parishGroup: parishGroup,
                                    ),
                                  ),
                                ),
                              );
                            },
                          ),
                          widget.payType !=PayType.paygroup?const PageArchDiocese():const PageNationalGroup()
                          //National and Archdiocesan groups

                        ],
                      ),
                    ),
                  ),
                ),
              ),
flutter tabview
1个回答
0
投票

您可以使用一系列选项卡模型来将选项卡元数据保存在一起吗?像这样的东西:

class TabModel {
  const TabModel({
    required this.title,
    required this.child,
  });

  final String title;
  final Widget child;
}
@override
Widget build(BuildContext context) {
  final tabs = [
    TabModel(title:'All parishes', child: AllParishesWidget()),
    if (widget.payType != PayType.paygroup) ...[
      TabModel(title:'Favorite parishes', child FavoriteParishesWidget()),
    ] else ...[
      TabModel(title: 'Favorite groups', child: FavoriteGroupsWidget()),
      TabModel(title: 'ArchDiocese', child: ArchdioceseWidget()),
      TabModel(title: 'National', child: NationalWidget())
    ],
  ];
  
  Expanded(
    child: Container(
      ...
      child: DefaultTabController(
        ...
          tabs: taps.map((tab) => Text(tab.title)).toList(),
        ...
        body: TabBarView(
          children: tabs.map((tab) => tab.child).toList(),
        ),
      ),
    ),
}
© www.soinside.com 2019 - 2024. All rights reserved.