Advertisement
HerdaDwiSulistianto

Untitled

Jan 23rd, 2022
1,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.65 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:http/http.dart' as http;
  3. import 'dart:convert';
  4.  
  5. class HomeScreen extends StatefulWidget {
  6.   HomeScreen({Key? key}) : super(key: key);
  7.  
  8.   @override
  9.   State<HomeScreen> createState() => _HomeScreenState();
  10. }
  11.  
  12. class _HomeScreenState extends State<HomeScreen> {
  13.   final TextEditingController _namaController = TextEditingController();
  14.   final String faseLoading = "faseLoading";
  15.   final String faseDone = "faseDone";
  16.   String fase = "";
  17.  
  18.   String? nama;
  19.   String? jenisKelamin;
  20.  
  21.   @override
  22.   void initState() {
  23.     super.initState();
  24.   }
  25.  
  26.   @override
  27.   Widget build(BuildContext context) {
  28.     return SafeArea(
  29.       child: Scaffold(
  30.         appBar: AppBar(
  31.           elevation: 0,
  32.           title: const Text(
  33.             "Tebak Jenis Kelamin",
  34.             style: TextStyle(fontSize: 20),
  35.           ),
  36.         ),
  37.         body: SingleChildScrollView(
  38.           child: Column(
  39.             crossAxisAlignment: CrossAxisAlignment.center,
  40.             children: [
  41.               _search(context),
  42.               fase == ""
  43.                   ? Container()
  44.                   : fase == faseLoading
  45.                       ? Container(
  46.                           margin: const EdgeInsets.only(top: 20),
  47.                           child: const CircularProgressIndicator())
  48.                       : _jenisKelamin()
  49.             ],
  50.           ),
  51.         ),
  52.       ),
  53.     );
  54.   }
  55.  
  56.   Widget _search(BuildContext context) {
  57.     return Container(
  58.       padding: const EdgeInsets.symmetric(horizontal: 1),
  59.       color: const Color(0xFFEEEEEE),
  60.       child: Card(
  61.         shape: const RoundedRectangleBorder(
  62.             borderRadius: BorderRadius.all(Radius.circular(3))),
  63.         clipBehavior: Clip.antiAliasWithSaveLayer,
  64.         child: Row(
  65.           children: <Widget>[
  66.             Expanded(
  67.               child: Container(
  68.                 padding: const EdgeInsets.only(left: 10, right: 5),
  69.                 decoration: const BoxDecoration(
  70.                   borderRadius: BorderRadius.only(
  71.                       topLeft: Radius.circular(5),
  72.                       bottomLeft: Radius.circular(5)),
  73.                 ),
  74.                 child: TextFormField(
  75.                     controller: _namaController,
  76.                     focusNode: FocusNode(canRequestFocus: false),
  77.                     onSaved: (val) {},
  78.                     cursorColor: Theme.of(context).colorScheme.primary,
  79.                     decoration: const InputDecoration(
  80.                       hintText: 'Masukkan nama',
  81.                       hintStyle:
  82.                           TextStyle(fontSize: 16.0, color: Color(0xFFcccccc)),
  83.                       errorStyle: TextStyle(height: 0),
  84.                       enabledBorder: UnderlineInputBorder(
  85.                         borderSide: BorderSide(color: Colors.transparent),
  86.                       ),
  87.                       focusedBorder: UnderlineInputBorder(
  88.                         borderSide: BorderSide(color: Colors.transparent),
  89.                       ),
  90.                       border: UnderlineInputBorder(
  91.                         borderSide: BorderSide(color: Colors.transparent),
  92.                       ),
  93.                     ),
  94.                     style:
  95.                         const TextStyle(color: Colors.black, fontSize: 16.0)),
  96.               ),
  97.             ),
  98.             GestureDetector(
  99.               onTap: () {
  100.                 if (_namaController.text != "") {
  101.                   tebakJenisKelamin();
  102.                   FocusScope.of(context).requestFocus(FocusNode());
  103.                 } else {
  104.                   setFase("");
  105.                   ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
  106.                     content: Text("Nama belum diisi"),
  107.                   ));
  108.                 }
  109.               },
  110.               child: Wrap(
  111.                 children: [
  112.                   Container(
  113.                     padding: const EdgeInsets.symmetric(
  114.                         vertical: 12, horizontal: 10),
  115.                     decoration: BoxDecoration(
  116.                         color: Theme.of(context).colorScheme.primary,
  117.                         borderRadius: const BorderRadius.only(
  118.                             bottomRight: Radius.circular(3),
  119.                             topRight: Radius.circular(3))),
  120.                     child: const Icon(
  121.                       Icons.search,
  122.                       color: Colors.white,
  123.                     ),
  124.                   ),
  125.                 ],
  126.               ),
  127.             )
  128.           ],
  129.         ),
  130.       ),
  131.     );
  132.   }
  133.  
  134.   Widget _jenisKelamin() {
  135.     if (jenisKelamin != null) {
  136.       return Container(
  137.         margin: const EdgeInsets.only(top: 20),
  138.         child: Column(
  139.           crossAxisAlignment: CrossAxisAlignment.center,
  140.           children: [
  141.             Text(
  142.               jenisKelamin == "male" ? "Laki-laki" : "Perempuan",
  143.               style: const TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
  144.             ),
  145.             Icon(
  146.               jenisKelamin == "male" ? Icons.male : Icons.female,
  147.               color: jenisKelamin == "male" ? Colors.blue : Colors.pink,
  148.               size: 100,
  149.             ),
  150.           ],
  151.         ),
  152.       );
  153.     }
  154.  
  155.     return Container();
  156.   }
  157.  
  158.   tebakJenisKelamin() async {
  159.     setFase(faseLoading);
  160.     var url =
  161.         Uri.parse('https://api.genderize.io/?name=${_namaController.text}');
  162.     var response = await http.get(url);
  163.     var body = json.decode(response.body);
  164.     setState(() {
  165.       jenisKelamin = '${body['gender']}';
  166.       setFase(faseDone);
  167.     });
  168.   }
  169.  
  170.   setFase(String fase) {
  171.     setState(() {
  172.       this.fase = fase;
  173.     });
  174.   }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement