Advertisement
Guest User

main.dart

a guest
Jan 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.93 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:fluttertoast/fluttertoast.dart';
  4.  
  5.  
  6. class EntryData extends StatefulWidget {
  7.   @override
  8.   EntryDataState createState() => new EntryDataState();
  9. }
  10.  
  11. class EntryDataState extends State<EntryData> {
  12.  
  13.   // text field
  14.   final _nomerRakController = TextEditingController();
  15.   final _namaProdukController = TextEditingController();
  16.   // dropdown category
  17.   List _category = ["Buah-buahan", "Snack", "Stationary", "Baju", "Ice Cream"];
  18.   List<DropdownMenuItem<String>> _dropDownMenuItems;
  19.   String _currentCategory;
  20.   // datepicker
  21.   String _datetime = '';
  22.   int _year = 2018;
  23.   int _month = 11;
  24.   int _date = 11;
  25.   // radio
  26.   int _radioValue1;
  27.  
  28.   @override
  29.   void initState() {
  30.     super.initState();
  31.     // dropdown category
  32.     _dropDownMenuItems = getDropDownMenuItems();
  33.     _currentCategory = _dropDownMenuItems[0].value;
  34.     // datepicker
  35.     DateTime now = DateTime.now();
  36.     _year = now.year;
  37.     _month = now.month;
  38.     _date = now.day;
  39.   }
  40.  
  41.   // dropdown category
  42.   List<DropdownMenuItem<String>> getDropDownMenuItems() {
  43.     List<DropdownMenuItem<String>> items = new List();
  44.     for (String kategori in _category) {
  45.       items.add(new DropdownMenuItem(
  46.           value: kategori,
  47.           child: new Text(kategori)
  48.       ));
  49.     }
  50.     return items;
  51.   }
  52.  
  53.   void changedDropDownItem(String selectedCategory) {
  54.     setState(() {
  55.       _currentCategory = selectedCategory;
  56.     });
  57.   }
  58.  
  59.   // radio discount
  60.   void _handleRadioValueChange1(int value) {
  61.     setState(() {
  62.       _radioValue1 = value;
  63.     });
  64.   }
  65.  
  66.   /// Display date picker.
  67.   void _showDatePicker() {
  68.     final bool showTitleActions = false;
  69.     DatePicker.showDatePicker(
  70.       context,
  71.       showTitleActions: true,
  72.       minYear: 2019,
  73.       maxYear: 2022,
  74.       initialYear: _year,
  75.       initialMonth: _month,
  76.       initialDate: _date,
  77.       confirm: Text(
  78.         'PILIH',
  79.         style: TextStyle(color: Colors.red),
  80.       ),
  81.       cancel: Text(
  82.         'BATAL',
  83.         style: TextStyle(color: Colors.cyan),
  84.       ),
  85.       locale: "en",
  86.       dateFormat: "dd-mm-yyyy",
  87.       onChanged: (year, month, date) {
  88.         //debugPrint('onChanged date: $year-$month-$date');
  89.  
  90.         if (!showTitleActions) {
  91.           _changeDatetime(year, month, date);
  92.         }
  93.       },
  94.       onConfirm: (year, month, date) {
  95.         _changeDatetime(year, month, date);
  96.       },
  97.     );
  98.   }
  99.  
  100.   void _changeDatetime(int year, int month, int date) {
  101.     setState(() {
  102.       _year = year;
  103.       _month = month;
  104.       _date = date;
  105.       _datetime = '$date-$month-$year';
  106.     });
  107.   }
  108.  
  109.   @override
  110.   Widget build(BuildContext context) {
  111.     return Scaffold(
  112.       appBar: AppBar(
  113.         title: Text('Produk Entry'),
  114.       ),
  115.       body: SafeArea(
  116.           child: ListView(
  117.             padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  118.             children: <Widget>[
  119.               // text field
  120.               TextField(
  121.                 controller: _nomerRakController,
  122.                 decoration: InputDecoration(
  123.                   filled: false,
  124.                   labelText: 'Nomer Rak',
  125.                 ),
  126.               ),
  127.               // spacer
  128.               SizedBox(height: 5.0),
  129.               // text field
  130.               TextField(
  131.                 controller: _namaProdukController,
  132.                 decoration: InputDecoration(
  133.                   filled: false,
  134.                   labelText: 'Nama Produk',
  135.                 ),
  136.               ),
  137.  
  138.               // Dropdown
  139.               new Container(
  140.                 padding: EdgeInsets.all(10.0),
  141.                 //color: Colors.blueGrey,
  142.                 child: new Row(
  143.                   children: <Widget>[
  144.                     new Text("Kategori: ", style: new TextStyle(fontSize: 15.0)),
  145.                     new DropdownButton(
  146.                       value: _currentCategory,
  147.                       items: _dropDownMenuItems,
  148.                       onChanged: changedDropDownItem,
  149.                     )
  150.                   ],
  151.                 ),
  152.               ),
  153.  
  154.               // datepicker
  155.               new Container(
  156.                 //padding: EdgeInsets.all(10.0),
  157.                 //color: Colors.blueGrey,
  158.                 child: new Row(
  159.                   children: <Widget>[
  160.                     RaisedButton(
  161.                       child: Text('Expired Date', style: new TextStyle(fontSize: 15.0)),
  162.                       onPressed: () {
  163.                         _showDatePicker();
  164.                       },
  165.                     ),
  166.                     new Text("  $_datetime", style: new TextStyle(fontSize: 15.0)),
  167.                   ],
  168.                 ),
  169.               ),
  170.  
  171.               // Radio
  172.               new Container(
  173.                 //padding: EdgeInsets.all(10.0),
  174.                 //color: Colors.blueGrey,
  175.                 child: new Row(
  176.                   children: <Widget>[
  177.                     new Radio(
  178.                       value: 0,
  179.                       groupValue: _radioValue1,
  180.                       onChanged: _handleRadioValueChange1,
  181.                     ),
  182.                     new Text(
  183.                       'Discount',
  184.                       style: new TextStyle(fontSize: 15.0),
  185.                     ),
  186.                     new Radio(
  187.                       value: 1,
  188.                       groupValue: _radioValue1,
  189.                       onChanged: _handleRadioValueChange1,
  190.                     ),
  191.                     new Text(
  192.                       'Non Discount',
  193.                       style: new TextStyle(fontSize: 15.0),
  194.                     ),
  195.                     ],
  196.                 ),
  197.               ),
  198.  
  199.               // button
  200.               RaisedButton(
  201.                 child: Text('SIMPAN'),
  202.                 onPressed: () {
  203.                   return showDialog(
  204.                     context: context,
  205.                     builder: (context) {
  206.                       return AlertDialog(
  207.                         // Retrieve the text the user has typed in using our
  208.                         // TextEditingController
  209.                         content: Text('Nomer Rak: ${_nomerRakController.text}\n'+
  210.                             'Produk: ${_namaProdukController.text}\n'+
  211.                             'Kategori: $_currentCategory\n' +
  212.                             'Expired Date: $_datetime\n' +
  213.                             'Radio: $_radioValue1'),
  214.                       );
  215.                     },
  216.                   );
  217.                   },
  218.               ),
  219.             ],
  220.           )
  221.       ),
  222.     );
  223.   }
  224. }
  225.  
  226. void main() => runApp(MyApp());
  227.  
  228. class MyApp extends StatelessWidget {
  229.  
  230.   @override
  231.   Widget build(BuildContext context) {
  232.  
  233.     return MaterialApp(
  234.       title: 'Entry Data Example',
  235.       theme: ThemeData(
  236.         primarySwatch: Colors.blue,
  237.       ),
  238.       home: new EntryData(),
  239.     );
  240.   }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement