edit_user.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 '../globals.dart';
  6. Future<void> edit(BuildContext context, int id, String username, String name) async{
  7. final dio = Dio();
  8. Response response;
  9. AlertDialog alert = AlertDialog(
  10. title: Text("Edit Sukses"),
  11. content: Text("Data teredit"),
  12. actions: [
  13. TextButton(
  14. child: Text('Ok'),
  15. onPressed: () => Navigator.of(context).pop(),
  16. ),
  17. ],
  18. );
  19. AlertDialog alert2 = AlertDialog(
  20. title: Text("Edit Gagal"),
  21. content: Text("Data Invalid"),
  22. actions: [
  23. TextButton(
  24. child: Text('Ok'),
  25. onPressed: () => Navigator.of(context).pop(),
  26. ),
  27. ],
  28. );
  29. var auth = 'Basic ${base64Encode(utf8.encode('$USER_LOGIN:$PASS_LOGIN'))}';
  30. Map<String, String> headers = {
  31. 'content-type': 'application/json',
  32. 'accept': 'application/json',
  33. 'authorization': auth
  34. };
  35. try {
  36. response = await dio.put("http://localhost:8080/api/v1/users/$id",
  37. options: Options(headers: headers),
  38. data: {'username':username, 'name':name}
  39. );
  40. print(response);
  41. if(response.statusCode == 200){
  42. showDialog(context: context, builder: (context) => alert);
  43. }
  44. } catch (error){
  45. showDialog(context: context, builder: (context) => alert2);
  46. }
  47. }