Files
momo/lib/material/home.dart

170 lines
6.5 KiB
Dart
Raw Normal View History

2023-02-08 17:20:16 +08:00
import 'package:dio/dio.dart';
import 'package:file_picker/file_picker.dart';
2023-05-14 19:59:43 +08:00
import 'package:flutter/foundation.dart';
2023-02-06 23:45:56 +08:00
import 'package:flutter/material.dart';
2023-02-08 17:20:16 +08:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-02-07 17:28:01 +08:00
import 'package:go_router/go_router.dart';
2023-02-08 17:20:16 +08:00
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';
2023-02-10 17:33:51 +08:00
import 'package:momo/provider/rerender.dart';
2023-02-08 23:48:25 +08:00
import 'package:momo/request/http_client.dart';
2023-02-06 23:45:56 +08:00
2023-02-08 17:20:16 +08:00
class HomePage extends ConsumerStatefulWidget {
const HomePage({Key? key, required this.content}) : super(key: key);
final Widget content;
2023-02-06 23:45:56 +08:00
@override
2023-02-08 17:20:16 +08:00
ConsumerState<HomePage> createState() => _HomePageState();
2023-02-06 23:45:56 +08:00
}
2023-02-08 17:20:16 +08:00
class _HomePageState extends ConsumerState<HomePage> {
2023-02-06 23:45:56 +08:00
int selectedIndex = 0;
2023-02-11 22:29:33 +08:00
bool uploading = false;
double uploadProgress = 0;
2023-02-08 17:20:16 +08:00
final tabList = [
2023-02-08 23:48:25 +08:00
{"path": "/", "title": "相册"},
2023-02-10 00:07:41 +08:00
{"path": "/profile", "title": "用户资料"},
{"path": "/detail", "title": "图片详情"}
2023-02-08 17:20:16 +08:00
];
2023-02-06 23:45:56 +08:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2023-02-08 23:48:25 +08:00
leading: ["/", "/profile"].contains(GoRouterState.of(context).location)
? null
: IconButton(
icon: const Icon(Icons.keyboard_arrow_left),
onPressed: () {
context.go("/");
},
),
title: Text(
2023-02-10 00:07:41 +08:00
tabList.firstWhere((element) =>
element["path"] ==
GoRouterState.of(context).fullpath)["title"] ??
"",
2023-02-08 23:48:25 +08:00
style: const TextStyle(fontWeight: FontWeight.bold),
2023-02-07 17:28:01 +08:00
),
elevation: 10,
2023-02-06 23:45:56 +08:00
),
2023-05-14 19:59:43 +08:00
floatingActionButton: selectedIndex == 0 &&
GoRouterState.of(context).fullpath == "/"
? (!uploading
? FloatingActionButton(
onPressed: () async {
FilePickerResult? result = await FilePicker.platform
.pickFiles(type: FileType.image);
2023-02-08 17:20:16 +08:00
2023-05-14 19:59:43 +08:00
if (result != null) {
String fileName = result.files.first.name;
String? mimeType = lookupMimeType(fileName);
MultipartFile file;
if (kIsWeb) {
final fileBytes = result.files.first.bytes;
file = MultipartFile.fromBytes(fileBytes as List<int>,
filename: result.files.first.name,
contentType: MediaType(mimeType!.split("/").first,
mimeType.split("/").last));
} else {
String? filePath = result.files.first.path;
if (filePath == null) {
return;
}
file = await MultipartFile.fromFile(filePath,
filename: result.files.first.name,
contentType: MediaType(mimeType!.split("/").first,
mimeType.split("/").last));
}
FormData data = FormData.fromMap({"pic": file});
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(const SnackBar(
content: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.check_circle,
color: Colors.white,
2023-02-11 22:29:33 +08:00
),
2023-05-14 19:59:43 +08:00
SizedBox(
width: 10,
),
Text("上传成功")
],
),
backgroundColor: Colors.lightGreen,
));
});
} else {}
},
child: const Icon(Icons.add),
)
: FloatingActionButton(
onPressed: null,
child: CircularProgressIndicator(
value: uploadProgress,
),
))
: null,
2023-02-06 23:45:56 +08:00
body: Row(
children: [
MediaQuery.of(context).size.width > 640
? NavigationRail(
2023-02-07 17:28:01 +08:00
elevation: 10,
backgroundColor: const Color(0xffebf6f5),
2023-02-06 23:45:56 +08:00
onDestinationSelected: (idx) {
setState(() {
selectedIndex = idx;
});
2023-02-09 17:07:14 +08:00
context.go(Uri(
path: tabList[idx]["path"] ?? "",
).toString());
2023-02-06 23:45:56 +08:00
},
destinations: const [
NavigationRailDestination(
icon: Icon(Icons.photo_album), label: Text("相册")),
NavigationRailDestination(
icon: Icon(Icons.person), label: Text("用户"))
],
extended: MediaQuery.of(context).size.width >= 1008,
selectedIndex: selectedIndex)
: const SizedBox(
width: 0,
2023-02-07 17:28:01 +08:00
),
2023-02-08 17:20:16 +08:00
Expanded(child: widget.content)
2023-02-06 23:45:56 +08:00
],
),
bottomNavigationBar: MediaQuery.of(context).size.width <= 640
? BottomNavigationBar(
2023-02-07 17:28:01 +08:00
elevation: 10,
2023-02-06 23:45:56 +08:00
currentIndex: selectedIndex,
onTap: (idx) {
setState(() {
selectedIndex = idx;
});
2023-02-08 23:48:25 +08:00
context.go(tabList[idx]["path"] ?? "");
2023-02-06 23:45:56 +08:00
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.photo_album), label: "相册"),
BottomNavigationBarItem(icon: Icon(Icons.person), label: "用户")
])
: null,
);
}
}