halaman login

login with DIO, emulator jalan, tp real device error...
mail@pastecode.io avatar
unknown
dart
a year ago
5.3 kB
7
Indexable
Never
import 'package:dio/dio.dart';
import 'package:lapis_legit/person.dart';
import 'package:lapis_legit/theme/theme_file.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class TigaPage extends StatefulWidget {
  const TigaPage({super.key});

  @override
  State<TigaPage> createState() => _TigaPageState();
}

class Services {
  static Future<Person?> signIn(String email, String password) async {
    try {
      var response = await Dio()
          .post('https://impostor.brainwarehub.com/api/login', data: {
        'email': email.toString(),        //aslinya "email", pakek petik, dirubah email.toString(), jalan
        'password': password.toString(), // aslinya "password", pakek petik
      });

      if (response.statusCode == 200) {
        print(response.data);
        return Person(
          email: response.data['email'],
          password: response.data['password'],
          success: response.data['success'],
        );
      }

      return null;
    } on DioException catch (exception) {
      print(exception);
      // SnackBar(content: Text('Email atau password salahhhh'));
      // print("failed login " + e.toString());
      // throw Exception(e.toString());
    }
  }
}
//===========================PEMBATAS=======================================================

class _TigaPageState extends State<TigaPage> with WidgetsBindingObserver {
  final TextEditingController emailController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  //===
  @override
  void dispose() {
    // TODO: implement dispose
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  //===
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.paused) {
      FocusScope.of(context).unfocus();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: kWhiteColor,
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Image.asset(
                'assets/coffee.PNG',
                height: 300,
                // width: MediaQuery.of(context).size.width,
              ),
              SizedBox(height: 20),
              //text form field email
              TextField(
                // keyboardType: TextInputType.emailAddress,
                controller: emailController,
                decoration: InputDecoration(
                  labelText: 'Email',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
              ),
              SizedBox(height: 20),
              //text form field password
              TextFormField(
                controller: passwordController,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  labelText: 'Password',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
              ),
              SizedBox(height: 20),
              //button Sign in
              Container(
                width: MediaQuery.of(context).size.width,
                child: TextButton(
                  style: TextButton.styleFrom(
                    backgroundColor: kOrangeColor,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                  onPressed: () async {
                    Person? result = await Services.signIn(
                        emailController.text, passwordController.text);

                    
                    if (result?.success == true) {
                      Navigator.pushNamed(context, '/mainscreen');
                    } else {
                      print('failed login hlmn 3.dart');
                      final snackBar =
                          SnackBar(content: Text('email atau password salahh'));
                      ScaffoldMessenger.of(context).showSnackBar(snackBar);
                    }
                  },
                  child: Text(
                    'Sign In ',
                    style: TextStyle(
                      fontSize: 20,
                      color: kWhiteColor,
                    ),
                  ),
                ),
              ),
              SizedBox(height: 70),
              //copyright watermark
              Text(
                '2023 INDRACO All right reserved',
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.grey.shade400,
                ),
              ),
            ],
          ),
        ),
      ),
      backgroundColor: kWhiteColor,
    );
  }
}