12345
12345
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:catatan/layout/models/note.dart';
import 'package:catatan/layout/utlis/dabase_helper.dart';
import 'package:intl/intl.dart';
NoteDetailState(this.note, this.appBarTitle);
@override
Widget build(BuildContext context) {
TextStyle textStyle = Theme.of(context).textTheme.title;
titleController.text = note.title;
descriptionController.text = note.description;
dateController.text = note.date;
return WillPopScope(
onWillPop: () {
// Write some code to control things, when user press Back navigation
button in device navigationBar
moveToLastScreen();
},
child: Scaffold(
appBar: AppBar(
title: Text(appBarTitle),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
// Write some code to control things, when user press back button
in AppBar
moveToLastScreen();
}),
),
body: Padding(
padding: EdgeInsets.only(top: 15.0, left: 10.0, right: 10.0),
child: ListView(
children: <Widget>[
// First element
ListTile(
title: DropdownButton(
items: _priorities.map((String dropDownStringItem) {
return DropdownMenuItem<String>(
value: dropDownStringItem,
child: Text(dropDownStringItem),
);
}).toList(),
style: textStyle,
value: getPriorityAsString(note.priority),
onChanged: (valueSelectedByUser) {
setState(() {
debugPrint('User selected $valueSelectedByUser');
updatePriorityAsInt(valueSelectedByUser);
});
}),
),
// Second Element
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
child: TextField(
controller: titleController,
style: textStyle,
onChanged: (value) {
debugPrint('Something changed in Title Text Field');
updateTitle();
},
decoration: InputDecoration(
labelText: 'Npm',
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
),
),
// Third Element
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
child: TextField(
controller: descriptionController,
style: textStyle,
onChanged: (value) {
debugPrint('Something changed in Description Text Field');
updateDescription();
},
decoration: InputDecoration(
labelText: 'Nama',
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
),
),
//fourth Element
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
child: TextField(
controller: telpController,
style: textStyle,
onChanged: (value) {
debugPrint('Something changed in Description Text Field');
updateTelp();
},
decoration: InputDecoration(
labelText: 'Telepon',
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
),
),
//fiveth Elemenet
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
child: TextField(
controller: dateController,
style: textStyle,
onChanged: (value) {
debugPrint('Something changed in Description Text Field');
updateDate();
},
decoration: InputDecoration(
labelText: 'Tanggal lahir',
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
),
),
//sixth Element
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
child: TextField(
controller: almtController,
style: textStyle,
onChanged: (value) {
debugPrint('Something changed in Description Text Field');
updateAlmt();
},
decoration: InputDecoration(
labelText: 'Alamat',
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
),
),
//seventh Element
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
child: TextField(
controller: emailController,
style: textStyle,
onChanged: (value) {
debugPrint('Something changed in Description Text Field');
updateEmail();
},
decoration: InputDecoration(
labelText: 'Email',
labelStyle: textStyle,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
),
),
// Fourth Element
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
child: Row(
children: <Widget>[
Expanded(
child: RaisedButton(
color: Theme.of(context).primaryColorDark,
textColor: Theme.of(context).primaryColorLight,
child: Text(
'SIMPAN',
textScaleFactor: 1.5,
),
onPressed: () {
setState(() {
debugPrint("Save button di klik");
_save();
});
},
),
),
Container(
width: 5.0,
),
Expanded(
child: RaisedButton(
color: Theme.of(context).primaryColorDark,
textColor: Theme.of(context).primaryColorLight,
child: Text(
'HAPUS',
textScaleFactor: 1.5,
),
onPressed: () {
setState(() {
debugPrint("Button Hapus Di Klik");
_delete();
});
},
),
),
],
),
),
],
),
),
));
}
void moveToLastScreen() {
Navigator.pop(context, true);
}
void updateDate() {
note.date = dateController.text;
}
void updateTelp() {
note.date = telpController.text;
}
void updateAlmt() {
note.date = almtController.text;
}
void updateEmail() {
note.date = emailController.text;
}
import 'dart:async';
import 'package:catatan/layout/models/note.dart';
import 'package:flutter/material.dart';
import 'package:catatan/layout/models/note.dart';
import 'package:catatan/layout/utlis/dabase_helper.dart';
import 'package:catatan/layout/note_detail.dart';
import 'package:sqflite/sqflite.dart';
ListView getNoteListView() {
TextStyle titleStyle = Theme.of(context).textTheme.subhead;
return ListView.builder(
itemCount: count,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.white,
elevation: 2.0,
child: ListTile(
leading: CircleAvatar(
backgroundColor:
getPriorityColor(this.noteList[position].priority),
child: getPriorityIcon(this.noteList[position].priority),
),
title: Text(
this.noteList[position].title,
style: titleStyle,
),
subtitle: Text(this.noteList[position].date),
trailing: GestureDetector(
child: Icon(
Icons.delete,
color : Colors.orange,
),
onTap: () {
_delete(context, noteList[position]);
},
),
onTap: () {
debugPrint("ListTile Tapped");
navigateToDetail(this.noteList[position], 'Edit Note');
},
),
);
},
);
}
// Returns the priority color
Color getPriorityColor(int priority) {
switch (priority) {
case 1:
return Colors.white;
break;
case 2:
return Colors.green;
break;
default:
return Colors.yellow;
}
}
void updateListView() {
final Future<Database> dbFuture = databaseHelper.initializeDatabase();
dbFuture.then((database) {
Future<List<Note>> noteListFuture = databaseHelper.getNoteList();
noteListFuture.then((noteList) {
setState(() {
this.noteList = noteList;
this.count = noteList.length;
});
});
});
}
}
note dart
class Note {
int _id;
String _title;
String _description;
String _date;
int _priority;
Note(this._title, this._date, this._priority, [this._description]);
Note.withId(this._id, this._title, this._date, this._priority,
[this._description]);
int get id => _id;
String get title => _title;
String get description => _description;
int get priority => _priority;
String get date => _date;
set title(String newTitle) {
if (newTitle.length <= 255) {
this._title = newTitle;
}
}
main dart
import 'package:catatan/layout/note_list.dart';
import 'package:flutter/material.dart';
import 'package:catatan/layout/note_detail.dart';
void main() {
runApp(MyApp());
}
import 'package:catatan/layout/models/note.dart';
import 'package:catatan/layout/note_list.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:catatan/main.dart';
class DatabaseHelper {
static DatabaseHelper _databaseHelper; // Singleton DatabaseHelper
static Database _database; // Singleton Database
String noteTable = 'note_table';
String colId = 'id';
String colTitle = 'title';
String colDescription = 'description';
String colPriority = 'priority';
String colDate = 'date';
DatabaseHelper._createInstance(); // Named constructor to create instance of
DatabaseHelper
factory DatabaseHelper() {
if (_databaseHelper == null) {
_databaseHelper = DatabaseHelper._createInstance(); // This is executed only once,
singleton object
}
return _databaseHelper;
}
Future<Database> get database async {
if (_database == null) {
_database = await initializeDatabase();
}
return _database;
}
Future<Database> initializeDatabase() async {
// Get the directory path for both Android and iOS to store database.
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + 'notes.db';
// Open/create the database at a given path
var notesDatabase = await openDatabase(path, version: 1, onCreate: _createDb)
;
return notesDatabase;
}
void _createDb(Database db, int newVersion) async {
await db.execute('CREATE TABLE $noteTable($colId INTEGER PRIMARY KEY
AUTOINCREMENT, $colTitle TEXT, '
'$colDescription TEXT, $colPriority INTEGER, $colDate TEXT)');
}
// Fetch Operation: Get all note objects from database
Future<List<Map<String, dynamic>>> getNoteMapList() async {
Database db = await this.database;
// var result = await db.rawQuery('SELECT * FROM $noteTable order by $colPriority
ASC');
var result = await db.query(noteTable, orderBy: '$colPriority ASC');
return result;
}
// Insert Operation: Insert a Note object to database
Future<int> insertNote(Note note) async {
Database db = await this.database;
var result = await db.insert(noteTable, note.toMap());
return result;
}
// Update Operation: Update a Note object and save it to database
Future<int> updateNote(Note note) async {
var db = await this.database;
var result = await db.update(noteTable, note.toMap(), where: '$colId = ?',
whereArgs: [note.id]);
return result;
}
// Delete Operation: Delete a Note object from database
Future<int> deleteNote(int id) async {
var db = await this.database;
int result = await db.rawDelete('DELETE FROM $noteTable WHERE $colId = $id');
return result;
}
// Get number of Note objects in database
Future<int> getCount() async {
Database db = await this.database;
List<Map<String, dynamic>> x = await db.rawQuery('SELECT COUNT (*) from
$noteTable');
int result = Sqflite.firstIntValue(x);
return result;
}
// Get the 'Map List' [ List<Map> ] and convert it to 'Note List' [ List<Note>]
Future<List<Note>> getNoteList() async {
var noteMapList = await getNoteMapList(); // Get 'Map List' from database
int count = noteMapList.length; // Count the number of map entries indb table
List<Note> noteList = List<Note>();
// For loop to create a 'Note List' from a 'Map List'
for (int i = 0; i < count; i++) {
noteList.add(Note.fromMapObject(noteMapList[i]));
}
return noteList;
}
}