Advertisement
mrblab24

upload_place.dart

Dec 19th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 11.41 KB | None | 0 0
  1. import 'package:admin/blocs/admin_bloc.dart';
  2. import 'package:admin/utils/dialog.dart';
  3. import 'package:admin/utils/styles.dart';
  4. import 'package:admin/widgets/place_preview.dart';
  5. import 'package:cloud_firestore/cloud_firestore.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:intl/intl.dart';
  8. import 'package:provider/provider.dart';
  9.  
  10. class UploadPlace extends StatefulWidget {
  11.   UploadPlace({Key key}) : super(key: key);
  12.  
  13.   @override
  14.   _UploadPlaceState createState() => _UploadPlaceState();
  15. }
  16.  
  17. class _UploadPlaceState extends State<UploadPlace> {
  18.  
  19.   final FirebaseFirestore firestore = FirebaseFirestore.instance;
  20.  
  21.   var formKey = GlobalKey<FormState>();
  22.   var scaffoldKey = GlobalKey<ScaffoldState>();
  23.   final String collectionName = 'places';
  24.   List paths =[];
  25.   final int _loves = 0;
  26.   final int _commentsCount = 0;
  27.   String _date;
  28.   String _timestamp;
  29.  
  30.  
  31.   var stateSelection;
  32.  
  33.   var nameCtrl = TextEditingController();
  34.   var locationCtrl = TextEditingController();
  35.   var descriptionCtrl = TextEditingController();
  36.   var image1Ctrl = TextEditingController();
  37.   var image2Ctrl = TextEditingController();
  38.   var image3Ctrl = TextEditingController();
  39.   var latCtrl = TextEditingController();
  40.   var lngCtrl = TextEditingController();
  41.  
  42.  
  43.  
  44.  
  45.   clearFields(){
  46.     nameCtrl.clear();
  47.     locationCtrl.clear();
  48.     descriptionCtrl.clear();
  49.     image1Ctrl.clear();
  50.     image2Ctrl.clear();
  51.     image3Ctrl.clear();
  52.     latCtrl.clear();
  53.     lngCtrl.clear();
  54.     FocusScope.of(context).unfocus();
  55.   }
  56.  
  57.  
  58.  
  59.  
  60.   bool notifyUsers = true;
  61.   bool uploadStarted = false;
  62.  
  63.  
  64.  
  65.  
  66.   void handleSubmit() async {
  67.     final AdminBloc ab = Provider.of<AdminBloc>(context, listen: false);
  68.  
  69.     if(stateSelection == null){
  70.       openDialog(context, 'Select State First', '');
  71.     }
  72.     else{
  73.       if (formKey.currentState.validate()) {
  74.       formKey.currentState.save();
  75.       if (ab.userType == 'tester') {
  76.         openDialog(context, 'You are a Tester', 'Only Admin can upload, delete & modify contents');
  77.       } else {
  78.         setState(()=> uploadStarted = true);
  79.         await getDate().then((_) async{
  80.           await saveToDatabase()
  81.           .then((value) => context.read<AdminBloc>().increaseCount('places_count'));
  82.           setState(()=> uploadStarted = false);
  83.           openDialog(context, 'Uploaded Successfully', '');
  84.           clearFields();
  85.          
  86.          
  87.         });
  88.       }
  89.      
  90.     }
  91.     }
  92.    
  93.   }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.   Future getDate() async {
  102.     DateTime now = DateTime.now();
  103.     String _d = DateFormat('dd MMMM yy').format(now);
  104.     String _t = DateFormat('yyyyMMddHHmmss').format(now);
  105.     setState(() {
  106.       _timestamp = _t;
  107.       _date = _d;
  108.     });
  109.    
  110.   }
  111.  
  112.  
  113.  
  114.   Future saveToDatabase() async {
  115.     final DocumentReference ref = firestore.collection(collectionName).doc(_timestamp);
  116.    
  117.  
  118.     var _placeData = {
  119.       'state' : stateSelection,
  120.       'place name' : nameCtrl.text,
  121.       'location' : locationCtrl.text,
  122.       'latitude' : double.parse(latCtrl.text),
  123.       'longitude' : double.parse(lngCtrl.text),
  124.       'description' : descriptionCtrl.text,
  125.       'image-1' : image1Ctrl.text,
  126.       'image-2' : image2Ctrl.text,
  127.       'image-3' : image3Ctrl.text,
  128.       'loves' : _loves,
  129.       'comments count' : _commentsCount,
  130.       'date' : _date,
  131.       'timestamp' : _timestamp
  132.     };
  133.  
  134.     await ref.set(_placeData);
  135.   }
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.   handlePreview() async{
  143.     if (formKey.currentState.validate()) {
  144.       formKey.currentState.save();
  145.       showPlacePreview(
  146.           context,
  147.           nameCtrl.text,
  148.           locationCtrl.text,
  149.           image1Ctrl.text,
  150.           descriptionCtrl.text,
  151.           double.parse(latCtrl.text),
  152.           double.parse(lngCtrl.text),
  153.         );
  154.     }
  155.   }
  156.  
  157.  
  158.  
  159.   @override
  160.   Widget build(BuildContext context) {
  161.     double h = MediaQuery.of(context).size.height;
  162.     return Scaffold(
  163.       key: scaffoldKey,
  164.       body: Form(
  165.             key: formKey,
  166.             child: ListView(children: <Widget>[
  167.               SizedBox(height: h * 0.10,),
  168.               Text('Place Details', style: TextStyle(
  169.                 fontSize: 30, fontWeight: FontWeight.w800
  170.               ),),
  171.               SizedBox(height: 20,),
  172.               statesDropdown(),
  173.               SizedBox(height: 20,),
  174.               TextFormField(
  175.                 decoration: inputDecoration('Enter place name', 'Place name', nameCtrl),
  176.                 controller: nameCtrl,
  177.                 validator: (value){
  178.                   if(value.isEmpty) return 'Value is empty'; return null;
  179.                 },
  180.                
  181.               ),
  182.               SizedBox(height: 20,),
  183.               TextFormField(
  184.                 decoration: inputDecoration('Enter location name', 'Location name', locationCtrl),
  185.                 controller: locationCtrl,
  186.                
  187.                 validator: (value){
  188.                   if(value.isEmpty) return 'Value is empty'; return null;
  189.                 },
  190.                
  191.               ),
  192.               SizedBox(height: 20,),
  193.              
  194.  
  195.               Row(
  196.                 children: <Widget>[
  197.                   Expanded(
  198.                 child: TextFormField(
  199.                 decoration: inputDecoration('Enter Latitude', 'Latitude', latCtrl),
  200.                 controller: latCtrl,
  201.                 keyboardType: TextInputType.number,
  202.                 validator: (value){
  203.                     if(value.isEmpty) return 'Value is empty'; return null;
  204.                 },
  205.                
  206.               ),
  207.             ),
  208.               SizedBox(width: 10,),
  209.               Expanded(
  210.                             child: TextFormField(
  211.                   decoration: inputDecoration('Enter Longitude', 'Longitude', lngCtrl),
  212.                   keyboardType: TextInputType.number,
  213.  
  214.                   controller: lngCtrl,
  215.                   validator: (value){
  216.                     if(value.isEmpty) return 'Value is empty'; return null;
  217.                   },
  218.                  
  219.                 ),
  220.               ),
  221.              
  222.                 ],
  223.               ),
  224.               SizedBox(height: 20,),
  225.  
  226.  
  227.               TextFormField(
  228.                 decoration: inputDecoration('Enter image url (thumbnail)', 'Image1(Thumbnail)', image1Ctrl),
  229.                 controller: image1Ctrl,
  230.                 validator: (value){
  231.                   if(value.isEmpty) return 'Value is empty'; return null;
  232.                 },
  233.                
  234.               ),
  235.               SizedBox(height: 20,),
  236.               TextFormField(
  237.                 decoration: inputDecoration('Enter image url', 'Image2', image2Ctrl),
  238.                 controller: image2Ctrl,
  239.                 validator: (value){
  240.                   if(value.isEmpty) return 'Value is empty'; return null;
  241.                 },
  242.                
  243.               ),
  244.               SizedBox(height: 20,),
  245.               TextFormField(
  246.                 decoration: inputDecoration('Enter image url', 'Image3', image3Ctrl),
  247.                 controller: image3Ctrl,
  248.                 validator: (value){
  249.                   if(value.isEmpty) return 'Value is empty'; return null;
  250.                 },
  251.                
  252.               ),
  253.               SizedBox(height: 20,),
  254.               TextFormField(
  255.                 decoration: InputDecoration(
  256.                   hintText: 'Enter place details (Html or Normal Text)',
  257.                   border: OutlineInputBorder(),
  258.                   labelText: 'Place details',
  259.                   contentPadding: EdgeInsets.only(right: 0, left: 10, top: 15, bottom: 5),
  260.                   suffixIcon: Padding(
  261.                     padding: const EdgeInsets.all(8.0),
  262.                     child: CircleAvatar(
  263.                       radius: 15,
  264.                       backgroundColor: Colors.grey[300],
  265.                       child: IconButton(icon: Icon(Icons.close, size: 15), onPressed: (){
  266.                         descriptionCtrl.clear();
  267.                       }),
  268.                     ),
  269.                   )
  270.                  
  271.                 ),
  272.                 textAlignVertical: TextAlignVertical.top,
  273.                 minLines: 5,
  274.                 maxLines: null,
  275.                 keyboardType: TextInputType.multiline,
  276.                 controller: descriptionCtrl,
  277.                 validator: (value){
  278.                   if(value.isEmpty) return 'Value is empty'; return null;
  279.                 },
  280.                
  281.               ),
  282.              
  283.  
  284.  
  285.               SizedBox(height: 100,),
  286.  
  287.  
  288.                   Row(
  289.                       mainAxisAlignment: MainAxisAlignment.end,
  290.                       children: <Widget>[
  291.                         FlatButton.icon(
  292.                          
  293.                           icon: Icon(Icons.remove_red_eye, size: 25, color: Colors.blueAccent,),
  294.                           label: Text('Preview', style: TextStyle(
  295.                             fontWeight: FontWeight.w400,
  296.                             color: Colors.black
  297.                           ),),
  298.                           onPressed: (){
  299.                             handlePreview();
  300.                           }
  301.                         )
  302.                       ],
  303.                     ),
  304.                 SizedBox(
  305.                   height: 10,
  306.                 ),
  307.                 Container(
  308.                     color: Colors.deepPurpleAccent,
  309.                     height: 45,
  310.                     child: uploadStarted == true
  311.                       ? Center(child: Container(height: 30, width: 30,child: CircularProgressIndicator()),)
  312.                       : FlatButton(
  313.                         child: Text(
  314.                           'Upload Place Data',
  315.                           style: TextStyle(
  316.                               color: Colors.white,
  317.                               fontSize: 16,
  318.                               fontWeight: FontWeight.w600),
  319.                         ),
  320.                         onPressed: () async{
  321.                           handleSubmit();
  322.                          
  323.                         })
  324.                      
  325.                       ),
  326.                 SizedBox(
  327.                   height: 200,
  328.                 ),
  329.              
  330.              
  331.  
  332.             ],)),
  333.       );
  334.        
  335.    }
  336.  
  337.  
  338.  
  339.   Widget statesDropdown() {
  340.     final AdminBloc ab = Provider.of(context, listen: false);
  341.     return Container(
  342.         height: 50,
  343.         padding: EdgeInsets.only(left: 15, right: 15),
  344.         decoration: BoxDecoration(
  345.             color: Colors.grey[200],
  346.             border: Border.all(color: Colors.grey[300]),
  347.             borderRadius: BorderRadius.circular(30)),
  348.         child: DropdownButtonFormField(
  349.             itemHeight: 50,
  350.             style: TextStyle(
  351.                 fontSize: 14,
  352.                 color: Colors.grey[800],
  353.                 fontFamily: 'Poppins',
  354.                 fontWeight: FontWeight.w500),
  355.             decoration: InputDecoration(border: InputBorder.none),
  356.             onChanged: (value) {
  357.               setState(() {
  358.                 stateSelection = value;
  359.               });
  360.             },
  361.             onSaved: (value) {
  362.               setState(() {
  363.                 stateSelection = value;
  364.               });
  365.             },
  366.             value: stateSelection,
  367.             hint: Text('Select State'),
  368.             items: ab.states.map((f) {
  369.               return DropdownMenuItem(
  370.                 child: Text(f),
  371.                 value: f,
  372.               );
  373.             }).toList()));
  374.   }
  375.  
  376.  
  377. }
  378.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement