|
| 1 | +import 'package:app_flowy/generated/locale_keys.g.dart'; |
| 2 | +import 'package:app_flowy/plugins/grid/application/filter/checkbox_filter_editor_bloc.dart'; |
| 3 | +import 'package:app_flowy/plugins/grid/presentation/widgets/filter/condition_button.dart'; |
| 4 | +import 'package:app_flowy/plugins/grid/presentation/widgets/filter/disclosure_button.dart'; |
1 | 5 | import 'package:app_flowy/plugins/grid/presentation/widgets/filter/filter_info.dart'; |
| 6 | +import 'package:app_flowy/workspace/presentation/widgets/pop_up_action.dart'; |
| 7 | +import 'package:appflowy_popover/appflowy_popover.dart'; |
| 8 | +import 'package:easy_localization/easy_localization.dart'; |
| 9 | +import 'package:flowy_infra/image.dart'; |
| 10 | +import 'package:flowy_infra_ui/flowy_infra_ui.dart'; |
| 11 | +import 'package:flowy_infra_ui/style_widget/text.dart'; |
| 12 | +import 'package:flowy_infra_ui/widget/spacing.dart'; |
| 13 | +import 'package:flowy_sdk/protobuf/flowy-grid/checkbox_filter.pbenum.dart'; |
2 | 14 | import 'package:flutter/material.dart'; |
| 15 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
3 | 16 |
|
4 | 17 | import 'choicechip.dart'; |
5 | 18 |
|
6 | | -class CheckboxFilterChoicechip extends StatelessWidget { |
| 19 | +class CheckboxFilterChoicechip extends StatefulWidget { |
7 | 20 | final FilterInfo filterInfo; |
8 | 21 | const CheckboxFilterChoicechip({required this.filterInfo, Key? key}) |
9 | 22 | : super(key: key); |
10 | 23 |
|
| 24 | + @override |
| 25 | + State<CheckboxFilterChoicechip> createState() => |
| 26 | + _CheckboxFilterChoicechipState(); |
| 27 | +} |
| 28 | + |
| 29 | +class _CheckboxFilterChoicechipState extends State<CheckboxFilterChoicechip> { |
| 30 | + late CheckboxFilterEditorBloc bloc; |
| 31 | + |
| 32 | + @override |
| 33 | + void initState() { |
| 34 | + bloc = CheckboxFilterEditorBloc(filterInfo: widget.filterInfo) |
| 35 | + ..add(const CheckboxFilterEditorEvent.initial()); |
| 36 | + super.initState(); |
| 37 | + } |
| 38 | + |
| 39 | + @override |
| 40 | + void dispose() { |
| 41 | + bloc.close(); |
| 42 | + super.dispose(); |
| 43 | + } |
| 44 | + |
| 45 | + @override |
| 46 | + Widget build(BuildContext context) { |
| 47 | + return BlocProvider.value( |
| 48 | + value: bloc, |
| 49 | + child: BlocBuilder<CheckboxFilterEditorBloc, CheckboxFilterEditorState>( |
| 50 | + builder: (blocContext, state) { |
| 51 | + return AppFlowyPopover( |
| 52 | + controller: PopoverController(), |
| 53 | + constraints: BoxConstraints.loose(const Size(200, 76)), |
| 54 | + direction: PopoverDirection.bottomWithCenterAligned, |
| 55 | + popupBuilder: (BuildContext context) { |
| 56 | + return CheckboxFilterEditor(bloc: bloc); |
| 57 | + }, |
| 58 | + child: ChoiceChipButton( |
| 59 | + filterInfo: widget.filterInfo, |
| 60 | + filterDesc: _makeFilterDesc(state), |
| 61 | + ), |
| 62 | + ); |
| 63 | + }, |
| 64 | + ), |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + String _makeFilterDesc(CheckboxFilterEditorState state) { |
| 69 | + final prefix = LocaleKeys.grid_checkboxFilter_choicechipPrefix_is.tr(); |
| 70 | + return "$prefix ${state.filter.condition.filterName}"; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +class CheckboxFilterEditor extends StatefulWidget { |
| 75 | + final CheckboxFilterEditorBloc bloc; |
| 76 | + const CheckboxFilterEditor({required this.bloc, Key? key}) : super(key: key); |
| 77 | + |
| 78 | + @override |
| 79 | + State<CheckboxFilterEditor> createState() => _CheckboxFilterEditorState(); |
| 80 | +} |
| 81 | + |
| 82 | +class _CheckboxFilterEditorState extends State<CheckboxFilterEditor> { |
| 83 | + final popoverMutex = PopoverMutex(); |
| 84 | + |
11 | 85 | @override |
12 | 86 | Widget build(BuildContext context) { |
13 | | - return ChoiceChipButton(filterInfo: filterInfo); |
| 87 | + return BlocProvider.value( |
| 88 | + value: widget.bloc, |
| 89 | + child: BlocBuilder<CheckboxFilterEditorBloc, CheckboxFilterEditorState>( |
| 90 | + builder: (context, state) { |
| 91 | + final List<Widget> children = [ |
| 92 | + _buildFilterPannel(context, state), |
| 93 | + ]; |
| 94 | + |
| 95 | + return Padding( |
| 96 | + padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 1), |
| 97 | + child: IntrinsicHeight(child: Column(children: children)), |
| 98 | + ); |
| 99 | + }, |
| 100 | + ), |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + Widget _buildFilterPannel( |
| 105 | + BuildContext context, CheckboxFilterEditorState state) { |
| 106 | + return SizedBox( |
| 107 | + height: 20, |
| 108 | + child: Row( |
| 109 | + children: [ |
| 110 | + FlowyText(state.filterInfo.field.name), |
| 111 | + const HSpace(4), |
| 112 | + CheckboxFilterConditionList( |
| 113 | + filterInfo: state.filterInfo, |
| 114 | + popoverMutex: popoverMutex, |
| 115 | + onCondition: (condition) { |
| 116 | + context |
| 117 | + .read<CheckboxFilterEditorBloc>() |
| 118 | + .add(CheckboxFilterEditorEvent.updateCondition(condition)); |
| 119 | + }, |
| 120 | + ), |
| 121 | + const Spacer(), |
| 122 | + DisclosureButton( |
| 123 | + popoverMutex: popoverMutex, |
| 124 | + onAction: (action) { |
| 125 | + switch (action) { |
| 126 | + case FilterDisclosureAction.delete: |
| 127 | + context |
| 128 | + .read<CheckboxFilterEditorBloc>() |
| 129 | + .add(const CheckboxFilterEditorEvent.delete()); |
| 130 | + break; |
| 131 | + } |
| 132 | + }, |
| 133 | + ), |
| 134 | + ], |
| 135 | + ), |
| 136 | + ); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +class CheckboxFilterConditionList extends StatelessWidget { |
| 141 | + final FilterInfo filterInfo; |
| 142 | + final PopoverMutex popoverMutex; |
| 143 | + final Function(CheckboxFilterCondition) onCondition; |
| 144 | + const CheckboxFilterConditionList({ |
| 145 | + required this.filterInfo, |
| 146 | + required this.popoverMutex, |
| 147 | + required this.onCondition, |
| 148 | + Key? key, |
| 149 | + }) : super(key: key); |
| 150 | + |
| 151 | + @override |
| 152 | + Widget build(BuildContext context) { |
| 153 | + final checkboxFilter = filterInfo.checkboxFilter()!; |
| 154 | + return PopoverActionList<ConditionWrapper>( |
| 155 | + asBarrier: true, |
| 156 | + mutex: popoverMutex, |
| 157 | + direction: PopoverDirection.bottomWithCenterAligned, |
| 158 | + actions: CheckboxFilterCondition.values |
| 159 | + .map( |
| 160 | + (action) => ConditionWrapper( |
| 161 | + action, |
| 162 | + checkboxFilter.condition == action, |
| 163 | + ), |
| 164 | + ) |
| 165 | + .toList(), |
| 166 | + buildChild: (controller) { |
| 167 | + return ConditionButton( |
| 168 | + conditionName: checkboxFilter.condition.filterName, |
| 169 | + onTap: () => controller.show(), |
| 170 | + ); |
| 171 | + }, |
| 172 | + onSelected: (action, controller) async { |
| 173 | + onCondition(action.inner); |
| 174 | + controller.close(); |
| 175 | + }, |
| 176 | + ); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +class ConditionWrapper extends ActionCell { |
| 181 | + final CheckboxFilterCondition inner; |
| 182 | + final bool isSelected; |
| 183 | + |
| 184 | + ConditionWrapper(this.inner, this.isSelected); |
| 185 | + |
| 186 | + @override |
| 187 | + Widget? rightIcon(Color iconColor) { |
| 188 | + if (isSelected) { |
| 189 | + return svgWidget("grid/checkmark"); |
| 190 | + } else { |
| 191 | + return null; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + @override |
| 196 | + String get name => inner.filterName; |
| 197 | +} |
| 198 | + |
| 199 | +extension TextFilterConditionExtension on CheckboxFilterCondition { |
| 200 | + String get filterName { |
| 201 | + switch (this) { |
| 202 | + case CheckboxFilterCondition.IsChecked: |
| 203 | + return LocaleKeys.grid_checkboxFilter_isChecked.tr(); |
| 204 | + case CheckboxFilterCondition.IsUnChecked: |
| 205 | + return LocaleKeys.grid_checkboxFilter_isUnchecked.tr(); |
| 206 | + default: |
| 207 | + return ""; |
| 208 | + } |
14 | 209 | } |
15 | 210 | } |
0 commit comments