|
import 'package:flutter/material.dart'; |
|
import 'package:path/path.dart'; |
|
import 'package:sqflite/sqflite.dart'; |
|
import 'package:workshop_codecon/models/car.dart'; |
|
import 'package:workshop_codecon/models/hw_collection.dart'; |
|
import 'package:workshop_codecon/repositories/car_repository.dart'; |
|
|
|
class CollectionDetails extends StatefulWidget { |
|
final MiniCollection miniCollection; |
|
|
|
const CollectionDetails({ |
|
Key key, |
|
this.miniCollection, |
|
}) : super(key: key); |
|
|
|
@override |
|
_CollectionDetailsState createState() => _CollectionDetailsState(); |
|
} |
|
|
|
class _CollectionDetailsState extends State<CollectionDetails> { |
|
List<int> inCollection = <int>[]; |
|
final carRepo = CarRepository(); |
|
|
|
@override |
|
void initState() { |
|
super.initState(); |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return Scaffold( |
|
appBar: AppBar( |
|
title: Text(widget.miniCollection.name), |
|
centerTitle: true, |
|
), |
|
body: SafeArea( |
|
child: Container( |
|
child: FutureBuilder( |
|
future: carRepo.get(widget.miniCollection.id), |
|
builder: (context, snapshot) { |
|
switch (snapshot.connectionState) { |
|
case ConnectionState.none: |
|
continue loading; |
|
break; |
|
loading: |
|
case ConnectionState.waiting: |
|
return Center( |
|
child: CircularProgressIndicator(), |
|
); |
|
break; |
|
case ConnectionState.active: |
|
// TODO: Handle this case. |
|
break; |
|
case ConnectionState.done: |
|
List<Car> cars = snapshot.data; |
|
|
|
return PageView( |
|
controller: PageController(initialPage: 1), |
|
scrollDirection: Axis.horizontal, |
|
children: cars.map((Car car) { |
|
return Container( |
|
child: Column( |
|
mainAxisAlignment: MainAxisAlignment.center, |
|
children: <Widget>[ |
|
if (car.mainImages.length > 0) |
|
Image.network(car.mainImages[0]['URL']), |
|
Text(car.name), |
|
BookmarkIcon( |
|
inCollection: inCollection, |
|
carId: car.id, |
|
) |
|
], |
|
), |
|
); |
|
}).toList(), |
|
); |
|
break; |
|
} |
|
}, |
|
), |
|
), |
|
), |
|
); |
|
} |
|
} |
|
|
|
class BookmarkIcon extends StatefulWidget { |
|
List<int> inCollection; |
|
int carId; |
|
|
|
BookmarkIcon({Key key, this.inCollection, this.carId}) : super(key: key); |
|
|
|
@override |
|
_BookmarkIconState createState() => _BookmarkIconState(); |
|
} |
|
|
|
class _BookmarkIconState extends State<BookmarkIcon> { |
|
final carRepo = CarRepository(); |
|
|
|
Database database; |
|
|
|
void loadDB() async { |
|
var databasesPath = await getDatabasesPath(); |
|
String path = join(databasesPath, 'workshop_codecon.db'); |
|
|
|
database = await openDatabase(path, version: 2, |
|
onCreate: (Database db, int version) async { |
|
await db.execute('CREATE TABLE carros (id INTEGER)'); |
|
}); |
|
|
|
carregarMeusCarros(); |
|
} |
|
|
|
void adicionarCarro(int id) async { |
|
await database.insert("carros", {'id': id}); |
|
} |
|
|
|
void removeCarro(int id) async { |
|
await database.delete("carros", where: 'id = ?', whereArgs: [id]); |
|
} |
|
|
|
void carregarMeusCarros() async { |
|
List<Map<String, dynamic>> r = |
|
await database.rawQuery("select * from carros"); |
|
|
|
r.forEach((item) { |
|
widget.inCollection.add(item['id']); |
|
}); |
|
|
|
setState(() { |
|
widget.inCollection = widget.inCollection; |
|
}); |
|
} |
|
|
|
@override |
|
void initState() { |
|
loadDB(); |
|
|
|
super.initState(); |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return IconButton( |
|
icon: Icon(widget.inCollection.contains(widget.carId) |
|
? Icons.bookmark |
|
: Icons.bookmark_border), |
|
onPressed: () { |
|
if (widget.inCollection.contains(widget.carId)) { |
|
widget.inCollection.remove(widget.carId); |
|
removeCarro(widget.carId); |
|
} else { |
|
widget.inCollection.add(widget.carId); |
|
adicionarCarro(widget.carId); |
|
} |
|
|
|
setState(() { |
|
widget.inCollection = widget.inCollection; |
|
}); |
|
}, |
|
); |
|
} |
|
} |