Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 9.81 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
  3. import 'package:barcode_scan/barcode_scan.dart';
  4. import 'package:flutter/services.dart';
  5. import 'dart:async';
  6. import 'dart:convert';
  7. import 'package:http/http.dart' as http;
  8.  
  9.  
  10. class Response {
  11.   final String status;
  12.   final String message;
  13.  
  14.   Response({this.status, this.message});
  15.  
  16.   factory Response.fromJson(Map<String, dynamic> json) {
  17.     return Response(
  18.       status: json['status'],
  19.       message: json['message'],
  20.     );
  21.   }
  22. }
  23.  
  24. Future<Response> post(String url,var body)async{
  25.   return await http
  26.       .post(Uri.encodeFull(url), body: body, headers: {"Accept":"application/json"})
  27.       .then((http.Response response) {
  28.  
  29.     final int statusCode = response.statusCode;
  30.  
  31.     if (statusCode < 200 || statusCode > 400 || json == null) {
  32.       throw new Exception("Error while fetching data");
  33.     }
  34.     return Response.fromJson(json.decode(response.body));
  35.   });
  36. }
  37.  
  38.  
  39. class EntryData extends StatefulWidget {
  40.   @override
  41.   EntryDataState createState() => new EntryDataState();
  42. }
  43.  
  44. class EntryDataState extends State<EntryData> {
  45.  
  46.   // text field
  47.   final _nomerRakController = TextEditingController();
  48.   final _namaProdukController = TextEditingController();
  49.   // dropdown category
  50.   List _category = ["Buah-buahan", "Snack", "Stationary", "Baju", "Ice Cream"];
  51.   List<DropdownMenuItem<String>> _dropDownMenuItems;
  52.   String _currentCategory;
  53.   // datepicker
  54.   String _datetime = '';
  55.   int _year = 2018;
  56.   int _month = 11;
  57.   int _date = 11;
  58.   // radio
  59.   String _radioValue1;
  60.   // scanner
  61.   String _scanResult = '';
  62.   // member simpan data
  63.   String _response = '';
  64.   bool apiCall = false;
  65.  
  66.   @override
  67.   void initState() {
  68.     super.initState();
  69.     // dropdown category
  70.     _dropDownMenuItems = getDropDownMenuItems();
  71.     _currentCategory = _dropDownMenuItems[0].value;
  72.     // datepicker
  73.     DateTime now = DateTime.now();
  74.     _year = now.year;
  75.     _month = now.month;
  76.     _date = now.day;
  77.   }
  78.  
  79.   // widget simpan data
  80.   Widget getProperWidget(){
  81.     if(apiCall)
  82.       return AlertDialog(
  83.         content: new Column(
  84.           children: <Widget>[
  85.             CircularProgressIndicator(),
  86.             Text("Please wait")
  87.           ],
  88.         )
  89.       );
  90.     else
  91.       return Center(
  92.         child: Text(
  93.           _response,
  94.           style: new TextStyle(fontSize: 15.0)
  95.         )
  96.       );
  97.   }
  98.  
  99.   void _callPostAPI() {
  100.     post(
  101.         "http://192.3.168.178/restapi/addproduct.php",
  102.         {
  103.           "nomor_rak": _nomerRakController.text,
  104.           "nama_produk": _namaProdukController.text,
  105.           "kategori": _currentCategory,
  106.           "expired_date": _datetime,
  107.           "scan_code": _scanResult,
  108.           "discount": _radioValue1
  109.         }).then((response) {
  110.  
  111.           setState(() {
  112.             apiCall = false;
  113.             _response = response.message;
  114.           });
  115.  
  116.         },
  117.  
  118.         onError: (error) {
  119.           apiCall = false;
  120.           _response = error.toString();
  121.         }
  122.     );
  123.   }
  124.  
  125.   // dropdown category
  126.   List<DropdownMenuItem<String>> getDropDownMenuItems() {
  127.     List<DropdownMenuItem<String>> items = new List();
  128.     for (String kategori in _category) {
  129.       items.add(new DropdownMenuItem(
  130.           value: kategori,
  131.           child: new Text(kategori)
  132.       ));
  133.     }
  134.     return items;
  135.   }
  136.  
  137.   void changedDropDownItem(String selectedCategory) {
  138.     setState(() {
  139.       _currentCategory = selectedCategory;
  140.     });
  141.   }
  142.  
  143.   // radio discount
  144.   void _handleRadioValueChange1(String value) {
  145.     setState(() {
  146.       _radioValue1 = value;
  147.     });
  148.   }
  149.  
  150.   /// Display date picker.
  151.   void _showDatePicker() {
  152.     final bool showTitleActions = false;
  153.     DatePicker.showDatePicker(
  154.       context,
  155.       showTitleActions: true,
  156.       minYear: 2019,
  157.       maxYear: 2022,
  158.       initialYear: _year,
  159.       initialMonth: _month,
  160.       initialDate: _date,
  161.       confirm: Text(
  162.         'PILIH',
  163.         style: TextStyle(color: Colors.red),
  164.       ),
  165.       cancel: Text(
  166.         'BATAL',
  167.         style: TextStyle(color: Colors.cyan),
  168.       ),
  169.       locale: "en",
  170.       dateFormat: "dd-mm-yyyy",
  171.       onChanged: (year, month, date) {
  172.         //debugPrint('onChanged date: $year-$month-$date');
  173.  
  174.         if (!showTitleActions) {
  175.           _changeDatetime(year, month, date);
  176.         }
  177.       },
  178.       onConfirm: (year, month, date) {
  179.         _changeDatetime(year, month, date);
  180.       },
  181.     );
  182.   }
  183.  
  184.   void _changeDatetime(int year, int month, int date) {
  185.     setState(() {
  186.       _year = year;
  187.       _month = month;
  188.       _date = date;
  189.       _datetime = '$date-$month-$year';
  190.     });
  191.   }
  192.  
  193.   // scan QR
  194.   Future _scanQR() async {
  195.     try {
  196.       String qrResult = await BarcodeScanner.scan();
  197.       setState(() {
  198.         _scanResult = qrResult;
  199.       });
  200.     } on PlatformException catch (ex) {
  201.       if (ex.code == BarcodeScanner.CameraAccessDenied) {
  202.         setState(() {
  203.           _scanResult = "Camera permission was denied";
  204.         });
  205.       } else {
  206.         setState(() {
  207.           _scanResult = "Unknown Error $ex";
  208.         });
  209.       }
  210.     } on FormatException {
  211.       setState(() {
  212.         _scanResult = "You pressed the back button before scanning anything";
  213.       });
  214.     } catch (ex) {
  215.       setState(() {
  216.         _scanResult = "Unknown Error $ex";
  217.       });
  218.     }
  219.   }
  220.  
  221.   @override
  222.   Widget build(BuildContext context) {
  223.     return Scaffold(
  224.       appBar: AppBar(
  225.         title: Text('Produk Entry'),
  226.       ),
  227.       body: SafeArea(
  228.           child: ListView(
  229.             padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  230.             children: <Widget>[
  231.               // text field
  232.               TextField(
  233.                 controller: _nomerRakController,
  234.                 decoration: InputDecoration(
  235.                   filled: false,
  236.                   labelText: 'Nomer Rak',
  237.                 ),
  238.               ),
  239.               // spacer
  240.               SizedBox(height: 5.0),
  241.               // text field
  242.               TextField(
  243.                 controller: _namaProdukController,
  244.                 decoration: InputDecoration(
  245.                   filled: false,
  246.                   labelText: 'Nama Produk',
  247.                 ),
  248.               ),
  249.  
  250.               // Dropdown
  251.               new Container(
  252.                 padding: EdgeInsets.all(10.0),
  253.                 //color: Colors.blueGrey,
  254.                 child: new Row(
  255.                   children: <Widget>[
  256.                     new Text("Kategori: ", style: new TextStyle(fontSize: 15.0)),
  257.                     new DropdownButton(
  258.                       value: _currentCategory,
  259.                       items: _dropDownMenuItems,
  260.                       onChanged: changedDropDownItem,
  261.                     )
  262.                   ],
  263.                 ),
  264.               ),
  265.  
  266.               // datepicker
  267.               new Container(
  268.                 //padding: EdgeInsets.all(10.0),
  269.                 //color: Colors.blueGrey,
  270.                 child: new Row(
  271.                   children: <Widget>[
  272.                     RaisedButton(
  273.                       child: Text('Expired Date', style: new TextStyle(fontSize: 15.0)),
  274.                       onPressed: () {
  275.                         _showDatePicker();
  276.                       },
  277.                     ),
  278.                     new Text("  $_datetime", style: new TextStyle(fontSize: 15.0)),
  279.                   ],
  280.                 ),
  281.               ),
  282.  
  283.               // QR scanner
  284.               new Container(
  285.                 //padding: EdgeInsets.all(10.0),
  286.                 //color: Colors.blueGrey,
  287.                 child: new Row(
  288.                   children: <Widget>[
  289.                     RaisedButton(
  290.                       child: Text(' Scan Code  ', style: new TextStyle(fontSize: 15.0)),
  291.                       onPressed: () {
  292.                         _scanQR();
  293.                       },
  294.                     ),
  295.                     new Text("  $_scanResult", style: new TextStyle(fontSize: 15.0)),
  296.                   ],
  297.                 ),
  298.               ),
  299.  
  300.               // Radio
  301.               new Container(
  302.                 //padding: EdgeInsets.all(10.0),
  303.                 //color: Colors.blueGrey,
  304.                 child: new Row(
  305.                   children: <Widget>[
  306.                     new Radio(
  307.                       value: "Discount",
  308.                       groupValue: _radioValue1,
  309.                       onChanged: _handleRadioValueChange1,
  310.                     ),
  311.                     new Text(
  312.                       'Discount',
  313.                       style: new TextStyle(fontSize: 15.0),
  314.                     ),
  315.                     new Radio(
  316.                       value: "Non Discount",
  317.                       groupValue: _radioValue1,
  318.                       onChanged: _handleRadioValueChange1,
  319.                     ),
  320.                     new Text(
  321.                       'Non Discount',
  322.                       style: new TextStyle(fontSize: 15.0),
  323.                     ),
  324.                     ],
  325.                 ),
  326.               ),
  327.  
  328.               // button
  329.               RaisedButton(
  330.                 child: Text('SIMPAN'),
  331.                 onPressed: () {
  332.  
  333.                   setState((){
  334.                     apiCall=true; // Set state like this
  335.                   });
  336.  
  337.                   _callPostAPI();
  338.                   },
  339.  
  340.               ),
  341.  
  342.               // POST Response
  343.               getProperWidget(),
  344.  
  345.             ],
  346.           )
  347.       ),
  348.     );
  349.   }
  350. }
  351.  
  352. void main() => runApp(MyApp());
  353.  
  354. class MyApp extends StatelessWidget {
  355.  
  356.   @override
  357.   Widget build(BuildContext context) {
  358.  
  359.     return MaterialApp(
  360.       title: 'Entry Data Example',
  361.       theme: ThemeData(
  362.         primarySwatch: Colors.blue,
  363.       ),
  364.       home: new EntryData(),
  365.     );
  366.   }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement