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: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<Note> 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<NoteBrowsingScreen> {
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: <Widget>[
_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,
),
],
),

View File

@ -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<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(
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);

View File

@ -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),
);
}
}