NoteViewer: Implement the left/right buttons works

This commit is contained in:
Vishesh Handa
2018-05-25 00:09:07 +02:00
parent 5197a84959
commit 46c0abc525
3 changed files with 73 additions and 44 deletions

View File

@ -1,17 +1,70 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'note.dart'; import 'note.dart';
class NoteViewer extends StatelessWidget { class NoteBrowsingScreen extends StatefulWidget {
final Note note; final List<Note> notes;
final _biggerFont = const TextStyle(fontSize: 18.0); 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<NoteBrowsingScreen> {
int noteIndex;
NoteBrowsingScreenState({@required this.noteIndex});
@override @override
Widget build(BuildContext context) { 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( child: new Column(
children: <Widget>[ children: <Widget>[
_buildHeader(context), _buildHeader(context),
@ -22,13 +75,6 @@ class NoteViewer extends StatelessWidget {
), ),
padding: const EdgeInsets.all(16.0), padding: const EdgeInsets.all(16.0),
); );
return new Scaffold(
appBar: new AppBar(
title: new Text('TIMELINE'),
),
body: bodyWidget,
);
} }
Widget _buildHeader(BuildContext context) { Widget _buildHeader(BuildContext context) {
@ -80,7 +126,7 @@ class NoteViewer extends StatelessWidget {
new IconButton( new IconButton(
icon: new Icon(Icons.arrow_left), icon: new Icon(Icons.arrow_left),
tooltip: 'Previous Entry', tooltip: 'Previous Entry',
onPressed: () {}, onPressed: showPrevNoteFunc,
), ),
new Expanded( new Expanded(
flex: 10, flex: 10,
@ -89,7 +135,7 @@ class NoteViewer extends StatelessWidget {
new IconButton( new IconButton(
icon: new Icon(Icons.arrow_right), icon: new Icon(Icons.arrow_right),
tooltip: 'Next Entry', tooltip: 'Next Entry',
onPressed: () {}, onPressed: showNextNoteFunc,
), ),
], ],
), ),

View File

@ -1,9 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:journal/state_container.dart'; import 'package:journal/state_container.dart';
import 'package:journal/widgets/journal_list.dart'; import 'package:journal/widgets/journal_list.dart';
import 'package:journal/note.dart';
import 'package:journal/note_editor.dart'; import 'package:journal/note_editor.dart';
import 'package:journal/note_viewer.dart'; import 'package:journal/note_viewer.dart';
@ -23,36 +21,21 @@ class HomeScreen extends StatelessWidget {
title: new Text('Journal'), title: new Text('Journal'),
), ),
floatingActionButton: createButton, floatingActionButton: createButton,
/*
body: new FutureBuilder<List<Note>>(
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( body: new JournalList(
notes: appState.notes, 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) { void _newPost(BuildContext context) {
var route = new MaterialPageRoute(builder: (context) => new NoteEditor()); var route = new MaterialPageRoute(builder: (context) => new NoteEditor());
Navigator.of(context).push(route); Navigator.of(context).push(route);

View File

@ -5,7 +5,7 @@ import 'package:intl/intl.dart';
import 'package:journal/note.dart'; import 'package:journal/note.dart';
import 'package:journal/state_container.dart'; import 'package:journal/state_container.dart';
typedef void NoteSelectedFunction(Note note); typedef void NoteSelectedFunction(int noteIndex);
class JournalList extends StatelessWidget { class JournalList extends StatelessWidget {
final NoteSelectedFunction noteSelectedFunction; final NoteSelectedFunction noteSelectedFunction;
@ -33,7 +33,7 @@ class JournalList extends StatelessWidget {
var note = notes[i]; var note = notes[i];
return new Dismissible( return new Dismissible(
key: new Key(note.id), key: new Key(note.id),
child: _buildRow(context, note), child: _buildRow(context, note, i),
background: new Container(color: Colors.red), background: new Container(color: Colors.red),
onDismissed: (direction) { onDismissed: (direction) {
container.removeNote(note); 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 formatter = new DateFormat('dd MMM, yyyy');
var title = formatter.format(journal.createdAt); var title = formatter.format(journal.createdAt);
@ -67,7 +67,7 @@ class JournalList extends StatelessWidget {
style: _biggerFont, style: _biggerFont,
), ),
subtitle: new Text(time + "\n" + body), subtitle: new Text(time + "\n" + body),
onTap: () => noteSelectedFunction(journal), onTap: () => noteSelectedFunction(noteIndex),
); );
} }
} }