|
| 1 | +// Copyright 2019 The FlutterCandies author. All rights reserved. |
| 2 | +// Use of this source code is governed by an Apache license that can be found |
| 3 | +// in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:wechat_camera_picker/wechat_camera_picker.dart'; |
| 7 | + |
| 8 | +import '../extensions/l10n_extensions.dart'; |
| 9 | +import 'asset_widget_builder.dart'; |
| 10 | +import 'preview_asset_widget.dart'; |
| 11 | + |
| 12 | +class SelectedAssetsListView extends StatelessWidget { |
| 13 | + const SelectedAssetsListView({ |
| 14 | + super.key, |
| 15 | + required this.assets, |
| 16 | + required this.isDisplayingDetail, |
| 17 | + required this.onRemoveAsset, |
| 18 | + }); |
| 19 | + |
| 20 | + final List<AssetEntity> assets; |
| 21 | + final ValueNotifier<bool> isDisplayingDetail; |
| 22 | + final void Function(int index) onRemoveAsset; |
| 23 | + |
| 24 | + Widget _selectedAssetWidget(BuildContext context, int index) { |
| 25 | + final AssetEntity asset = assets.elementAt(index); |
| 26 | + return ValueListenableBuilder<bool>( |
| 27 | + valueListenable: isDisplayingDetail, |
| 28 | + builder: (_, bool value, __) => GestureDetector( |
| 29 | + onTap: () async { |
| 30 | + if (value) { |
| 31 | + showDialog( |
| 32 | + context: context, |
| 33 | + builder: (BuildContext context) => GestureDetector( |
| 34 | + onTap: Navigator.of(context).pop, |
| 35 | + child: Center(child: PreviewAssetWidget(asset)), |
| 36 | + ), |
| 37 | + ); |
| 38 | + } |
| 39 | + }, |
| 40 | + child: RepaintBoundary( |
| 41 | + child: ClipRRect( |
| 42 | + borderRadius: BorderRadius.circular(8.0), |
| 43 | + child: AssetWidgetBuilder( |
| 44 | + entity: asset, |
| 45 | + isDisplayingDetail: value, |
| 46 | + ), |
| 47 | + ), |
| 48 | + ), |
| 49 | + ), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + Widget _selectedAssetDeleteButton(BuildContext context, int index) { |
| 54 | + return GestureDetector( |
| 55 | + onTap: () => onRemoveAsset(index), |
| 56 | + child: DecoratedBox( |
| 57 | + decoration: BoxDecoration( |
| 58 | + borderRadius: BorderRadius.circular(4.0), |
| 59 | + color: Theme.of(context).canvasColor.withOpacity(0.5), |
| 60 | + ), |
| 61 | + child: const Icon(Icons.close, size: 18.0), |
| 62 | + ), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + Widget selectedAssetsListView(BuildContext context) { |
| 67 | + return Expanded( |
| 68 | + child: ListView.builder( |
| 69 | + shrinkWrap: true, |
| 70 | + physics: const BouncingScrollPhysics(), |
| 71 | + padding: const EdgeInsets.symmetric(horizontal: 8.0), |
| 72 | + scrollDirection: Axis.horizontal, |
| 73 | + itemCount: assets.length, |
| 74 | + itemBuilder: (BuildContext context, int index) { |
| 75 | + return Padding( |
| 76 | + padding: const EdgeInsets.symmetric( |
| 77 | + horizontal: 8.0, |
| 78 | + vertical: 16.0, |
| 79 | + ), |
| 80 | + child: AspectRatio( |
| 81 | + aspectRatio: 1.0, |
| 82 | + child: Stack( |
| 83 | + children: <Widget>[ |
| 84 | + Positioned.fill(child: _selectedAssetWidget(context, index)), |
| 85 | + ValueListenableBuilder<bool>( |
| 86 | + valueListenable: isDisplayingDetail, |
| 87 | + builder: (_, bool value, __) => AnimatedPositioned( |
| 88 | + duration: kThemeAnimationDuration, |
| 89 | + top: value ? 6.0 : -30.0, |
| 90 | + right: value ? 6.0 : -30.0, |
| 91 | + child: _selectedAssetDeleteButton(context, index), |
| 92 | + ), |
| 93 | + ), |
| 94 | + ], |
| 95 | + ), |
| 96 | + ), |
| 97 | + ); |
| 98 | + }, |
| 99 | + ), |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + @override |
| 104 | + Widget build(BuildContext context) { |
| 105 | + return ValueListenableBuilder<bool>( |
| 106 | + valueListenable: isDisplayingDetail, |
| 107 | + builder: (_, bool value, __) => AnimatedContainer( |
| 108 | + duration: kThemeChangeDuration, |
| 109 | + curve: Curves.easeInOut, |
| 110 | + height: assets.isNotEmpty |
| 111 | + ? value |
| 112 | + ? 120.0 |
| 113 | + : 80.0 |
| 114 | + : 40.0, |
| 115 | + child: Column( |
| 116 | + children: <Widget>[ |
| 117 | + SizedBox( |
| 118 | + height: 20.0, |
| 119 | + child: GestureDetector( |
| 120 | + onTap: () { |
| 121 | + if (assets.isNotEmpty) { |
| 122 | + isDisplayingDetail.value = !isDisplayingDetail.value; |
| 123 | + } |
| 124 | + }, |
| 125 | + child: Row( |
| 126 | + mainAxisSize: MainAxisSize.min, |
| 127 | + children: <Widget>[ |
| 128 | + Text(context.l10n.selectedAssetsText), |
| 129 | + if (assets.isNotEmpty) |
| 130 | + Padding( |
| 131 | + padding: const EdgeInsetsDirectional.only(start: 10.0), |
| 132 | + child: Icon( |
| 133 | + value ? Icons.arrow_downward : Icons.arrow_upward, |
| 134 | + size: 18.0, |
| 135 | + ), |
| 136 | + ), |
| 137 | + ], |
| 138 | + ), |
| 139 | + ), |
| 140 | + ), |
| 141 | + selectedAssetsListView(context), |
| 142 | + ], |
| 143 | + ), |
| 144 | + ), |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
0 commit comments