Skip to content

Adding null safety #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 117 additions & 142 deletions lib/screens/note_list.dart
Original file line number Diff line number Diff line change
@@ -1,170 +1,145 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_app/models/note.dart';
import 'package:flutter_app/utils/database_helper.dart';
import 'package:flutter_app/screens/note_detail.dart';
import 'package:flutter_apps/models/note.dart';
import 'package:flutter_apps/utils/database_helper.dart';
import 'package:sqflite/sqflite.dart';

import 'note_detail.dart';

class NoteList extends StatefulWidget {

@override
State<StatefulWidget> createState() {

return NoteListState();
}
@override
_NoteListState createState() => _NoteListState();
}

class NoteListState extends State<NoteList> {
class _NoteListState extends State<NoteList> {
int count = 0;
DatabaseHelper databaseHelper = DatabaseHelper();
List<Note>? noteList;

DatabaseHelper databaseHelper = DatabaseHelper();
List<Note> noteList;
int count = 0;

@override
@override
Widget build(BuildContext context) {

if (noteList == null) {
noteList = List<Note>();
updateListView();
}
if (noteList == null) {
noteList = <Note>[];
updateListView();
print("Build");
}

return Scaffold(

appBar: AppBar(
title: Text('Notes'),
),

body: getNoteListView(),

floatingActionButton: FloatingActionButton(
onPressed: () {
debugPrint('FAB clicked');
navigateToDetail(Note('', '', 2), 'Add Note');
},

tooltip: 'Add Note',

child: Icon(Icons.add),

),
appBar: AppBar(
title: Text('Notes'),
),
body: getNoteListView(),
floatingActionButton: FloatingActionButton(
onPressed: () {
print("FAB clicked");
navigateToDetail(Note('', '', 2), 'Add Note');
},
tooltip: 'Add Note',
child: Icon(Icons.add),
),
);
}

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.grey,),
onTap: () {
_delete(context, noteList[position]);
},
),


onTap: () {
debugPrint("ListTile Tapped");
navigateToDetail(this.noteList[position],'Edit Note');
},

),
);
},
);
TextStyle? titleStyle = Theme.of(context).textTheme.subtitle1;

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.grey,
),
onTap: () {
_delete(context, noteList![position]);
},
),
onTap: () {
print("ListTile Tapped");
navigateToDetail(this.noteList![position], 'Edit Note');
},
),
);
},
);
}

// Returns the priority color
Color getPriorityColor(int priority) {
switch (priority) {
case 1:
return Colors.red;
break;
case 2:
return Colors.yellow;
break;

default:
return Colors.yellow;
}
}

// Returns the priority icon
Icon getPriorityIcon(int priority) {
switch (priority) {
case 1:
return Icon(Icons.play_arrow);
break;
case 2:
return Icon(Icons.keyboard_arrow_right);
break;

default:
return Icon(Icons.keyboard_arrow_right);
}
}

void _delete(BuildContext context, Note note) async {
// Colors according to the priority
Color getPriorityColor(int priority) {
switch (priority) {
case 1:
return Colors.red;
case 2:
return Colors.yellow;
default:
return Colors.yellow;
}
}

int result = await databaseHelper.deleteNote(note.id);
if (result != 0) {
_showSnackBar(context, 'Note Deleted Successfully');
updateListView();
}
}
// Icon according to the priority
Icon getPriorityIcon(int priority) {
switch (priority) {
case 1:
return Icon(Icons.play_arrow);
case 2:
return Icon(Icons.keyboard_arrow_right);
default:
return Icon(Icons.keyboard_arrow_right);
}
}

void _showSnackBar(BuildContext context, String message) {
void _delete(BuildContext context, Note note) async {
int result = await databaseHelper.deleteNote(note.id!);
if (result != 0) {
_showSnackBar(context, 'Note Deleted Successfully!');
updateListView();
}
}

final snackBar = SnackBar(content: Text(message));
Scaffold.of(context).showSnackBar(snackBar);
}
void _showSnackBar(BuildContext context, String message) {
final snackBar = SnackBar(content: Text(message));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}

void navigateToDetail(Note note, String title) async {
bool result = await Navigator.push(context, MaterialPageRoute(builder: (context) {
return NoteDetail(note, title);
}));
bool result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return NoteDetail(note, title);
},
),
);

if (result == true) {
updateListView();
}
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;
});
});
});
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;
});
});
});
}
}