Files
momo/lib/material/home.dart
2023-02-08 17:20:16 +08:00

122 lines
4.2 KiB
Dart

import 'package:dio/dio.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
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';
class HomePage extends ConsumerStatefulWidget {
const HomePage({Key? key, required this.content}) : super(key: key);
final Widget content;
@override
ConsumerState<HomePage> createState() => _HomePageState();
}
class _HomePageState extends ConsumerState<HomePage> {
int selectedIndex = 0;
final tabList = [
{
"path": "/",
},
{"path": "/profile"}
];
@override
Widget build(BuildContext context) {
String? token = ref.watch(tokenProvider);
return Scaffold(
appBar: AppBar(
title: const Text(
"App Header",
style: 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);
if (result != null) {
String? filePath = result.files.first.path;
if (filePath == null) {
return;
}
Dio dio = Dio();
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))
});
Response resp = await dio.post(
"http://192.168.110.156:8080/image/upload",
data: data,
options: Options(headers: {"Authorization": token}));
} else {}
// context.go("/login");
},
child: const Icon(Icons.add),
)
: null,
body: Row(
children: [
MediaQuery.of(context).size.width > 640
? NavigationRail(
elevation: 10,
backgroundColor: const Color(0xffebf6f5),
leading: MediaQuery.of(context).size.width >= 1008
? const Text(
"Side Header",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 36),
)
: null,
onDestinationSelected: (idx) {
setState(() {
selectedIndex = idx;
});
context.go(tabList[idx]["path"] ?? "");
},
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,
),
Expanded(child: widget.content)
],
),
bottomNavigationBar: MediaQuery.of(context).size.width <= 640
? BottomNavigationBar(
elevation: 10,
currentIndex: selectedIndex,
onTap: (idx) {
setState(() {
selectedIndex = idx;
});
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.photo_album), label: "相册"),
BottomNavigationBarItem(icon: Icon(Icons.person), label: "用户")
])
: null,
);
}
}