User panel fully functional

This commit is contained in:
Andrew 2023-04-25 15:08:03 +07:00
parent e562216658
commit f67e947f8b
16 changed files with 1346 additions and 641 deletions

View file

@ -1,6 +1,5 @@
import 'dart:async';
import 'package:animated_background/animated_background.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
@ -8,9 +7,9 @@ import 'package:tuuli_api/tuuli_api.dart';
import 'package:tuuli_app/api_controller.dart';
class LoginPageController extends GetxController {
final _login = "".obs;
String get login => _login.value;
set login(String value) => _login.value = value;
final _username = "".obs;
String get username => _username.value;
set username(String value) => _username.value = value;
final _password = "".obs;
String get password => _password.value;
@ -20,18 +19,18 @@ class LoginPageController extends GetxController {
bool get submitted => _submitted.value;
set submitted(bool value) => _submitted.value = value;
bool get isFormValid => login.isNotEmpty && password.isNotEmpty;
bool get isFormValid => username.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 resp = await ApiController.to.apiClient.getAccessToken(
authModel: AuthModel(
username: username,
password: password,
),
);
final respData = resp.data;
if (resp.statusCode == 200 && respData != null) {
@ -105,9 +104,9 @@ class LoginPage extends GetView<LoginPageController> {
enabled: !controller.submitted,
decoration: const InputDecoration(
labelText: 'Login',
hintText: 'Enter your login',
hintText: 'Enter your username',
),
onChanged: (value) => controller.login = value,
onChanged: (value) => controller.username = value,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your Login';
@ -149,147 +148,3 @@ class LoginPage extends GetView<LoginPageController> {
);
}
}
/*
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<StatefulWidget> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> with TickerProviderStateMixin {
final _formKey = GlobalKey<FormState>();
final apiClient = Get.find<ApiClient>();
var submitted = false;
final loginPageController = TextEditingController();
final passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
final screenSize = MediaQuery.of(context).size;
final formWidth = screenSize.width <= 600 ? screenSize.width : 300.0;
return Scaffold(
body: Stack(
children: [
AnimatedBackground(
behaviour: RandomParticleBehaviour(),
vsync: this,
child: const SizedBox.square(
dimension: 0,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
LimitedBox(
maxWidth: formWidth,
child: Container(
color: Colors.black.withAlpha(100),
padding: const EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
controller: loginPageController,
enabled: !submitted,
decoration: const InputDecoration(
labelText: 'Login',
hintText: 'Enter your login',
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your Login';
}
return null;
},
),
TextFormField(
controller: passwordController,
obscureText: true,
enabled: !submitted,
decoration: const InputDecoration(
labelText: 'Password',
hintText: 'Enter your password',
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: submitted ? null : _submit,
child: const Text('Login'),
),
],
),
),
),
),
],
),
],
),
);
}
Future<void> _submit() async {
if (!_formKey.currentState!.validate()) {
return;
}
setState(() {
submitted = true;
});
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Trying to login...'),
),
);
final response = await apiClient.login(
loginPageController.text.trim(),
passwordController.text.trim(),
);
response.unfold((data) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Login successful'),
),
);
apiClient.setAccessToken(data.accessToken);
GetStorage()
.write("accessToken", data.accessToken)
.then((value) => GetStorage().save())
.then((value) {
Timer(1.seconds, () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
WidgetsBinding.instance.addPostFrameCallback((_) {
Get.offAllNamed("/home");
});
});
});
}, (error) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(error.toString()),
),
);
setState(() {
submitted = false;
});
});
}
}
*/