Advertisement
yudiwibisono

jawaban_q3_jenis_pinjaman

May 13th, 2023
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 7.39 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'dart:convert';
  5.  
  6. void main() {
  7.   runApp(const MyApp());
  8. }
  9.  
  10. //screen kedua
  11. class ScreenDetil extends StatelessWidget {
  12.   const ScreenDetil({Key? key}) : super(key: key);
  13.  
  14.   @override
  15.   Widget build(BuildContext context) {
  16.     return Scaffold(
  17.         appBar: AppBar(
  18.           title: const Text(' Detil '),
  19.         ),
  20.         body: BlocBuilder<DetilJenisPinjamanCubit, DetilJenisPinjamanModel>(
  21.             builder: (context, detilPinjaman) {
  22.           return Column(children: [
  23.             Text("id: ${detilPinjaman.id}"),
  24.             Text("nama: ${detilPinjaman.nama}"),
  25.             Text("bunga: ${detilPinjaman.bunga}"),
  26.             Text("Syariah: ${detilPinjaman.isSyariah}"),
  27.           ]);
  28.         }));
  29.   }
  30. }
  31.  
  32. class DetilJenisPinjamanModel {
  33.   String id;
  34.   String nama;
  35.   String bunga;
  36.   String isSyariah;
  37.  
  38.   DetilJenisPinjamanModel({
  39.     required this.id,
  40.     required this.nama,
  41.     required this.bunga,
  42.     required this.isSyariah,
  43.   }); //constructor
  44. }
  45.  
  46. class DetilJenisPinjamanCubit extends Cubit<DetilJenisPinjamanModel> {
  47.   //String url = "http://127.0.0.1:8000/detil_jenis_pinjaman/";
  48.   String url = "http://178.128.17.76:8000/detil_jenis_pinjaman/";
  49.  
  50.   DetilJenisPinjamanCubit()
  51.       : super(DetilJenisPinjamanModel(
  52.             bunga: '', isSyariah: '', id: '', nama: ''));
  53.  
  54.   //map dari json ke atribut
  55.   void setFromJson(Map<String, dynamic> json) {
  56.     emit(DetilJenisPinjamanModel(
  57.       id: json["id"],
  58.       nama: json["nama"],
  59.       bunga: json["bunga"],
  60.       isSyariah: json["is_syariah"],
  61.     ));
  62.   }
  63.  
  64.   void fetchData(String id) async {
  65.     String urlJenis = "$url$id";
  66.     final response = await http.get(Uri.parse(urlJenis));
  67.     if (response.statusCode == 200) {
  68.       setFromJson(jsonDecode(response.body));
  69.     } else {
  70.       throw Exception('Gagal load');
  71.     }
  72.   }
  73. }
  74.  
  75. // --------
  76.  
  77. class JenisPinjaman {
  78.   String id;
  79.   String nama;
  80.   JenisPinjaman({required this.id, required this.nama});
  81. }
  82.  
  83. class JenisPinjamanModel {
  84.   String strPilihanJenis = "0"; //untuk drop down
  85.   List<JenisPinjaman> dataPinjaman;
  86.   JenisPinjamanModel(
  87.       {required this.dataPinjaman,
  88.       required this.strPilihanJenis}); //constructor
  89. }
  90.  
  91. class JenisPinjamanCubit extends Cubit<JenisPinjamanModel> {
  92.   //String url = "http://127.0.0.1:8000/jenis_pinjaman/";
  93.   String url = "http://178.128.17.76:8000/jenis_pinjaman/";
  94.  
  95.   JenisPinjamanCubit()
  96.       : super(JenisPinjamanModel(dataPinjaman: [], strPilihanJenis: "0"));
  97.  
  98.   //map dari json ke atribut
  99.   void setFromJson(Map<String, dynamic> json, String jenis) {
  100.     var arrData = json["data"];
  101.     List<JenisPinjaman> arrOut = [];
  102.     for (var el in arrData) {
  103.       String id = el["id"];
  104.       String nama = el['nama'];
  105.       arrOut.add(JenisPinjaman(id: el["id"], nama: el["nama"]));
  106.     }
  107.     emit(JenisPinjamanModel(dataPinjaman: arrOut, strPilihanJenis: jenis));
  108.   }
  109.  
  110.   void fetchData(String jenis) async {
  111.     String urlJenis = "$url$jenis";
  112.     final response = await http.get(Uri.parse(urlJenis));
  113.     if (response.statusCode == 200) {
  114.       setFromJson(jsonDecode(response.body), jenis);
  115.     } else {
  116.       throw Exception('Gagal load');
  117.     }
  118.   }
  119. }
  120.  
  121. class MyApp extends StatelessWidget {
  122.   const MyApp({Key? key}) : super(key: key);
  123.  
  124.   @override
  125.   Widget build(BuildContext context) {
  126.     return MaterialApp(
  127.         home: MultiBlocProvider(
  128.       providers: [
  129.         BlocProvider<JenisPinjamanCubit>(
  130.           create: (BuildContext context) => JenisPinjamanCubit(),
  131.         ),
  132.         BlocProvider<DetilJenisPinjamanCubit>(
  133.           create: (BuildContext context) => DetilJenisPinjamanCubit(),
  134.         ),
  135.       ],
  136.       child: const HalamanUtama(),
  137.     ));
  138.   }
  139. }
  140.  
  141. class HalamanUtama extends StatelessWidget {
  142.   const HalamanUtama({Key? key}) : super(key: key);
  143.   @override
  144.   Widget build(Object context) {
  145.     return MaterialApp(
  146.         home: Scaffold(
  147.       appBar: AppBar(
  148.         title: const Text(' My App P2P '),
  149.       ),
  150.       body: Center(
  151.         child: BlocBuilder<JenisPinjamanCubit, JenisPinjamanModel>(
  152.           builder: (context, jenisPinjaman) {
  153.             //init combo
  154.             List<DropdownMenuItem<String>> jenis = [];
  155.             var itm0 = const DropdownMenuItem<String>(
  156.               value: "0",
  157.               child: Text("Pilih jenis pinjaman"),
  158.             );
  159.             var itm1 = const DropdownMenuItem<String>(
  160.               value: "1",
  161.               child: Text("Jenis pinjaman 1"),
  162.             );
  163.             var itm2 = const DropdownMenuItem<String>(
  164.               value: "2",
  165.               child: Text("Jenis pinjaman 2"),
  166.             );
  167.             var itm3 = const DropdownMenuItem<String>(
  168.               value: "3",
  169.               child: Text("Jenis pinjaman 3"),
  170.             );
  171.  
  172.             jenis.add(itm0);
  173.             jenis.add(itm1);
  174.             jenis.add(itm2);
  175.             jenis.add(itm3);
  176.  
  177.             //String pilihanJenis = "0";
  178.  
  179.             return Center(
  180.                 child: Column(children: [
  181.               Container(padding: const EdgeInsets.all(10), child: const Text("""
  182. nim1,nama1; nim2,nama2; Saya berjanji tidak akan berbuat curang data atau membantu orang lain berbuat curang""")),
  183.               Container(
  184.                 padding: const EdgeInsets.all(20),
  185.                 child: DropdownButton(
  186.                   value: jenisPinjaman.strPilihanJenis,
  187.                   items: jenis,
  188.                   onChanged: (String? newValue) {
  189.                     if ((newValue != null) && (newValue != "0")) {
  190.                       context.read<JenisPinjamanCubit>().fetchData(newValue);
  191.                     }
  192.                   },
  193.                 ),
  194.               ),
  195.               BlocBuilder<DetilJenisPinjamanCubit, DetilJenisPinjamanModel>(
  196.                   builder: (context, detilPinjaman) {
  197.                 return Expanded(
  198.                     child: ListView.builder(
  199.                         itemCount:
  200.                             jenisPinjaman.dataPinjaman.length, //jumlah baris
  201.                         itemBuilder: (context, index) {
  202.                           return ListTile(
  203.                               onTap: () {
  204.                                 context
  205.                                     .read<DetilJenisPinjamanCubit>()
  206.                                     .fetchData(
  207.                                         jenisPinjaman.dataPinjaman[index].id);
  208.                                 Navigator.of(context)
  209.                                     .push(MaterialPageRoute(builder: (context) {
  210.                                   return ScreenDetil();
  211.                                 }));
  212.                               },
  213.                               leading: Image.network(
  214.                                   'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),
  215.                               trailing: const Icon(Icons.more_vert),
  216.                               title:
  217.                                   Text(jenisPinjaman.dataPinjaman[index].nama),
  218.                               subtitle: Text(
  219.                                   " id: ${jenisPinjaman.dataPinjaman[index].id}  "),
  220.                               tileColor: Colors.white70);
  221.                         }));
  222.               })
  223.             ]));
  224.           },
  225.         ),
  226.       ),
  227.     ));
  228.   }
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement