Files
momo/lib/material/home.dart

137 lines
4.8 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-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-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-08 17:20:16 +08:00
final tabList = [
2023-02-08 23:48:25 +08:00
{"path": "/", "title": "相册"},
{"path": "/profile", "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(
tabList[selectedIndex]["title"] ?? "",
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-02-08 17:20:16 +08:00
floatingActionButton: selectedIndex == 0
? 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))
});
2023-02-08 23:48:25 +08:00
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,
));
2023-02-08 17:20:16 +08:00
} else {}
// context.go("/login");
},
child: const Icon(Icons.add),
)
: 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
leading: MediaQuery.of(context).size.width >= 1008
? const Text(
2023-02-07 17:28:01 +08:00
"Side Header",
2023-02-06 23:45:56 +08:00
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 36),
)
: null,
onDestinationSelected: (idx) {
setState(() {
selectedIndex = idx;
});
2023-02-08 17:20:16 +08:00
context.go(tabList[idx]["path"] ?? "");
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,
);
}
}