OpenMediacenterMobileFlutter/lib/login/login_screen.dart

163 lines
5.9 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
2021-12-10 10:40:20 +00:00
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:openmediacentermobile/api/token.dart';
import 'package:openmediacentermobile/log/log.dart';
import 'package:openmediacentermobile/login/logincontext.dart';
2021-12-10 10:40:20 +00:00
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
2021-12-10 10:40:20 +00:00
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final TextEditingController _domainTextController = TextEditingController();
final TextEditingController _passwordTextController = TextEditingController();
String error = "";
Future<String> login(String password, String domain) async {
Log.i("logging in...");
final compl = Completer<String>();
final resp = await http.post(
Uri.parse(domain + '/api/login/login'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'Password': password,
}),
);
if (resp.statusCode != 200) {
compl.complete(resp.body);
} else {
final json = jsonDecode(resp.body);
final token = json["Token"];
Token.getInstance().setToken(token, domain);
LoginContext.of(context).onLoggin(true);
compl.complete("");
}
// LoginContext.of(context).onLoggin(true);
return compl.future;
}
@override
void dispose() {
_domainTextController.dispose();
_passwordTextController.dispose();
super.dispose();
}
2021-12-10 10:40:20 +00:00
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/login.png'), fit: BoxFit.cover),
),
child: Scaffold(
backgroundColor: Colors.transparent,
body: Stack(
children: [
Container(
padding: const EdgeInsets.only(left: 35, top: 90),
child: const Text(
'Welcome\nBack',
style: TextStyle(color: Colors.white, fontSize: 33),
),
),
SingleChildScrollView(
child: Container(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(left: 35, right: 35),
child: Column(
children: [
TextField(
controller: _domainTextController,
style: const TextStyle(color: Colors.black),
decoration: InputDecoration(
fillColor: Colors.grey.shade100,
filled: true,
hintText: "Domain",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
)),
),
const SizedBox(
height: 30,
),
TextField(
controller: _passwordTextController,
style: const TextStyle(),
obscureText: true,
decoration: InputDecoration(
fillColor: Colors.grey.shade100,
filled: true,
hintText: "Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
)),
),
const SizedBox(
height: 40,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Sign in',
style: TextStyle(
fontSize: 27, fontWeight: FontWeight.w700),
),
CircleAvatar(
radius: 30,
backgroundColor: const Color(0xff4c505b),
child: IconButton(
color: Colors.white,
onPressed: () {
final pwd =
_passwordTextController.value.text;
final domain =
_domainTextController.value.text;
login(pwd, domain).then((value) {
if (value != "") {
setState(() {
error = value;
});
}
});
},
icon: const Icon(
Icons.arrow_forward,
)),
)
],
),
error != "" ? Text(error) : const Text("")
],
),
)
],
),
),
),
],
),
),
);
2021-12-10 10:40:20 +00:00
}
}