listuser.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // ignore_for_file: must_call_super, prefer_typing_uninitialized_variables
  2. import 'package:dio/dio.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:go_router/go_router.dart';
  5. import 'package:namer_app/footer.dart';
  6. import 'package:namer_app/globals.dart';
  7. import 'package:namer_app/header.dart';
  8. import '../service/delete_user.dart';
  9. class ListUserPage extends StatefulWidget {
  10. const ListUserPage({super.key});
  11. @override
  12. State<ListUserPage> createState() => _ListUserPageState();
  13. }
  14. class _ListUserPageState extends State<ListUserPage> {
  15. var jsonList;
  16. @override
  17. void initState(){
  18. getData();
  19. }
  20. void getData() async {
  21. try {
  22. var response = await Dio()
  23. .get('http://localhost:8080/api/v1/users',
  24. options: Options(headers: headers));
  25. if (response.statusCode == 200) {
  26. setState(() {
  27. jsonList = response.data['results'] as List;
  28. });
  29. } else {
  30. print(response.statusCode);
  31. }
  32. } catch (e) {
  33. print(e);
  34. }
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. return Scaffold(
  39. appBar: CustomAppbar(),
  40. body: Stack(children: [
  41. SingleChildScrollView(
  42. child: Column(
  43. children: [
  44. Center(
  45. child: Row(
  46. mainAxisAlignment: MainAxisAlignment.center,
  47. children: [
  48. Text(
  49. 'LIST USER',
  50. style: TextStyle(color: Colors.black, fontSize: 48),
  51. ),
  52. ],
  53. )),
  54. Column(
  55. children:
  56. List.generate(
  57. jsonList == null ? 0 : jsonList.length,
  58. (index) => SizedBox(
  59. child: ListTile(
  60. leading: SizedBox(
  61. child: Icon(Icons.person),
  62. ),
  63. title: Text(jsonList[index]['username']),
  64. subtitle: Text(jsonList[index]['name']),
  65. trailing: ButtonUser(items: jsonList[index]),
  66. ),
  67. ))
  68. ),
  69. ],
  70. ),
  71. ),
  72. ]),
  73. bottomNavigationBar: Footer(),
  74. );
  75. }
  76. }
  77. class ButtonUser extends StatelessWidget {
  78. final Map<String, dynamic> items;
  79. ButtonUser({
  80. required this.items,
  81. super.key,
  82. });
  83. @override
  84. Widget build(BuildContext context) {
  85. return Row(
  86. mainAxisSize: MainAxisSize.min,
  87. children: [
  88. ElevatedButton(
  89. onPressed: () => context.go("/signup/listuser/edit/$items['id']"),
  90. style: ElevatedButton.styleFrom(
  91. backgroundColor: Colors.black,
  92. ),
  93. child: Text(
  94. 'Edit',
  95. style: TextStyle(color: Colors.white),
  96. ),
  97. ),
  98. ElevatedButton(
  99. //todo tombol item
  100. onPressed: () => deluser(context, items['id']),
  101. style: ElevatedButton.styleFrom(
  102. backgroundColor: Colors.black,
  103. ),
  104. child: Text(
  105. 'Delete',
  106. style: TextStyle(color: Colors.white),
  107. ),
  108. ),
  109. ],
  110. );
  111. }
  112. }