Files
momo/lib/material/home.dart
quantulr 1b88c226ad dio
2023-02-08 23:48:25 +08:00

137 lines
4.8 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/request/http_client.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": "/", "title": "相册"},
{"path": "/profile", "title": "用户"}
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
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 {
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);
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");
},
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;
});
context.go(tabList[idx]["path"] ?? "");
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.photo_album), label: "相册"),
BottomNavigationBarItem(icon: Icon(Icons.person), label: "用户")
])
: null,
);
}
}