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

问题描述

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

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,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,future,child) {
        return FutureBuilder<List<Story>>(
          future: future,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,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 传递到父屏幕?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)