This commit is contained in:
cxc
2023-02-07 17:28:01 +08:00
parent ac65c1dec0
commit 06ff4a41f4
25 changed files with 537 additions and 184 deletions

View File

@ -1,14 +1,49 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:momo/material/router.dart';
import 'package:momo/provider/token.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MyMaterialApp extends StatelessWidget {
class MyMaterialApp extends ConsumerWidget {
const MyMaterialApp({Key? key}) : super(key: key);
Future<String?> loadToken() async {
final prefs = await SharedPreferences.getInstance();
String? tk = await prefs.getString("token");
return tk;
}
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: MyMaterialRouterConfig.router,
theme: ThemeData(useMaterial3: true),
);
Widget build(BuildContext context, WidgetRef ref) {
return FutureBuilder(
future: loadToken(),
builder: (BuildContext context, AsyncSnapshot<String?> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
String? token = snapshot.data;
if (token != null) {
// ref
// .watch(
// tokenProvider.notifier,
// )
// .setToken(token);
}
MyMaterialRouterConfig myMaterialRouterConfig =
MyMaterialRouterConfig(token);
return MaterialApp.router(
routerConfig: myMaterialRouterConfig.router,
theme: ThemeData(
useMaterial3: true, scaffoldBackgroundColor: Colors.white),
);
} else {
return MaterialApp(
theme: ThemeData(
useMaterial3: true, scaffoldBackgroundColor: Colors.white),
home: const Center(
child: CircularProgressIndicator(),
),
);
}
});
}
}

27
lib/material/gallery.dart Normal file
View File

@ -0,0 +1,27 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:momo/provider/token.dart';
class Gallery extends ConsumerStatefulWidget {
const Gallery({Key? key}) : super(key: key);
@override
ConsumerState<Gallery> createState() => _GalleryState();
}
class _GalleryState extends ConsumerState<Gallery> {
@override
Widget build(BuildContext context) {
String token = ref.watch(tokenProvider);
print(token);
return Center(
child: ElevatedButton(
onPressed: () {
// ref.watch(tokenProvider.notifier).removeToken();
context.go("/login");
},
child: Text("exit")),
);
}
}

View File

@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:momo/material/gallery.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@ -14,16 +16,21 @@ class _HomePageState extends State<HomePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.pinkAccent,
title: const Text("APP"),
title: const Text(
"App Header",
style: TextStyle(fontWeight: FontWeight.bold),
),
elevation: 10,
),
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(
"Header",
"Side Header",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 36),
)
@ -43,11 +50,13 @@ class _HomePageState extends State<HomePage> {
selectedIndex: selectedIndex)
: const SizedBox(
width: 0,
)
),
Expanded(child: pageList[selectedIndex])
],
),
bottomNavigationBar: MediaQuery.of(context).size.width <= 640
? BottomNavigationBar(
elevation: 10,
currentIndex: selectedIndex,
onTap: (idx) {
setState(() {
@ -63,3 +72,10 @@ class _HomePageState extends State<HomePage> {
);
}
}
List<Widget> pageList = [
Gallery(),
Scaffold(
body: Placeholder(),
)
];

101
lib/material/login.dart Normal file
View File

@ -0,0 +1,101 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
import 'package:momo/models/login_resp.dart';
import 'package:momo/provider/token.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LoginPage extends StatelessWidget {
const LoginPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("登录"),
centerTitle: true,
),
body: const Padding(
padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
child: LoginForm(),
));
}
}
class LoginForm extends ConsumerStatefulWidget {
const LoginForm({Key? key}) : super(key: key);
@override
ConsumerState<LoginForm> createState() => _LoginFormState();
}
class _LoginFormState extends ConsumerState<LoginForm> {
TextEditingController usernameController =
TextEditingController(text: "kencho");
TextEditingController passwordController =
TextEditingController(text: "169482");
@override
Widget build(BuildContext context) {
return ListView(
children: [
Form(
child: Column(
children: [
TextFormField(
controller: usernameController,
decoration: const InputDecoration(
enabledBorder: OutlineInputBorder(borderSide: BorderSide()),
focusedBorder: OutlineInputBorder(borderSide: BorderSide()),
hintText: 'Enter your username',
),
),
const SizedBox(
height: 20,
),
TextFormField(
controller: passwordController,
decoration: const InputDecoration(
enabledBorder: OutlineInputBorder(borderSide: BorderSide()),
focusedBorder: OutlineInputBorder(borderSide: BorderSide()),
hintText: 'Enter your password',
),
),
const SizedBox(
height: 20,
),
],
)),
ElevatedButton(
child: const Text("登录"),
onPressed: () async {
if (usernameController.text.isNotEmpty &&
passwordController.text.isNotEmpty) {
http.Response resp = await http.post(
Uri.parse("http://localhost:8080/user/login"),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"username": usernameController.text,
"password": passwordController.text
}));
if (resp.statusCode == HttpStatus.ok) {
LoginResp loginResp =
LoginResp.fromJson(jsonDecode(resp.body));
SharedPreferences prefs =
await SharedPreferences.getInstance();
// ref.watch(tokenProvider.notifier).setToken(loginResp.token);
prefs.setString("token", loginResp.token).then((value) {
context.go("/");
});
}
}
}),
],
);
}
}

View File

@ -1,16 +1,32 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:momo/material/home.dart';
import 'package:momo/material/login.dart';
class MyMaterialRouterConfig {
static GoRouter router = GoRouter(
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) {
return HomePage();
},
),
],
);
late GoRouter router;
MyMaterialRouterConfig(String? token) {
router = GoRouter(
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) {
return const HomePage();
},
redirect: (BuildContext context, GoRouterState state) {
if (token == null || token.isEmpty) {
return '/login';
}
return null;
}),
GoRoute(
path: "/login",
builder: (BuildContext context, GoRouterState state) {
return const LoginPage();
},
)
],
);
}
}