Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:apetrus/utils/Constantes.dart' as Constantes;
- import 'package:shared_preferences/shared_preferences.dart';
- import 'home_page.dart';
- import 'package:http/http.dart' as http;
- import 'dart:convert';
- import 'dart:async';
- class LoginPage extends StatefulWidget {
- @override
- _LoginPageState createState() => _LoginPageState();
- }
- class _LoginPageState extends State<LoginPage> {
- SharedPreferences prefs;
- bool checkvalue = false;
- TextEditingController usuario = new TextEditingController();
- TextEditingController senha = new TextEditingController();
- Map<String, dynamic> retornoJSON;
- bool _isLoading = false;
- final formKey = new GlobalKey<FormState>();
- final scaffoldKey = new GlobalKey<ScaffoldState>();
- String _usuario, _senha;
- @override
- initState() {
- _getDadosSalvos();
- super.initState();
- }
- void _submit() {
- final form = formKey.currentState;
- if(form.validate()) {
- form.save();
- setState(() {
- _isLoading = true;
- });
- _validaLogin(_usuario, _senha);
- }
- }
- _getDadosSalvos() async {
- prefs = await SharedPreferences.getInstance();
- setState(() {
- checkvalue = (prefs.getBool("_checkvalue") ?? false);
- usuario.text = (prefs.getString("_usuario") ?? "");
- senha.text = (prefs.getString("_senha") ?? "");
- });
- }
- _salvarLocal() async {
- prefs = await SharedPreferences.getInstance();
- if(checkvalue) {
- setState(() {
- prefs.setString("_usuario", _usuario);
- prefs.setString("_senha", _senha);
- prefs.setBool("_checkvalue", checkvalue);
- });
- } else {
- usuario.clear();
- senha.clear();
- prefs.clear();
- }
- }
- // Navigator.of(context).pushNamed('/homepage');
- Future<Null> _validaLogin(String email, String senha) async {
- Map<String, String> dadosUser = {
- 'email': email,
- 'senha': senha
- };
- var result = await http.post(Constantes.url + "loginMobile", body: dadosUser);
- String retornoBody = result.body;
- retornoJSON = json.decode(retornoBody);
- if(result.request.finalized) {
- setState(() {
- _isLoading = false;
- });
- if(retornoJSON['result'] == 'erro') {
- _showErro("Ops!","Erro ao acessar, revise seus dados!");
- } else {
- _salvarLocal();
- Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => HomePage(retornoJSON['result'])),
- );
- }
- } else {
- _showErro("Ops!","Não foi possivel carregar!");
- setState(() {
- _isLoading = false;
- });
- }
- }
- _showErro(String titulo, String mensagem) {
- showDialog(
- context: context,
- builder: (BuildContext context) {
- return new AlertDialog(
- title: new Text(titulo),
- content: new Text(mensagem),
- actions: <Widget>[
- new FlatButton(
- onPressed: () => Navigator.of(context).pop(),
- child: new Text("OK")
- )
- ],
- );
- }
- );
- }
- @override
- Widget build(BuildContext context) {
- var loginBtn = new SizedBox(
- width: double.maxFinite,
- child: new RaisedButton(
- shape: new RoundedRectangleBorder(
- borderRadius: new BorderRadius.circular(20.0)
- ),
- onPressed: _submit,
- color: Theme.of(context).primaryColor,
- textColor: Colors.white,
- child: new Text("ENTRAR"),
- ),
- );
- var loginForm = new ListView(
- children: <Widget>[
- new Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- new Container(
- color: const Color(Constantes.CorPadrao),
- width: double.infinity,
- height: 200.0,
- child: new Padding(
- padding: const EdgeInsets.all(30.0),
- child: new Image.asset(
- Constantes.logo
- ),
- ),
- ),
- new Divider(
- height: 1.0,
- color: Theme.of(context).primaryColor,
- ),
- new Padding(
- padding: const EdgeInsets.all(20.0),
- child: new Form(
- key: formKey,
- child: new Column(
- children: <Widget>[
- new TextFormField(
- controller: usuario,
- onSaved: (val) => _usuario = val,
- validator: (val) {
- return val.isEmpty
- ? "Digite seu e-mail"
- : null;
- },
- decoration: new InputDecoration(labelText: "E-mail"),
- ),
- new Padding(
- padding: const EdgeInsets.only(bottom: 10.0)
- ),
- new TextFormField(
- controller: senha,
- onSaved: (val) => _senha = val,
- validator: (val) {
- return val.isEmpty
- ? "Digite sua senha"
- : null;
- },
- obscureText: true,
- decoration: new InputDecoration(labelText: "Senha"),
- ),
- new Padding(
- padding: const EdgeInsets.only(bottom: 10.0)
- ),
- loginBtn,
- new Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- new Checkbox(
- value: checkvalue,
- onChanged: (bool newvalue) {
- setState(() {
- checkvalue = newvalue;
- });
- }),
- new Text("Salvar dados de acesso")
- ],
- ),
- ],
- )
- ),
- ),
- ],
- ),
- ],
- );
- var loader = new Center(
- child: new CircularProgressIndicator(),
- );
- return Scaffold(
- appBar: null,
- key: scaffoldKey,
- body: _isLoading ? loader : loginForm,
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement