Gokulakrishnan

Selection Issue

Dec 29th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 8.96 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import '../model/Note.dart';
  3. import '../database_helper.dart';
  4. import 'package:intl/intl.dart';
  5.  
  6. class NoteDetail extends StatefulWidget {
  7.  
  8.    final String appBarTitle;
  9.    final Note note;
  10.  
  11.    NoteDetail(this.note,this.appBarTitle);
  12.  
  13.    @override
  14.    State<StatefulWidget> createState(){
  15.      return NoteDetailState(this.note, this.appBarTitle);
  16.    }
  17. }
  18.  
  19. class NoteDetailState extends State<NoteDetail> {
  20.   static var _priorities = ["Club Leader","Team Leader",];
  21.   DatabaseHelper helper = DatabaseHelper();
  22.   String appBarTitle;
  23.   Note note;
  24.    
  25.   NoteDetailState(this.note,this.appBarTitle);
  26.  
  27.   //1. Declare some text editing controller
  28.   TextEditingController titleController = TextEditingController();
  29.   TextEditingController descriptionController = TextEditingController();
  30.  
  31.   @override
  32.   Widget build(BuildContext context) {
  33.     //7 provide a textstyle based on apptitlebar and note itself
  34.     TextStyle textStyle = Theme.of(context).textTheme.title;
  35.  
  36.     titleController.text = note.title; //in case user is coming baack and note value is filled up
  37.     descriptionController.text = note.description;
  38.  
  39.     return WillPopScope(
  40.       onWillPop: (){
  41.         moveToLastScreen();
  42.       },
  43.       child: SafeArea(
  44.         child: Scaffold(
  45.           backgroundColor: Colors.grey[400],
  46.           appBar: AppBar(
  47.             title: Text(appBarTitle),
  48.             backgroundColor: Colors.purple[300],
  49.             leading: IconButton(
  50.               icon: Icon(Icons.arrow_back),
  51.               onPressed: (){
  52.                 moveToLastScreen();
  53.               },
  54.             ),
  55.           ),
  56.           body: Padding(
  57.               padding: EdgeInsets.all(10.0),
  58.               child: Card(
  59.                 shape: RoundedRectangleBorder(
  60.                   borderRadius: BorderRadius.circular(20.0),
  61.                 ),
  62.                 child: ListView(
  63.                   children: <Widget>[
  64.                     Padding(
  65.                       padding: EdgeInsets.only(top: 15.0, bottom: 5.0),
  66.                       //dropdown menu
  67.                       child: new ListTile(
  68.                         leading: const Icon(Icons.low_priority),
  69.                         title: DropdownButton(
  70.                             items: _priorities.map((String dropDownStringItem) {
  71.                               return DropdownMenuItem<String>(
  72.                                 value: dropDownStringItem,
  73.                                 child: Text(dropDownStringItem,
  74.                                     style: TextStyle(
  75.                                         fontSize: 20.0,
  76.                                         fontWeight: FontWeight.bold,
  77.                                         color: Colors.red)),
  78.                               );
  79.                             }).toList(),
  80.                             value: getPriorityAsString(note.priority),
  81.                             onChanged: (valueSelectedByUser) {
  82.                               setState(() {
  83.                                 updatePriorityAsInt(valueSelectedByUser);
  84.                               });
  85.                             }),
  86.                       ),
  87.                     ),
  88.                     // Second Element
  89.                     Padding(
  90.                       padding:
  91.                           EdgeInsets.only(top: 15.0, bottom: 15.0, left: 15.0),
  92.                       child: TextField(
  93.                         controller: titleController,
  94.                         style: textStyle,
  95.                         onChanged: (value) {
  96.                           updateTitle();
  97.                         },
  98.                         decoration: InputDecoration(
  99.                           labelText: 'Title',
  100.                           labelStyle: textStyle,
  101.                           icon: Icon(Icons.title),
  102.                         ),
  103.                       ),
  104.                     ),
  105.  
  106.                     // Third Element
  107.                     Padding(
  108.                       padding:
  109.                           EdgeInsets.only(top: 15.0, bottom: 15.0, left: 15.0),
  110.                       child: TextField(
  111.                         controller: descriptionController,
  112.                         style: textStyle,
  113.                         onChanged: (value) {
  114.                           updateDescription();
  115.                         },
  116.                         decoration: InputDecoration(
  117.                           labelText: 'Details',
  118.                           icon: Icon(Icons.details),
  119.                         ),
  120.                       ),
  121.                     ),
  122.  
  123.                     // Fourth Element
  124.                     Padding(
  125.                       padding: EdgeInsets.all(15.0),
  126.                       child: Row(
  127.                         children: <Widget>[
  128.                           Expanded(
  129.                             child: RaisedButton(
  130.                               textColor: Colors.white,
  131.                               color: Colors.green,
  132.                               padding: const EdgeInsets.all(8.0),
  133.                               child: Text(
  134.                                 'Save',
  135.                                 textScaleFactor: 1.5,
  136.                               ),
  137.                               onPressed: () {
  138.                                 setState(() {
  139.                                   debugPrint("Save button clicked");
  140.                                   _save();
  141.                                 });
  142.                               },
  143.                             ),
  144.                           ),
  145.                           Container(
  146.                             width: 5.0,
  147.                           ),
  148.                           Expanded(
  149.                             child: RaisedButton(
  150.                               textColor: Colors.white,
  151.                               color: Colors.red[400],
  152.                               padding: const EdgeInsets.all(8.0),
  153.                               child: Text(
  154.                                 'Delete',
  155.                                 textScaleFactor: 1.5,
  156.                               ),
  157.                               onPressed: () {
  158.                                 setState(() {
  159.                                   _delete();
  160.                                 });
  161.                               },
  162.                             ),
  163.                           ),
  164.                         ],
  165.                       ),
  166.                     ),
  167.                   ],
  168.                 ),
  169.               ),
  170.             ),
  171.         ),
  172.       ),
  173.     );
  174.    
  175.   }
  176.  
  177.   void updateTitle(){
  178.     //user typing in text will be saved in title
  179.     note.title = titleController.text;
  180.   }
  181.   void updateDescription(){
  182.     //user typing in text will be saved in title
  183.     note.description = descriptionController.text;
  184.   }
  185.  
  186.  
  187.  
  188.  
  189.   //3 how u want to save it..first move back to the screen and then save it
  190.   void _save() async{
  191.     moveToLastScreen();
  192.    
  193.     note.date = DateFormat.yMMMd().format(DateTime.now());
  194.     int result;
  195.     //first case is if ide is present it means we are updating the note
  196.     if(note.id!=null){
  197.       result = await helper.updateNote(note);
  198.     }else {
  199.       result = await helper.insertNote(note);
  200.     }
  201.  
  202.     //if result is correctly saved or not
  203.     if(result!=0)
  204.     {
  205.       _showAlertDialog('Status', 'Note Saved Succfully');
  206.     }else{
  207.       _showAlertDialog('Status', 'Problem Saving Note');
  208.     }
  209.  
  210.   }
  211.  
  212.   //4..delete
  213.   void _delete() async{
  214.     moveToLastScreen();
  215.     //below if means that user has clicked it but not doing anything
  216.     if(note.id==null){
  217.      _showAlertDialog('Status', 'First add a note');
  218.      return;
  219.     }
  220.     int result = await helper.deleteNote(note.id);
  221.     //next we neede to check whether result has something or not
  222.     if(result!=0)
  223.     {
  224.       _showAlertDialog('Status', 'Note Deleted Successfully');
  225.     }else{
  226.       _showAlertDialog('Status', 'Problem Deleting Note');
  227.     }
  228.  
  229.   }
  230.  
  231.   //5.. convert to int to save into database
  232.  
  233.   void updatePriorityAsInt(String value){
  234.     switch (value) {
  235.       case 'Club Leader':
  236.         note.priority =1;
  237.         break;
  238.       case 'Team Leader':
  239.         note.priority = 2;
  240.         break;
  241.     }
  242.   }
  243.  
  244.   //6 convert int to string to show user
  245.   //when user is trying to edit it and we need to fetch the value from database ..which we give priority as 1 or 2
  246.   // and we are going to convert 1 and 2 into high adn low
  247.   String getPriorityAsString(int value){
  248.     String priority;
  249.     switch (value) {
  250.       case 1:
  251.         priority = _priorities[0];
  252.         break;
  253.       case 2:
  254.         priority = _priorities[1];
  255.         break;
  256.      
  257.     }
  258.     return priority;
  259.   }
  260.  
  261.   void moveToLastScreen(){
  262.     Navigator.pop(context, true);
  263.   }
  264.  
  265.  
  266.  
  267.  
  268.   //2 create a helper method..
  269.  
  270.   void _showAlertDialog(String title, String message){
  271.     AlertDialog alertDialog = AlertDialog(
  272.       title: Text(title),
  273.       content: Text(message),
  274.     );
  275.  
  276.     showDialog(context: context, builder: (_) => alertDialog);
  277.   }
  278. }
Add Comment
Please, Sign In to add comment