Files
notes-app/lib/screens/note_list.dart
2019-05-09 16:56:33 +05:45

223 lines
6.7 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:notes_app/db_helper/db_helper.dart';
import 'package:notes_app/modal_class/notes.dart';
import 'package:notes_app/screens/note_detail.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:notes_app/utils/widgets.dart';
import 'package:sqflite/sqflite.dart';
class NoteList extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return NoteListState();
}
}
class NoteListState extends State<NoteList> {
DatabaseHelper databaseHelper = DatabaseHelper();
List<Note> noteList;
int count = 0;
int axisCount = 2;
@override
Widget build(BuildContext context) {
if (noteList == null) {
noteList = List<Note>();
updateListView();
}
return Scaffold(
appBar: AppBar(
title: Text('Notes', style: Theme.of(context).textTheme.headline),
centerTitle: true,
elevation: 0,
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(
Icons.menu,
color: Colors.black,
),
onPressed: () {},
),
actions: <Widget>[
IconButton(
icon: Icon(
axisCount == 2 ? Icons.list : Icons.grid_on,
color: Colors.black,
),
onPressed: () {
setState(() {
axisCount = axisCount == 2 ? 4 : 2;
});
},
)
],
),
body: noteList.length == 0
? Container(
color: Colors.white,
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text('Click on the add button to add a new note!',
style: Theme.of(context).textTheme.body1),
),
),
)
: getNotesList(),
floatingActionButton: FloatingActionButton(
onPressed: () {
navigateToDetail(Note('', '', 3, 0), 'Add Note');
},
tooltip: 'Add Note',
shape: CircleBorder(side: BorderSide(color: Colors.black, width: 2.0)),
child: Icon(Icons.add, color: Colors.black),
backgroundColor: Colors.white,
),
);
}
Widget getNotesList() {
return StaggeredGridView.countBuilder(
physics: BouncingScrollPhysics(),
crossAxisCount: 4,
itemCount: count,
itemBuilder: (BuildContext context, int index) => GestureDetector(
onTap: () {
navigateToDetail(this.noteList[index], 'Edit Note');
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: colors[this.noteList[index].color],
border: Border.all(width: 2, color: Colors.black),
borderRadius: BorderRadius.circular(8.0)),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
this.noteList[index].title,
style: Theme.of(context).textTheme.body1,
),
),
),
Text(
getPriorityText(this.noteList[index].priority),
style: TextStyle(
color: getPriorityColor(
this.noteList[index].priority)),
),
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: Text(
this.noteList[index].description == null
? ''
: this.noteList[index].description,
style: Theme.of(context).textTheme.body2),
)
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(this.noteList[index].date,
style: Theme.of(context).textTheme.subtitle),
])
],
),
),
),
),
staggeredTileBuilder: (int index) => StaggeredTile.fit(axisCount),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
);
}
// Returns the priority color
Color getPriorityColor(int priority) {
switch (priority) {
case 1:
return Colors.red;
break;
case 2:
return Colors.yellow;
break;
case 3:
return Colors.green;
break;
default:
return Colors.yellow;
}
}
// Returns the priority icon
String getPriorityText(int priority) {
switch (priority) {
case 1:
return '!!!';
break;
case 2:
return '!!';
break;
case 3:
return '!';
break;
default:
return '!';
}
}
// void _delete(BuildContext context, Note note) async {
// int result = await databaseHelper.deleteNote(note.id);
// if (result != 0) {
// _showSnackBar(context, 'Note Deleted Successfully');
// updateListView();
// }
// }
// void _showSnackBar(BuildContext context, String message) {
// final snackBar = SnackBar(content: Text(message));
// Scaffold.of(context).showSnackBar(snackBar);
// }
void navigateToDetail(Note note, String title) async {
bool result = await Navigator.push(context,
MaterialPageRoute(builder: (context) => NoteDetail(note, title)));
if (result == true) {
updateListView();
}
}
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;
});
});
});
}
}