mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 02:07:39 +08:00
Load the notes from the server
This commit is contained in:
@ -1,33 +1,52 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
class Journal {
|
import 'package:flutter/material.dart';
|
||||||
final DateTime creationDateTime;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
|
class Note {
|
||||||
|
final DateTime createdAt;
|
||||||
final String body;
|
final String body;
|
||||||
|
|
||||||
const Journal({this.creationDateTime, this.body});
|
const Note({this.createdAt, this.body});
|
||||||
|
|
||||||
factory Journal.fromJson(Map<String, dynamic> json) {
|
factory Note.fromJson(Map<String, dynamic> json) {
|
||||||
return new Journal(
|
print(json)
|
||||||
creationDateTime: json['creationDateTime'],
|
return new Note(
|
||||||
body: json['body'],
|
createdAt: DateTime.parse(json['createdAt']),
|
||||||
|
body: json['data'],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<List<Note>> fetchNotes() async {
|
||||||
|
final response = await http.get('http://192.168.1.132:8000/notes');
|
||||||
|
final responseJson = json.decode(response.body);
|
||||||
|
|
||||||
|
var notes = <Note>[];
|
||||||
|
for (var postJson in responseJson) {
|
||||||
|
notes.add(new Note.fromJson(postJson));
|
||||||
|
}
|
||||||
|
|
||||||
|
return notes;
|
||||||
|
}
|
||||||
|
|
||||||
// How to load this dynamically?
|
// How to load this dynamically?
|
||||||
// I can put this in a widget with a state
|
// I can put this in a widget with a state
|
||||||
// and do an async call.
|
// and do an async call.
|
||||||
var state = <Journal>[
|
/*
|
||||||
Journal(
|
var state = <Note>[
|
||||||
creationDateTime: new DateTime.now(),
|
Note(
|
||||||
|
createdAt: new DateTime.now(),
|
||||||
body: "The quick brown fox jumped over the very lazy dog, who then ran"
|
body: "The quick brown fox jumped over the very lazy dog, who then ran"
|
||||||
"all around the garden untill he fell down",
|
"all around the garden untill he fell down",
|
||||||
),
|
),
|
||||||
Journal(
|
Note(
|
||||||
creationDateTime: new DateTime.now().subtract(new Duration(days: 1)),
|
createdAt: new DateTime.now().subtract(new Duration(days: 1)),
|
||||||
body: "This is the body",
|
body: "This is the body",
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
*/
|
||||||
|
|
||||||
void main() => runApp(new MyApp());
|
void main() => runApp(new MyApp());
|
||||||
|
|
||||||
@ -45,15 +64,31 @@ class JournalList extends StatelessWidget {
|
|||||||
appBar: new AppBar(
|
appBar: new AppBar(
|
||||||
title: new Text('Journal'),
|
title: new Text('Journal'),
|
||||||
),
|
),
|
||||||
body: _buildSuggestions(context),
|
|
||||||
floatingActionButton: createButton,
|
floatingActionButton: createButton,
|
||||||
|
body: new FutureBuilder<List<Note>>(
|
||||||
|
future: fetchNotes(),
|
||||||
|
builder: (context, snapshot) {
|
||||||
|
if (snapshot.hasData) {
|
||||||
|
var notes = snapshot.data;
|
||||||
|
return _buildSuggestions(context, notes);
|
||||||
|
} else if (snapshot.hasError) {
|
||||||
|
return new Text("${snapshot.error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CircularProgressIndicator();
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildRow(BuildContext context, Journal journal) {
|
Widget _buildRow(BuildContext context, Note journal) {
|
||||||
var title = journal.creationDateTime.toString();
|
var title = journal.createdAt.toString();
|
||||||
var time = "10:24";
|
var time = "10:24";
|
||||||
|
|
||||||
var body = journal.body;
|
var body = journal.body;
|
||||||
|
if (body.length >= 100) {
|
||||||
|
body = body.substring(0, 100);
|
||||||
|
}
|
||||||
|
body = body.replaceAll("\n", " ");
|
||||||
|
|
||||||
return new ListTile(
|
return new ListTile(
|
||||||
isThreeLine: true,
|
isThreeLine: true,
|
||||||
@ -92,15 +127,15 @@ So now what is going to happen?
|
|||||||
Navigator.of(context).push(route);
|
Navigator.of(context).push(route);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildSuggestions(BuildContext context) {
|
Widget _buildSuggestions(BuildContext context, List<Note> notes) {
|
||||||
return new ListView.builder(
|
return new ListView.builder(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
itemBuilder: (context, i) {
|
itemBuilder: (context, i) {
|
||||||
if (i >= state.length) {
|
if (i >= notes.length) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
//if (i.isOdd) return new Divider();
|
//if (i.isOdd) return new Divider();
|
||||||
return _buildRow(context, state[i]);
|
return _buildRow(context, notes[i]);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@ packages:
|
|||||||
source: hosted
|
source: hosted
|
||||||
version: "0.13.3"
|
version: "0.13.3"
|
||||||
http:
|
http:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: http
|
name: http
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
@ -130,6 +130,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.1.1"
|
version: "3.1.1"
|
||||||
|
intl:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: intl
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.15.6"
|
||||||
io:
|
io:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -4,6 +4,9 @@ description: A simple journaling app.
|
|||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
http: "^0.11.3+16"
|
||||||
|
intl: "^0.15.6"
|
||||||
|
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
Reference in New Issue
Block a user