105 lines
3.3 KiB
Dart
105 lines
3.3 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.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:provider/provider.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
|
|
.post(Uri.parse("http://192.168.110.156:8080/user/login"),
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({
|
|
"username": usernameController.text,
|
|
"password": passwordController.text
|
|
}))
|
|
.then((resp) {
|
|
if (resp.statusCode == HttpStatus.ok) {
|
|
LoginResp loginResp =
|
|
LoginResp.fromJson(jsonDecode(resp.body));
|
|
ref.watch(tokenProvider.notifier).setToken(loginResp.token);
|
|
// context.go("/");
|
|
// });
|
|
}
|
|
}).catchError((err) {
|
|
print(err.toString());
|
|
});
|
|
}
|
|
}),
|
|
],
|
|
);
|
|
}
|
|
}
|