This commit is contained in:
quantulr
2023-02-08 23:48:25 +08:00
parent 53ff2fc59b
commit 1b88c226ad
14 changed files with 102 additions and 470 deletions

View File

@ -5,7 +5,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';
import 'package:momo/provider/token.dart';
import 'package:momo/request/http_client.dart';
class HomePage extends ConsumerStatefulWidget {
const HomePage({Key? key, required this.content}) : super(key: key);
@ -18,29 +18,31 @@ class HomePage extends ConsumerStatefulWidget {
class _HomePageState extends ConsumerState<HomePage> {
int selectedIndex = 0;
final tabList = [
{
"path": "/",
},
{"path": "/profile"}
{"path": "/", "title": "相册"},
{"path": "/profile", "title": "用户"}
];
@override
Widget build(BuildContext context) {
String? token = ref.watch(tokenProvider);
return Scaffold(
appBar: AppBar(
title: const Text(
"App Header",
style: TextStyle(fontWeight: FontWeight.bold),
leading: ["/", "/profile"].contains(GoRouterState.of(context).location)
? null
: IconButton(
icon: const Icon(Icons.keyboard_arrow_left),
onPressed: () {
context.go("/");
},
),
title: Text(
tabList[selectedIndex]["title"] ?? "",
style: const TextStyle(fontWeight: FontWeight.bold),
),
elevation: 10,
),
floatingActionButton: selectedIndex == 0
? FloatingActionButton(
onPressed: () async {
if (token == null) {
return;
}
FilePickerResult? result =
await FilePicker.platform.pickFiles(type: FileType.image);
@ -49,7 +51,6 @@ class _HomePageState extends ConsumerState<HomePage> {
if (filePath == null) {
return;
}
Dio dio = Dio();
String? mimeType = lookupMimeType(filePath);
FormData data = FormData.fromMap({
"pic": await MultipartFile.fromFile(filePath,
@ -57,10 +58,23 @@ class _HomePageState extends ConsumerState<HomePage> {
contentType: MediaType(mimeType!.split("/").first,
mimeType.split("/").last))
});
Response resp = await dio.post(
"http://192.168.110.156:8080/image/upload",
data: data,
options: Options(headers: {"Authorization": token}));
await dio.post("/image/upload", data: data);
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 {}
// context.go("/login");
@ -109,6 +123,7 @@ class _HomePageState extends ConsumerState<HomePage> {
setState(() {
selectedIndex = idx;
});
context.go(tabList[idx]["path"] ?? "");
},
items: const [
BottomNavigationBarItem(