fix
This commit is contained in:
101
lib/material/login.dart
Normal file
101
lib/material/login.dart
Normal 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("/");
|
||||
});
|
||||
}
|
||||
}
|
||||
}),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user