diff --git a/lib/note_viewer.dart b/lib/note_viewer.dart index 7dbd1500..309cbd93 100644 --- a/lib/note_viewer.dart +++ b/lib/note_viewer.dart @@ -1,17 +1,70 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'note.dart'; -class NoteViewer extends StatelessWidget { - final Note note; - final _biggerFont = const TextStyle(fontSize: 18.0); +class NoteBrowsingScreen extends StatefulWidget { + final List notes; + final int noteIndex; - const NoteViewer({this.note}); + const NoteBrowsingScreen({ + @required this.notes, + @required this.noteIndex, + }); + + @override + NoteBrowsingScreenState createState() { + return new NoteBrowsingScreenState(noteIndex: noteIndex); + } +} + +class NoteBrowsingScreenState extends State { + int noteIndex; + + NoteBrowsingScreenState({@required this.noteIndex}); @override Widget build(BuildContext context) { - var bodyWidget = new SingleChildScrollView( + var viewer = new NoteViewer( + note: widget.notes[noteIndex], + showNextNoteFunc: () { + setState(() { + if (noteIndex < widget.notes.length - 1) noteIndex += 1; + }); + }, + showPrevNoteFunc: () { + setState(() { + if (noteIndex > 0) noteIndex -= 1; + }); + }, + ); + + return new Scaffold( + appBar: new AppBar( + title: new Text('TIMELINE'), + ), + body: viewer, + ); + } +} + +class NoteViewer extends StatelessWidget { + final Note note; + final VoidCallback showNextNoteFunc; + final VoidCallback showPrevNoteFunc; + + final _biggerFont = const TextStyle(fontSize: 18.0); + + const NoteViewer({ + @required this.note, + @required this.showNextNoteFunc, + @required this.showPrevNoteFunc, + }); + + @override + Widget build(BuildContext context) { + return new SingleChildScrollView( child: new Column( children: [ _buildHeader(context), @@ -22,13 +75,6 @@ class NoteViewer extends StatelessWidget { ), padding: const EdgeInsets.all(16.0), ); - - return new Scaffold( - appBar: new AppBar( - title: new Text('TIMELINE'), - ), - body: bodyWidget, - ); } Widget _buildHeader(BuildContext context) { @@ -80,7 +126,7 @@ class NoteViewer extends StatelessWidget { new IconButton( icon: new Icon(Icons.arrow_left), tooltip: 'Previous Entry', - onPressed: () {}, + onPressed: showPrevNoteFunc, ), new Expanded( flex: 10, @@ -89,7 +135,7 @@ class NoteViewer extends StatelessWidget { new IconButton( icon: new Icon(Icons.arrow_right), tooltip: 'Next Entry', - onPressed: () {}, + onPressed: showNextNoteFunc, ), ], ), diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index 000b57a0..1f9ae055 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -1,9 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:flutter/foundation.dart'; import 'package:journal/state_container.dart'; import 'package:journal/widgets/journal_list.dart'; -import 'package:journal/note.dart'; import 'package:journal/note_editor.dart'; import 'package:journal/note_viewer.dart'; @@ -23,36 +21,21 @@ class HomeScreen extends StatelessWidget { title: new Text('Journal'), ), floatingActionButton: createButton, - /* - body: new FutureBuilder>( - future: fetchNotes(), - builder: (context, snapshot) { - if (snapshot.hasData) { - var notes = snapshot.data; - return new JournalList( - notes: notes, - noteSelectedFunction: (note) => _noteSelected(note, context), - ); - } else if (snapshot.hasError) { - return new Text("${snapshot.error}"); - } - - return new CircularProgressIndicator(); - }), - */ body: new JournalList( notes: appState.notes, - noteSelectedFunction: (note) => _noteSelected(note, context), + noteSelectedFunction: (noteIndex) { + var route = new MaterialPageRoute( + builder: (context) => new NoteBrowsingScreen( + notes: appState.notes, + noteIndex: noteIndex, + ), + ); + Navigator.of(context).push(route); + }, ), ); } - void _noteSelected(Note note, BuildContext context) { - var route = - new MaterialPageRoute(builder: (context) => new NoteViewer(note: note)); - Navigator.of(context).push(route); - } - void _newPost(BuildContext context) { var route = new MaterialPageRoute(builder: (context) => new NoteEditor()); Navigator.of(context).push(route); diff --git a/lib/widgets/journal_list.dart b/lib/widgets/journal_list.dart index 0ffc9036..eb484f5c 100644 --- a/lib/widgets/journal_list.dart +++ b/lib/widgets/journal_list.dart @@ -5,7 +5,7 @@ import 'package:intl/intl.dart'; import 'package:journal/note.dart'; import 'package:journal/state_container.dart'; -typedef void NoteSelectedFunction(Note note); +typedef void NoteSelectedFunction(int noteIndex); class JournalList extends StatelessWidget { final NoteSelectedFunction noteSelectedFunction; @@ -33,7 +33,7 @@ class JournalList extends StatelessWidget { var note = notes[i]; return new Dismissible( key: new Key(note.id), - child: _buildRow(context, note), + child: _buildRow(context, note, i), background: new Container(color: Colors.red), onDismissed: (direction) { container.removeNote(note); @@ -47,7 +47,7 @@ class JournalList extends StatelessWidget { ); } - Widget _buildRow(BuildContext context, Note journal) { + Widget _buildRow(BuildContext context, Note journal, int noteIndex) { var formatter = new DateFormat('dd MMM, yyyy'); var title = formatter.format(journal.createdAt); @@ -67,7 +67,7 @@ class JournalList extends StatelessWidget { style: _biggerFont, ), subtitle: new Text(time + "\n" + body), - onTap: () => noteSelectedFunction(journal), + onTap: () => noteSelectedFunction(noteIndex), ); } }