submit.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // ignore_for_file: use_build_context_synchronously
  2. import 'dart:convert';
  3. import 'package:dio/dio.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:go_router/go_router.dart';
  6. import 'package:namer_app/globals.dart' as globals;
  7. Future<void> submit(BuildContext context, String email, String password) async {
  8. final dio = Dio();
  9. Response response;
  10. if (email.isEmpty || password.isEmpty) {
  11. final snackBar = SnackBar(
  12. duration: const Duration(seconds: 5),
  13. content: Text("Username dan password harus diisi"),
  14. backgroundColor: Colors.red,
  15. );
  16. ScaffoldMessenger.of(context).showSnackBar(snackBar);
  17. return;
  18. }
  19. AlertDialog alert2 = AlertDialog(
  20. title: Text("Login Gagal"),
  21. content: Text("Username atau Password salah"),
  22. actions: [
  23. TextButton(
  24. child: Text('Ok'),
  25. onPressed: () => Navigator.of(context).pop(),
  26. ),
  27. ],
  28. );
  29. var auth = 'Basic ${base64Encode(utf8.encode('$email:$password'))}';
  30. Map<String, String> headers = {
  31. 'content-type': 'application/json',
  32. 'accept': 'application/json',
  33. 'authorization': auth
  34. };
  35. try{
  36. response = await dio.get('http://localhost:8080/api/v1/users',
  37. options: Options(headers: headers));
  38. if (response.statusCode == 200){
  39. globals.userLogin = email;
  40. return context.go('/login/bug');
  41. }
  42. } catch (error){ showDialog(context: context, builder: (context) => alert2);}
  43. }