如何使用颤振中的提供程序或值通知程序在 modalbottomsheet 和父屏幕之间传递数据

时间:2021-05-30 10:05:12

标签: flutter provider

我的自定义底页是这样的:

Future<PersistentBottomSheetController?> _init(BuildContext context) async {
    return showModalBottomSheet(
      context: context,
      isDismissible: isDismissable,
      enableDrag: canDragToClose,
      isScrollControlled: isScrollable,
      builder: (_) {
        if (_isDraggble) {
          return DraggableScrollableSheet(
            initialChildSize: initialChildSize,
            maxChildSize: maxChildSize,
            minChildSize: minChildSize,
            builder: (context, scrollController) {
              return body(context, controller: scrollController);
            },
          );
        }
        return WillPopScope(
          onWillPop: () async => canPop,
          child: body(context),
        );
      },
    );
  }

我正在使用以下代码打开工作表:

    class StoryNameAndAccessLevel extends StatelessWidget {
  const StoryNameAndAccessLevel({
    Key? key,
    required this.scaler,
    required this.title,
  }) : super(key: key);

  final HwScaleUtil scaler;
  final String title;

  @override
  Widget build(BuildContext context) {
    ValueNotifier<AccessRole> _selectedItem = ValueNotifier(AccessRole.NoRole);
    return ValueListenableBuilder(
        valueListenable: _selectedItem,
        builder: (context, AccessRole value, child) {
          return Padding(
            padding: scaler.insets.symmetric(vertical: 1),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                HwText(
                  title,
                  style: HwTextStyle.header55,
                ),
                GestureDetector(
                  onTap: ()  {
                     HwBottomModal.draggable(
                      ctx: context,
                      builder: (controller) =>
                          SetCustomAccessLevel(controller, title),
                      initialChildSize: .65,
                    );
                  },
                  child: HwText(
                    value == AccessRole.NoRole
                        ? 'set access level'
                        : returnRole(value),
                    style:
                        HwTextStyle.subTitle.copyWith(color: Color(0xff00a7b4)),
                  ),
                )
              ],
            ),
          );
        });
  }
}

String returnRole(AccessRole role) {
  if (role == AccessRole.Admin) return 'Admin';
  if (role == AccessRole.Manager) return 'Manager';
  if (role == AccessRole.ViewOnly) return 'View only';
  if (role == AccessRole.NoAccess) return 'No access';
  return 'set access level';
}

这个类用于以下屏幕:

    class CustomAccess extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final model = context.watch<StoryState>();
    final scaler = context.scaler;
    return ValueListenableBuilder<Future<List<Story>>?>(
      valueListenable: model.storiesRef,
      builder: (context, future, child) {
        return FutureBuilder<List<Story>>(
          future: future,
          builder: (context, task) {
            if (task.connectionState == ConnectionState.waiting)
              return AppSpinner();
            if (task.hasError) {
              return DashboardErrorState.noTitle(
                subTitle: parseError(
                  task.error,
                  "We could not fetch your stories at this time, due to an error",
                ),
                onRetry: () {
                  model.refreshstories();
                },
              );
            }
            if (!task.hasData || (task.data?.isEmpty ?? true)) return child!;
            return RefreshIndicator(
              child: CustomScrollView(
                shrinkWrap: true,
                slivers: [
                  SliverPadding(
                    padding:
                        scaler.insets.symmetric(horizontal: 0, vertical: 2),
                    sliver: SliverList(
                      delegate: SliverChildBuilderDelegate(
                        (_, index) {
                          return StoryNameAndAccessLevel(
                            scaler: scaler,
                            title:
                                '${task.data![index].name},
                          );
                        },
                        childCount: task.data?.length ?? 0,
                      ),
                    ),
                  ),
                ],
              ),
              onRefresh: () async {
                model.refreshstories();
              },
            );
          },
        );
      },
      child: const StoryEmptyState(),
    );
  }
}

最后我的小部件在底部打开

    class SetCustomAccessLevel extends StatelessWidget {
  final ScrollController controller;
  ValueNotifier<AccessRole> _selectedItem = ValueNotifier(AccessRole.NoRole);
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  String title;
  SetCustomAccessLevel(this.controller, this.title);
  List<CustomRadioTile> _userAccessRole = [
    CustomRadioTile(
      title: 'Admin',
      subtitle:
          'Full access to managing stories.',
      selectedItem: AccessRole.Admin,
    ),
    CustomRadioTile(
      title: 'Manager',
      subtitle:
          'Limited editing access  ',
      selectedItem: AccessRole.Manager,
    ),
    CustomRadioTile(
      title: 'View Only',
      subtitle:
          'Can view but cannot edit  ',
      selectedItem: AccessRole.ViewOnly,
    ),
    CustomRadioTile(
      title: 'No Access',
      selectedItem: AccessRole.NoAccess,
    ),
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        key: _scaffoldKey,
        appBar: HwAppBar(
          title: 'Set Access Level',
          implyLeading: false,
          actions: [
            IconButton(
              icon: SvgPicture.asset(
                HwSvgs.closeIcon,
              ),
              onPressed: () {
                Navigator.pop(context, _selectedItem);
              },
            ),
          ],
        ),
        body: ListView(
          children: [
            Center(
              child: HwText(
                title,
                style: HwTextStyle.black.copyWith(
                    fontSize: 56,
                    color: HwColors.black,
                    fontWeight: FontWeight.bold),
              ),
            ),
            ValueListenableBuilder(
                valueListenable: _selectedItem,
                builder: (context, AccessRole value, child) {
                  return Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Container(
                      child: ListView.builder(
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: _userAccessRole.length,
                        itemBuilder: (context, index) {
                          return Column(
                            children: [
                              HwSizedBox(
                                height: 3,
                              ),
                              InkWell(
                                onTap: () {
                                  _selectedItem.value =
                                      _userAccessRole[index].selectedItem;
                                },
                                child: Container(
                                  decoration: BoxDecoration(),
                                  child: CustomRadioTile(
                                    selectedItem:
                                        _userAccessRole[index].selectedItem,
                                    radio: value ==
                                            _userAccessRole[index].selectedItem
                                        ? SvgPicture.asset(HwSvgs.radioCheck)
                                        : SvgPicture.asset(
                                            HwSvgs.unActiveRadioButton),
                                    title: _userAccessRole[index].title,
                                    subtitle: _userAccessRole[index].subtitle,
                                  ),
                                ),
                              ),
                            ],
                          );
                        },
                      ),
                    ),
                  );
                }),
          ],
        ));
  }
}

所以我想要做的是,当用户点击一个单独的故事时,会出现一个底部表格,用户可以在其中选择管理员、经理、仅查看或无访问权限的选项之一,当他们关闭底部表格时,那个特定的故事应该有一个文本显示它被选中的角色。

现在对于我的代码,当我关闭底部表时,我收到了 'ValueNotifier<AccessRole>' is not a subtype of type 'PersistentBottomSheetController<dynamic>?' of 'result' 的错误。

那么我如何才能有效地将这些数据从 modalbottomsheete 传递到父屏幕?

0 个答案:

没有答案