|
| 1 | +import 'package:cached_network_image/cached_network_image.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:path/path.dart'; |
| 4 | + |
| 5 | +typedef GestureTapCallback = void Function(); |
| 6 | +typedef DismissDirectionCallback = void Function(DismissDirection direction); |
| 7 | + |
| 8 | +class ManageItem extends StatelessWidget { |
| 9 | + final Key key; |
| 10 | + final String url; |
| 11 | + final String name; |
| 12 | + final String info; |
| 13 | + final FileContentType type; |
| 14 | + final GestureTapCallback onTap; |
| 15 | + final DismissDirectionCallback onDismiss; |
| 16 | + final ConfirmDismissCallback confirmDismiss; |
| 17 | + |
| 18 | + ManageItem( |
| 19 | + this.key, |
| 20 | + this.url, |
| 21 | + this.name, |
| 22 | + this.info, |
| 23 | + this.type, { |
| 24 | + this.onTap, |
| 25 | + this.onDismiss, |
| 26 | + this.confirmDismiss, |
| 27 | + }); |
| 28 | + |
| 29 | + @override |
| 30 | + Widget build(BuildContext context) { |
| 31 | + return Dismissible( |
| 32 | + key: key, |
| 33 | + direction: DismissDirection.endToStart, |
| 34 | + child: Stack( |
| 35 | + children: <Widget>[ |
| 36 | + ListTile( |
| 37 | + leading: buildLeading(), |
| 38 | + title: Text( |
| 39 | + this.name, |
| 40 | + maxLines: 1, |
| 41 | + overflow: TextOverflow.ellipsis, |
| 42 | + textWidthBasis: TextWidthBasis.parent, |
| 43 | + style: TextStyle(fontSize: 16, fontWeight: FontWeight.w400), |
| 44 | + ), |
| 45 | + subtitle: Text( |
| 46 | + this.info, |
| 47 | + maxLines: 1, |
| 48 | + style: TextStyle(color: Colors.grey), |
| 49 | + ), |
| 50 | + onTap: onTap, |
| 51 | + ), |
| 52 | + Align( |
| 53 | + alignment: Alignment.bottomCenter, |
| 54 | + child: Divider( |
| 55 | + height: 1, |
| 56 | + color: Theme.of(context).dividerColor, |
| 57 | + indent: 80, |
| 58 | + ), |
| 59 | + ) |
| 60 | + ], |
| 61 | + ), |
| 62 | + onDismissed: onDismiss, |
| 63 | + confirmDismiss: confirmDismiss, |
| 64 | + background: Container( |
| 65 | + color: Colors.red, |
| 66 | + ), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + Widget buildLeading() { |
| 71 | + if (this.type == FileContentType.DIR) { |
| 72 | + return buildCenterIcon(Icon(IconData(0xe63f, fontFamily: 'iconfont'))); |
| 73 | + } |
| 74 | + try { |
| 75 | + String path = Uri.parse(this.url).path; |
| 76 | + String suffix = extension(path).replaceFirst('.', ''); |
| 77 | + var imageSuffixs = ['png', 'bmp', 'jpeg', 'gif', 'jpg']; |
| 78 | + if (imageSuffixs.contains(suffix)) { |
| 79 | + return SizedBox( |
| 80 | + height: 50, |
| 81 | + width: 50, |
| 82 | + child: Card( |
| 83 | + clipBehavior: Clip.antiAlias, |
| 84 | + shape: RoundedRectangleBorder( |
| 85 | + borderRadius: BorderRadiusDirectional.circular(2)), |
| 86 | + child: CachedNetworkImage( |
| 87 | + imageUrl: this.url, |
| 88 | + fit: BoxFit.cover, |
| 89 | + placeholder: (context, url) => Center( |
| 90 | + child: Container( |
| 91 | + width: 10, |
| 92 | + height: 10, |
| 93 | + child: CircularProgressIndicator( |
| 94 | + strokeWidth: 3, |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + errorWidget: (context, url, error) { |
| 99 | + return Container( |
| 100 | + color: Colors.grey, |
| 101 | + child: Center( |
| 102 | + child: Column( |
| 103 | + mainAxisAlignment: MainAxisAlignment.center, |
| 104 | + children: <Widget>[ |
| 105 | + Icon( |
| 106 | + Icons.error, |
| 107 | + size: 12, |
| 108 | + ), |
| 109 | + SizedBox(height: 2), |
| 110 | + Text( |
| 111 | + '加载失败', |
| 112 | + style: TextStyle(fontSize: 8), |
| 113 | + ) |
| 114 | + ], |
| 115 | + ), |
| 116 | + ), |
| 117 | + ); |
| 118 | + }, |
| 119 | + ), |
| 120 | + ), |
| 121 | + ); |
| 122 | + } else { |
| 123 | + return buildCenterIcon(Icon(IconData(0xe654, fontFamily: 'iconfont'))); |
| 124 | + } |
| 125 | + } catch (e) { |
| 126 | + return buildCenterIcon(Icon(IconData(0xe654, fontFamily: 'iconfont'))); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Widget buildCenterIcon(Icon icon) { |
| 131 | + return SizedBox( |
| 132 | + height: 50, |
| 133 | + width: 50, |
| 134 | + child: Center( |
| 135 | + child: icon, |
| 136 | + ), |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +/// 文件类型 |
| 142 | +enum FileContentType { |
| 143 | + /// 文件 |
| 144 | + FILE, |
| 145 | + |
| 146 | + /// 文件夹 |
| 147 | + DIR, |
| 148 | +} |
0 commit comments