Skip to content

Instantly share code, notes, and snippets.

View Norbert515's full-sized avatar
😃

Norbert Kozsir Norbert515

😃
View GitHub Profile
@Norbert515
Norbert515 / main.dart
Created February 15, 2018 19:27
TextField
new TextField(
decoration: new InputDecoration(
hintText: 'Choose a book',
),
onChanged: (string) => (subject.add(string)),
),
@Norbert515
Norbert515 / main.dart
Created February 15, 2018 19:29
ListView
new Expanded(
child: new ListView.builder(
padding: new EdgeInsets.all(8.0),
itemCount: _items.length,
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new Padding(
padding: new EdgeInsets.all(8.0),
child: new Row(
children: <Widget>[
@Norbert515
Norbert515 / main.dart
Created February 15, 2018 19:30
init
@override
void initState() {
super.initState();
subject.stream.debounce(new Duration(milliseconds: 600)).listen(_textChanged);
}
@Norbert515
Norbert515 / main.dart
Created February 15, 2018 19:32
addBook
void _addBook(dynamic book) {
setState(() {
_items.add(new Book(book["volumeInfo"]["title"], book["volumeInfo"]["imageLinks"]["smallThumbnail"]));
});
}
@Norbert515
Norbert515 / main.dart
Created February 15, 2018 19:54
textChanged
void _textChanged(String text) {
if(text.isEmpty) {
setState((){_isLoading = false;});
_clearList();
return;
}
setState((){_isLoading = true;});
_clearList();
http.get("https://www.googleapis.com/books/v1/volumes?q=$text")
.then((response) => response.body)
@Norbert515
Norbert515 / database.dart
Created February 20, 2018 16:53
BookDatabase init
class BookDatabase {
static final BookDatabase _bookDatabase = new BookDatabase._internal();
final String tableName = "Books";
Database db;
static BookDatabase get() {
return _bookDatabase;
}
@Norbert515
Norbert515 / database.dart
Created February 20, 2018 17:00
getBook
/// Get a book by its id, if there is not entry for that ID, returns null.
Future<Book> getBook(String id) async{
var result = await db.rawQuery('SELECT * FROM $tableName WHERE ${Book.db_id} = "$id"');
if(result.length == 0)return null;
return new Book.fromMap(result[0]);
}
@Norbert515
Norbert515 / database.dart
Created February 20, 2018 17:04
updateBook
/// Inserts or replaces the book.
Future updateBook(Book book) async {
await db.inTransaction(() async {
await db.rawInsert(
'INSERT OR REPLACE INTO '
'$tableName(${Book.db_id}, ${Book.db_title}, ${Book.db_url}, ${Book.db_star}, ${Book.db_notes})'
' VALUES("${book.id}", "${book.title}", "${book.url}", ${book.starred? 1:0}, "${book.notes}")');
});
}
@Norbert515
Norbert515 / main.dart
Created February 20, 2018 17:15
BookCard
class BookCard extends StatefulWidget {
BookCard(this.book);
final Book book;
@override
State<StatefulWidget> createState() => new BookCardState();
@Norbert515
Norbert515 / main.dart
Created February 20, 2018 17:16
BookCardState variable and initState()
class BookCardState extends State<BookCard> {
Book bookState;
@override
void initState() {
super.initState();
bookState = widget.book;