Login page working

This commit is contained in:
Andrew 2023-04-24 11:04:27 +07:00
parent 67e14ea836
commit 24f4d7de2b
2 changed files with 151 additions and 17 deletions

View file

@ -3,9 +3,139 @@ import 'dart:async';
import 'package:animated_background/animated_background.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:tuuli_app/api/api_client.dart';
import 'package:tuuli_api/tuuli_api.dart';
import 'package:tuuli_app/api_controller.dart';
class LoginPageController extends GetxController
with GetSingleTickerProviderStateMixin {
final _login = "".obs;
String get login => _login.value;
set login(String value) => _login.value = value;
final _password = "".obs;
String get password => _password.value;
set password(String value) => _password.value = value;
final _submitted = false.obs;
bool get submitted => _submitted.value;
set submitted(bool value) => _submitted.value = value;
bool get isFormValid => login.isNotEmpty && password.isNotEmpty;
Future<void> submitForm() async {
submitted = true;
if (isFormValid) {
final amb = AuthModelBuilder()
..username = login
..password = password;
try {
final resp = await ApiController.to.apiClient
.getAccessTokenApiGetAccessTokenPost(authModel: amb.build());
final respData = resp.data;
if (resp.statusCode == 200 && respData != null) {
final accessToken = respData.accessToken;
Get.find<ApiController>().token = accessToken;
Get.offAllNamed("/home");
} else {
Get.snackbar(
"Login failed",
resp.statusMessage ?? "Unknown error",
);
}
} catch (e) {
Get.snackbar(
"Login failed",
"$e",
);
}
}
submitted = false;
}
}
class LoginPage extends GetView<LoginPageController> {
const LoginPage({super.key});
@override
Widget build(BuildContext context) {
final screenSize = Get.mediaQuery.size;
final formWidth = screenSize.width <= 600 ? screenSize.width : 300.0;
return Scaffold(
body: Stack(
children: [
AnimatedBackground(
behaviour: RandomParticleBehaviour(),
vsync: controller,
child: const SizedBox.square(
dimension: 0,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
LimitedBox(
maxWidth: formWidth,
child: Obx(
() => Container(
color: Colors.black.withAlpha(100),
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
enabled: !controller.submitted,
decoration: const InputDecoration(
labelText: 'Login',
hintText: 'Enter your login',
),
onChanged: (value) => controller.login = value,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your Login';
}
return null;
},
),
TextFormField(
obscureText: true,
enabled: !controller.submitted,
decoration: const InputDecoration(
labelText: 'Password',
hintText: 'Enter your password',
),
onChanged: (value) => controller.password = value,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: controller.submitted
? null
: () => controller.submitForm(),
child: const Text('Login'),
),
],
),
),
),
),
],
),
],
),
);
}
}
/*
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@ -19,7 +149,7 @@ class _LoginPageState extends State<LoginPage> with TickerProviderStateMixin {
final apiClient = Get.find<ApiClient>();
var submitted = false;
final loginController = TextEditingController();
final loginPageController = TextEditingController();
final passwordController = TextEditingController();
@override
@ -53,7 +183,7 @@ class _LoginPageState extends State<LoginPage> with TickerProviderStateMixin {
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
controller: loginController,
controller: loginPageController,
enabled: !submitted,
decoration: const InputDecoration(
labelText: 'Login',
@ -113,7 +243,7 @@ class _LoginPageState extends State<LoginPage> with TickerProviderStateMixin {
);
final response = await apiClient.login(
loginController.text.trim(),
loginPageController.text.trim(),
passwordController.text.trim(),
);
response.unfold((data) {
@ -148,3 +278,4 @@ class _LoginPageState extends State<LoginPage> with TickerProviderStateMixin {
});
}
}
*/