|
| 1 | +import 'package:app_flowy/generated/locale_keys.g.dart'; |
| 2 | +import 'package:app_flowy/plugins/trash/src/sizes.dart'; |
| 3 | +import 'package:app_flowy/plugins/trash/src/trash_header.dart'; |
| 4 | +import 'package:app_flowy/startup/startup.dart'; |
| 5 | +import 'package:easy_localization/easy_localization.dart'; |
| 6 | +import 'package:flowy_infra/image.dart'; |
| 7 | +import 'package:flowy_infra/theme.dart'; |
| 8 | +import 'package:flowy_infra_ui/style_widget/button.dart'; |
| 9 | +import 'package:flowy_infra_ui/style_widget/scrolling/styled_list.dart'; |
| 10 | +import 'package:flowy_infra_ui/style_widget/scrolling/styled_scroll_bar.dart'; |
| 11 | +import 'package:flowy_infra_ui/style_widget/scrolling/styled_scrollview.dart'; |
| 12 | +import 'package:flowy_infra_ui/style_widget/text.dart'; |
| 13 | +import 'package:flowy_infra_ui/widget/spacing.dart'; |
| 14 | +import 'package:flutter/material.dart'; |
| 15 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 16 | +import 'package:styled_widget/styled_widget.dart'; |
| 17 | + |
| 18 | +import 'application/trash_bloc.dart'; |
| 19 | +import 'src/trash_cell.dart'; |
| 20 | + |
| 21 | +class TrashPage extends StatefulWidget { |
| 22 | + const TrashPage({Key? key}) : super(key: key); |
| 23 | + |
| 24 | + @override |
| 25 | + State<TrashPage> createState() => _TrashPageState(); |
| 26 | +} |
| 27 | + |
| 28 | +class _TrashPageState extends State<TrashPage> { |
| 29 | + final ScrollController _scrollController = ScrollController(); |
| 30 | + @override |
| 31 | + Widget build(BuildContext context) { |
| 32 | + final theme = context.watch<AppTheme>(); |
| 33 | + const horizontalPadding = 80.0; |
| 34 | + return BlocProvider( |
| 35 | + create: (context) => getIt<TrashBloc>()..add(const TrashEvent.initial()), |
| 36 | + child: BlocBuilder<TrashBloc, TrashState>( |
| 37 | + builder: (context, state) { |
| 38 | + return SizedBox.expand( |
| 39 | + child: Column( |
| 40 | + mainAxisAlignment: MainAxisAlignment.start, |
| 41 | + children: [ |
| 42 | + _renderTopBar(context, theme, state), |
| 43 | + const VSpace(32), |
| 44 | + _renderTrashList(context, state), |
| 45 | + ], |
| 46 | + ).padding(horizontal: horizontalPadding, vertical: 48), |
| 47 | + ); |
| 48 | + }, |
| 49 | + ), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + Widget _renderTrashList(BuildContext context, TrashState state) { |
| 54 | + const barSize = 6.0; |
| 55 | + return Expanded( |
| 56 | + child: ScrollbarListStack( |
| 57 | + axis: Axis.vertical, |
| 58 | + controller: _scrollController, |
| 59 | + scrollbarPadding: EdgeInsets.only(top: TrashSizes.headerHeight), |
| 60 | + barSize: barSize, |
| 61 | + child: StyledSingleChildScrollView( |
| 62 | + controller: ScrollController(), |
| 63 | + barSize: barSize, |
| 64 | + axis: Axis.horizontal, |
| 65 | + child: SizedBox( |
| 66 | + width: TrashSizes.totalWidth, |
| 67 | + child: ScrollConfiguration( |
| 68 | + behavior: const ScrollBehavior().copyWith(scrollbars: false), |
| 69 | + child: CustomScrollView( |
| 70 | + shrinkWrap: true, |
| 71 | + physics: StyledScrollPhysics(), |
| 72 | + controller: _scrollController, |
| 73 | + slivers: [ |
| 74 | + _renderListHeader(context, state), |
| 75 | + _renderListBody(context, state), |
| 76 | + ], |
| 77 | + ), |
| 78 | + ), |
| 79 | + ), |
| 80 | + ), |
| 81 | + ), |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + Widget _renderTopBar(BuildContext context, AppTheme theme, TrashState state) { |
| 86 | + return SizedBox( |
| 87 | + height: 36, |
| 88 | + child: Row( |
| 89 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 90 | + children: [ |
| 91 | + FlowyText.semibold(LocaleKeys.trash_text.tr()), |
| 92 | + const Spacer(), |
| 93 | + IntrinsicWidth( |
| 94 | + child: FlowyButton( |
| 95 | + text: FlowyText.medium(LocaleKeys.trash_restoreAll.tr(), |
| 96 | + fontSize: 12), |
| 97 | + leftIcon: svgWidget('editor/restore', color: theme.iconColor), |
| 98 | + hoverColor: theme.hover, |
| 99 | + onTap: () => context.read<TrashBloc>().add( |
| 100 | + const TrashEvent.restoreAll(), |
| 101 | + ), |
| 102 | + ), |
| 103 | + ), |
| 104 | + const HSpace(6), |
| 105 | + IntrinsicWidth( |
| 106 | + child: FlowyButton( |
| 107 | + text: FlowyText.medium(LocaleKeys.trash_deleteAll.tr(), |
| 108 | + fontSize: 12), |
| 109 | + leftIcon: svgWidget('editor/delete', color: theme.iconColor), |
| 110 | + hoverColor: theme.hover, |
| 111 | + onTap: () => |
| 112 | + context.read<TrashBloc>().add(const TrashEvent.deleteAll()), |
| 113 | + ), |
| 114 | + ) |
| 115 | + ], |
| 116 | + ), |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + Widget _renderListHeader(BuildContext context, TrashState state) { |
| 121 | + return SliverPersistentHeader( |
| 122 | + delegate: TrashHeaderDelegate(), |
| 123 | + floating: true, |
| 124 | + pinned: true, |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + Widget _renderListBody(BuildContext context, TrashState state) { |
| 129 | + return SliverList( |
| 130 | + delegate: SliverChildBuilderDelegate( |
| 131 | + (BuildContext context, int index) { |
| 132 | + final object = state.objects[index]; |
| 133 | + return SizedBox( |
| 134 | + height: 42, |
| 135 | + child: TrashCell( |
| 136 | + object: object, |
| 137 | + onRestore: () { |
| 138 | + context.read<TrashBloc>().add(TrashEvent.putback(object.id)); |
| 139 | + }, |
| 140 | + onDelete: () => |
| 141 | + context.read<TrashBloc>().add(TrashEvent.delete(object)), |
| 142 | + ), |
| 143 | + ); |
| 144 | + }, |
| 145 | + childCount: state.objects.length, |
| 146 | + addAutomaticKeepAlives: false, |
| 147 | + ), |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
0 commit comments