import 'dart:async'; import 'dart:convert'; 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'; class LoginScreen extends StatefulWidget { const LoginScreen({Key? key}) : super(key: key); @override _LoginScreenState createState() => _LoginScreenState(); } class _LoginScreenState extends State { final TextEditingController _domainTextController = TextEditingController(); final TextEditingController _passwordTextController = TextEditingController(); String error = ""; Future login(String password, String domain) async { Log.i("logging in..."); final compl = Completer(); final resp = await http.post( Uri.parse(domain + '/api/login/login'), headers: { 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode({ '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(); } @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("") ], ), ) ], ), ), ), ], ), ), ); } }