初始化
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hgdj/assets_tool/app_colors.dart';
|
||||
import 'package:hgdj/assets_tool/images.dart';
|
||||
import 'package:hgdj/hj_utils/screen.dart';
|
||||
import 'package:hgdj/tools_base/toast.dart';
|
||||
import 'package:image_pickers/image_pickers.dart';
|
||||
import 'package:mobkit_dashed_border/mobkit_dashed_border.dart';
|
||||
|
||||
/// AI 功能的单图选择器:未选时是虚线占位框,选后是缩略图 + 右上角删除
|
||||
class PicPicker extends StatefulWidget {
|
||||
/// 与 logic 共享同一份本地图片路径,选/删直接写回
|
||||
final List<String> picList;
|
||||
final double? width;
|
||||
final double? height;
|
||||
|
||||
const PicPicker({
|
||||
super.key,
|
||||
required this.picList,
|
||||
this.width,
|
||||
this.height,
|
||||
});
|
||||
|
||||
@override
|
||||
State<PicPicker> createState() => _PicPickerState();
|
||||
}
|
||||
|
||||
class _PicPickerState extends State<PicPicker> {
|
||||
List<String> get picList => widget.picList;
|
||||
|
||||
void _delPic() {
|
||||
if (picList.isNotEmpty) picList.removeAt(0);
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
Future<void> _addPic() async {
|
||||
final paths = await _pickImg();
|
||||
if (paths.isEmpty) {
|
||||
showToast("请选择图片");
|
||||
return;
|
||||
}
|
||||
picList
|
||||
..clear()
|
||||
..addAll(paths);
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
//相册选图,只取 1 张;不做二次压缩,交给插件的 compressSize
|
||||
Future<List<String>> _pickImg() async {
|
||||
final medias = await ImagePickers.pickerPaths(
|
||||
uiConfig: UIConfig(uiThemeColor: AppColors.primaryColor),
|
||||
galleryMode: GalleryMode.image,
|
||||
selectCount: 1,
|
||||
showCamera: true,
|
||||
);
|
||||
final paths =
|
||||
medias.map((e) => e.path ?? "").where((e) => e.isNotEmpty).toList();
|
||||
if (paths.length < medias.length) showToast("添加图片失败");
|
||||
return paths;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SizedBox(
|
||||
width: widget.width,
|
||||
height: widget.height,
|
||||
child: picList.isEmpty ? _addBtn() : _picItem(),
|
||||
);
|
||||
}
|
||||
|
||||
// 已选图:点图预览,点右上角叉删除
|
||||
Widget _picItem() {
|
||||
return GestureDetector(
|
||||
onTap: () => ImagePickers.previewImages(picList, 0),
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Image.file(
|
||||
File(picList.first),
|
||||
width: widget.width,
|
||||
height: widget.height,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: 0,
|
||||
right: 0,
|
||||
child: InkWell(
|
||||
enableFeedback: false,
|
||||
onTap: _delPic,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(6),
|
||||
child: Image.asset("close_grey.png".commonImgPath,
|
||||
width: 14, height: 14),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 未选图:虚线占位框
|
||||
Widget _addBtn() {
|
||||
return GestureDetector(
|
||||
onTap: _addPic,
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0x0DFFFFFF),
|
||||
border: DashedBorder.fromBorderSide(
|
||||
dashLength: 2,
|
||||
side: const BorderSide(color: Color(0xff656565)),
|
||||
),
|
||||
borderRadius: const BorderRadius.all(Radius.circular(3)),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Image.asset('add_grey.png'.communityPath, width: 24),
|
||||
10.sizeBoxH,
|
||||
const Text("添加图片",
|
||||
style: TextStyle(fontSize: 12, color: Color(0xff999999))),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user