image detail

This commit is contained in:
quantulr
2023-02-11 22:29:33 +08:00
parent 5f5f2554eb
commit b2e52d0539
6 changed files with 201 additions and 90 deletions

View File

@ -18,6 +18,8 @@ class HomePage extends ConsumerStatefulWidget {
class _HomePageState extends ConsumerState<HomePage> {
int selectedIndex = 0;
bool uploading = false;
double uploadProgress = 0;
final tabList = [
{"path": "/", "title": "相册"},
{"path": "/profile", "title": "用户资料"},
@ -45,48 +47,69 @@ class _HomePageState extends ConsumerState<HomePage> {
),
elevation: 10,
),
floatingActionButton: selectedIndex == 0
? FloatingActionButton(
onPressed: () async {
FilePickerResult? result =
await FilePicker.platform.pickFiles(type: FileType.image);
floatingActionButton:
selectedIndex == 0 && GoRouterState.of(context).fullpath == "/"
? (!uploading
? FloatingActionButton(
onPressed: () async {
FilePickerResult? result = await FilePicker.platform
.pickFiles(type: FileType.image);
if (result != null) {
String? filePath = result.files.first.path;
if (filePath == null) {
return;
}
String? mimeType = lookupMimeType(filePath);
FormData data = FormData.fromMap({
"pic": await MultipartFile.fromFile(filePath,
filename: result.files.first.name,
contentType: MediaType(mimeType!.split("/").first,
mimeType.split("/").last))
});
await dio.post("/image/upload", data: data).then((value) {
ref.read(uniqueIdProvider.notifier).updateId();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Icon(
Icons.check_circle,
color: Colors.white,
),
SizedBox(
width: 10,
),
Text("上传成功")
],
if (result != null) {
String? filePath = result.files.first.path;
if (filePath == null) {
return;
}
String? mimeType = lookupMimeType(filePath);
FormData data = FormData.fromMap({
"pic": await MultipartFile.fromFile(filePath,
filename: result.files.first.name,
contentType: MediaType(
mimeType!.split("/").first,
mimeType.split("/").last))
});
setState(() {
uploading = true;
});
await dio.post("/image/upload", data: data,
onSendProgress: (sended, total) {
print("$sended / $total");
setState(() {
uploadProgress = sended / total;
});
}).then((value) {
setState(() {
uploading = false;
});
ref.read(uniqueIdProvider.notifier).updateId();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Icon(
Icons.check_circle,
color: Colors.white,
),
SizedBox(
width: 10,
),
Text("上传成功")
],
),
backgroundColor: Colors.lightGreen,
));
});
} else {}
},
child: const Icon(Icons.add),
)
: FloatingActionButton(
onPressed: null,
child: CircularProgressIndicator(
value: uploadProgress,
),
backgroundColor: Colors.lightGreen,
));
});
} else {}
},
child: const Icon(Icons.add),
)
: null,
))
: null,
body: Row(
children: [
MediaQuery.of(context).size.width > 640