123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- import 'package:flutter/material.dart';
- import 'package:flutter/widgets.dart';
- import 'package:trpp/data/data.dart';
- import 'package:trpp/data/theme.dart';
- import 'package:font_awesome_flutter/font_awesome_flutter.dart';
- import 'package:trpp/widgets/custom_alert_dialog.dart';
- import 'package:trpp/widgets/toolbar.dart';
- import 'note.dart';
- import 'settings.dart';
- class HomeScreen extends StatefulWidget {
- HomeScreen({Key key}) : super(key: key);
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<HomeScreen> {
- TupleTheme theme;
- List<NotesModel> notesList = [];
- List<NotificationModel> notificationList = [];
- bool _inDeletion = false;
- @override
- void initState() {
- super.initState();
- NotesDatabaseService.db.init();
- setNotesFromDB();
- setNotificationFormDB();
- setLocalTheme();
- }
- setLocalTheme() async {
- theme = (await getCurrentTheme());
- }
- setNotesFromDB() async {
- if (!_inDeletion) {
- var fetchedNotes = await NotesDatabaseService.db.getNotesFromDB();
- notesList = fetchedNotes;
- } else
- print("Skip notes list update due to deletion");
- setState(() {});
- }
- setNotificationFormDB() async {
- var fetchedNotes = await NotesDatabaseService.db.getNotificationsFromDB();
- notificationList = fetchedNotes;
- deleteBadNotifications();
- setState(() {});
- }
- deleteBadNotifications() async {
- List<NotificationModel> badGuys = List<NotificationModel>();
- for (int i = 0; i < notificationList.length; ++i) {
- if (DateTime.parse(notificationList[i].date1).isBefore(DateTime.now())) {
- NotesDatabaseService.db.deleteNotificationInDB(notificationList[i]);
- badGuys.add(notificationList[i]);
- }
- }
- for (int i = 0; i < badGuys.length; ++i)
- notificationList.remove(badGuys[i]);
- if (badGuys.isNotEmpty) setState(() {});
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: SafeArea(
- child: Column(
- children: <Widget>[
- //HomeAppBar(),
- CustomToolbar(
- needBackBtn: false,
- title: "Home",
- icon: FontAwesomeIcons.cog,
- onPressed: openSettings,
- ),
- CustomListView(
- notesList: notesList,
- openNote: openNote,
- onDismissed: dismissNote,
- notificationList: notificationList,
- ),
- ],
- ),
- ),
- floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
- floatingActionButton: Padding(
- padding: EdgeInsets.only(right: 8, bottom: 46),
- child: FloatingActionButton(
- child: Icon(FontAwesomeIcons.plus),
- onPressed: () => openNote(NOTESCREEN_MODE_EDIT, null, true, null)),
- ),
- backgroundColor: Theme.of(context).backgroundColor,
- );
- }
- void openSettings() {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => SettingsScreen(
- title: theme,
- )));
- }
- void openNote(bool mode, NotesModel nm, bool isNew,
- NotificationModel notificationModel) async {
- final res = await Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => AddNoteScreen(
- oldNm: nm,
- isNew: isNew,
- mode: mode,
- notificationModel: notificationModel)));
- if (res != null && res) {
- setNotesFromDB();
- setNotificationFormDB();
- }
- }
- dismissNote(NotesModel nm) async {
- _inDeletion = true;
- _inDeletion = !(await NotesDatabaseService.db.deleteNoteInDB(nm));
- notesList.remove(nm);
- setState(() {});
- }
- }
- class CustomDismissible extends StatelessWidget {
- CustomDismissible(
- {Key key,
- this.index,
- this.nm,
- this.openNote,
- this.onDismissed,
- this.notificationModel})
- : super(key: key);
- final int index;
- final NotesModel nm;
- final NotificationModel notificationModel;
- final Function openNote;
- final Function onDismissed;
- @override
- Widget build(BuildContext context) {
- return Dismissible(
- key: ValueKey(index),
- direction: DismissDirection.endToStart,
- child: Card(child: NoteListItem(index, nm, openNote, notificationModel)),
- background: Padding(
- padding: EdgeInsets.only(right: 30),
- child: Align(
- alignment: Alignment.centerRight,
- child: Icon(FontAwesomeIcons.trashAlt,
- color: Colors.red.shade500, size: 28),
- ),
- ),
- onDismissed: (direction) {
- onDismissed(nm);
- },
- confirmDismiss: (direction) => showDialog(
- context: context, builder: (context) => CustomAlertDialog()),
- );
- }
- }
- class CustomListView extends StatelessWidget {
- final List<NotesModel> notesList;
- final List<NotificationModel> notificationList;
- final Function openNote;
- final Function onDismissed;
- CustomListView(
- {Key key,
- this.notesList,
- this.openNote,
- this.onDismissed,
- this.notificationList})
- : super(key: key);
- NotificationModel getModel(int index) {
- for (int i = 0; i < notificationList.length; ++i)
- if (notificationList[i].note == notesList[index].id)
- return notificationList[i];
- return null;
- }
- @override
- Widget build(BuildContext context) {
- return Flexible(
- child: ListView.builder(
- padding: EdgeInsets.all(10),
- physics: BouncingScrollPhysics(),
- itemCount: notesList.length,
- itemBuilder: (context, index) => CustomDismissible(
- index: index,
- nm: notesList[index],
- openNote: openNote,
- onDismissed: onDismissed,
- notificationModel: getModel(index),
- ),
- ),
- );
- }
- }
- class NoteListItem extends StatelessWidget {
- NoteListItem(this.index, this.nm, this.openNote, this.notificationModel);
- final int index;
- final NotesModel nm;
- final NotificationModel notificationModel;
- final Function openNote;
- String getText() {
- if (notificationModel != null)
- return notificationModel.getString();
- else
- return "";
- }
- @override
- Widget build(BuildContext context) {
- return ListTile(
- title: Text(
- nm.getTitleFromModel(16),
- maxLines: 2,
- overflow: TextOverflow.ellipsis,
- style: TextStyle(
- fontSize: 19,
- fontWeight: FontWeight.w500,
- color: Theme.of(context).primaryColor,
- ),
- ),
- subtitle: Padding(
- padding: const EdgeInsets.only(top: 8),
- child: Text(
- nm.getShortDesc(24),
- overflow: TextOverflow.ellipsis,
- maxLines: 1,
- style: TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.w300,
- color: Theme.of(context).primaryColor,
- ),
- ),
- ),
- trailing: Text(
- getText(),
- style: TextStyle(
- fontSize: 12,
- fontWeight: FontWeight.w400,
- color: Theme.of(context).accentColor,
- ),
- ),
- onTap: () => openNote(NOTESCREEN_MODE_VIEW, nm, false, notificationModel),
- contentPadding: EdgeInsets.all(17),
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
- tileColor: Theme.of(context).cardColor,
- );
- }
- }
|