added DatePicker, fixed provider.watch on CarDetailScreen,
new txn now updates car mileage
This commit is contained in:
294
lib/main.dart
294
lib/main.dart
@@ -15,12 +15,12 @@ void main() async {
|
||||
child: const MaterialApp(
|
||||
title: 'Dashboard',
|
||||
home: MyApp(),
|
||||
/* initialRoute: '/',
|
||||
/* initialRoute: '/',
|
||||
routes: {
|
||||
'/': (context) => const MyApp(),
|
||||
'/about': (context) => const AboutScreen(),
|
||||
'/newcar': (context) => const NewCarScreen(),
|
||||
'/detail': (context) => const CarDetailScreen(),
|
||||
'/detail': (context) => CarDetailScreen(),
|
||||
'/edit': (context) => EditCarScreen(),
|
||||
}, */
|
||||
),
|
||||
@@ -32,8 +32,10 @@ void main() async {
|
||||
class GarageModel extends ChangeNotifier {
|
||||
/// internal state of garage
|
||||
late List<Car> _cars = [];
|
||||
late List<Txn> _txns = []; // hold current car txns, repl on new load
|
||||
final DbHelperSqlite _dbHelper = DbHelperSqlite.instance;
|
||||
UnmodifiableListView<Car> get cars => UnmodifiableListView(_cars);
|
||||
UnmodifiableListView<Txn> get txns => UnmodifiableListView(_txns);
|
||||
|
||||
void add(Car car) {
|
||||
// _cars.add(car);
|
||||
@@ -81,6 +83,24 @@ class GarageModel extends ChangeNotifier {
|
||||
print(_cars);
|
||||
// notifyListeners(); //for some reason this inits db infinitely
|
||||
}
|
||||
|
||||
void insertTxn(Txn txn) async {
|
||||
await _dbHelper.insertTxn(txn);
|
||||
// update car
|
||||
await getTxns(txn.carid!);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void deleteTxn(Txn txn) {
|
||||
_dbHelper.deleteTxn(txn);
|
||||
// get old mileage from car
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> getTxns(int carid) async {
|
||||
_txns = await _dbHelper.fetchTxns(carid);
|
||||
print('get txns: $_txns');
|
||||
}
|
||||
}
|
||||
|
||||
class Garage extends StatefulWidget {
|
||||
@@ -95,7 +115,6 @@ class _Garage extends State<Garage> {
|
||||
@override
|
||||
void initState() {
|
||||
_cars = Provider.of<GarageModel>(context, listen: false).getCars();
|
||||
// _cars = Provider.of<GarageModel>(context).getCars();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@@ -117,30 +136,19 @@ class _Garage extends State<Garage> {
|
||||
return Expanded(
|
||||
child: Consumer<GarageModel>(builder: (context, garage, child) {
|
||||
return ListView.separated(
|
||||
// padding: const EdgeInsets.all(8),
|
||||
itemCount: garage._cars.length,
|
||||
itemBuilder: (context, index) => ListTile(
|
||||
leading: const Icon(Icons.directions_car),
|
||||
title: Text(garage.cars[index].nickname ?? "nick_ph"),
|
||||
subtitle: Text(garage.cars[index].vin ?? "vin_ph"),
|
||||
/* trailing: IconButton(
|
||||
onPressed: Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => EditCarScreen(
|
||||
car: garage._cars[index], carIndex: index)),
|
||||
),
|
||||
icon: Icons.edit),
|
||||
),
|
||||
onTap: () => Navigator.pushNamed(context, '/detail'), */
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
// context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => CarDetailScreen(
|
||||
car: garage._cars[index], carIndex: index),
|
||||
),
|
||||
);
|
||||
Navigator.of(context)
|
||||
.push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) =>
|
||||
CarDetailScreen(carIndex: index),
|
||||
))
|
||||
.then((value) => setState(() {}));
|
||||
},
|
||||
),
|
||||
separatorBuilder: (BuildContext context, int index) =>
|
||||
@@ -154,6 +162,63 @@ class _Garage extends State<Garage> {
|
||||
}
|
||||
}
|
||||
|
||||
class CurrentCar extends StatefulWidget {
|
||||
CurrentCar({super.key, required this.car});
|
||||
Car car;
|
||||
|
||||
@override
|
||||
State<CurrentCar> createState() => _CurrentCar();
|
||||
}
|
||||
|
||||
class _CurrentCar extends State<CurrentCar> {
|
||||
late Future _txns;
|
||||
late Car car = widget.car;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_txns = Provider.of<GarageModel>(context, listen: false).getTxns(car.id!);
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder(
|
||||
future: _txns,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
} else {
|
||||
if (snapshot.error != null) {
|
||||
return const Center(
|
||||
child: Text('An error occurred'),
|
||||
);
|
||||
} else {
|
||||
return Expanded(child: Consumer<GarageModel>(
|
||||
builder: (context, garage, child) {
|
||||
return ListView.separated(
|
||||
// padding: const EdgeInsets.all(8),
|
||||
itemCount: garage.txns.length,
|
||||
itemBuilder: (context, index) => ListTile(
|
||||
// dense: true, //only affects text, use visualDensity instead
|
||||
visualDensity:
|
||||
const VisualDensity(horizontal: 0, vertical: -4),
|
||||
leading: const Icon(Icons.local_gas_station),
|
||||
title: Text(garage.txns[index].txntype ?? "type"),
|
||||
subtitle: Text(garage.txns[index].note ?? "note"),
|
||||
onTap: (() => VoidCallback)),
|
||||
separatorBuilder: (BuildContext context, int index) =>
|
||||
const Divider(),
|
||||
);
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class MyDrawer extends StatelessWidget {
|
||||
const MyDrawer({super.key});
|
||||
|
||||
@@ -387,7 +452,7 @@ class _NewCarScreenState extends State<NewCarScreen> {
|
||||
mileage: _car.mileage);
|
||||
});
|
||||
var garage =
|
||||
context.read<GarageModel>(); //implement Provider
|
||||
context.read<GarageModel>();
|
||||
garage.add(_car);
|
||||
_form.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
@@ -429,14 +494,18 @@ class _NewCarScreenState extends State<NewCarScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
class CarDetailScreen extends StatelessWidget {
|
||||
const CarDetailScreen({super.key, required this.car, required this.carIndex});
|
||||
final Car car;
|
||||
final int carIndex; //pass this thru for edit screen
|
||||
class CarDetailScreen extends StatefulWidget {
|
||||
CarDetailScreen({super.key, required this.carIndex});
|
||||
final int carIndex;
|
||||
@override
|
||||
State<CarDetailScreen> createState() => _CarDetailScreenState();
|
||||
}
|
||||
|
||||
class _CarDetailScreenState extends State<CarDetailScreen> {
|
||||
late Car car = context.watch<GarageModel>()._cars[widget.carIndex];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// const Car car = context.read<GarageModel>()._cars[index];
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
@@ -452,18 +521,19 @@ class CarDetailScreen extends StatelessWidget {
|
||||
children: [
|
||||
const Text("plh img picker"),
|
||||
Center(
|
||||
// edit car screen
|
||||
child: Ink(
|
||||
decoration: const ShapeDecoration(
|
||||
color: Colors.lightBlue, shape: CircleBorder()),
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.edit),
|
||||
color: Colors.white,
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
onPressed: () async {
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) =>
|
||||
EditCarScreen(car: car, carIndex: carIndex)),
|
||||
builder: (context) => EditCarScreen(
|
||||
car: car, carIndex: widget.carIndex)),
|
||||
);
|
||||
},
|
||||
),
|
||||
@@ -480,12 +550,21 @@ class CarDetailScreen extends StatelessWidget {
|
||||
Text("VIN: ${car.vin}"),
|
||||
Text("License Plate: ${car.plate}"),
|
||||
Text("Mileage: ${car.mileage.toString()}"),
|
||||
CurrentCar(car: car),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: const Icon(Icons.edit),
|
||||
onPressed: (() => VoidCallback), //add Transaction
|
||||
onPressed: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => NewTxn(
|
||||
car: car,
|
||||
carIndex: widget.carIndex),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -502,7 +581,7 @@ class EditCarScreen extends StatefulWidget {
|
||||
|
||||
class _EditCarScreenState extends State<EditCarScreen> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
late Car car = widget.car; //initialization is required
|
||||
late Car car = widget.car;
|
||||
late int carIndex = widget.carIndex;
|
||||
|
||||
@override
|
||||
@@ -520,7 +599,6 @@ class _EditCarScreenState extends State<EditCarScreen> {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
//new car form text fields
|
||||
TextFormField(
|
||||
initialValue: car.nickname,
|
||||
decoration: const InputDecoration(
|
||||
@@ -585,15 +663,6 @@ class _EditCarScreenState extends State<EditCarScreen> {
|
||||
//validate form
|
||||
updateForm
|
||||
.save(); //save values (reqd before putting them anywhere)
|
||||
/* setState(() {
|
||||
//modify state of car (necessary?)
|
||||
car = garage._cars[carIndex];
|
||||
car
|
||||
vin: car.vin,
|
||||
nickname: car.nickname,
|
||||
plate: car.plate,
|
||||
mileage: car.mileage);
|
||||
}); //implement Provider */
|
||||
garage.update(car, carIndex);
|
||||
updateForm.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
@@ -641,8 +710,9 @@ class _EditCarScreenState extends State<EditCarScreen> {
|
||||
}
|
||||
|
||||
class NewTxn extends StatefulWidget {
|
||||
NewTxn({super.key, required this.car});
|
||||
NewTxn({super.key, required this.car, required this.carIndex});
|
||||
Car car;
|
||||
int carIndex;
|
||||
|
||||
@override
|
||||
State<NewTxn> createState() => _NewTxnState();
|
||||
@@ -650,14 +720,28 @@ class NewTxn extends StatefulWidget {
|
||||
|
||||
class _NewTxnState extends State<NewTxn> {
|
||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||
late Car car = widget.car; //initialization is required
|
||||
late Car car = widget.car;
|
||||
late int carIndex = widget.carIndex;
|
||||
DateTime datetime = DateTime.now();
|
||||
Txn txn = Txn();
|
||||
bool refresh = false;
|
||||
|
||||
Future<void> _selectDate(BuildContext context) async {
|
||||
final DateTime? timepicked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: datetime,
|
||||
firstDate: DateTime.utc(1776, 7, 4),
|
||||
lastDate: DateTime.utc(2222, 2, 22));
|
||||
if (timepicked != null && timepicked != datetime) {
|
||||
setState(() => datetime = timepicked);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Edit ${car.nickname}"),
|
||||
title: Text("Add new txn for ${car.nickname}"),
|
||||
backgroundColor: const Color.fromARGB(255, 185, 47, 5),
|
||||
),
|
||||
body: Container(
|
||||
@@ -667,35 +751,41 @@ class _NewTxnState extends State<NewTxn> {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
//new car form text fields
|
||||
/* RadioListTile(
|
||||
value: ,
|
||||
), */
|
||||
// txntype - change to dropdown
|
||||
|
||||
TextFormField(
|
||||
initialValue: car.vin,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'VIN',
|
||||
labelText: 'Type',
|
||||
),
|
||||
onSaved: (val) => setState(() => car.vin = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a unique VIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (val) => setState(() => txn.txntype = val),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Flexible(
|
||||
flex: 1,
|
||||
child: IconButton(
|
||||
onPressed: () => _selectDate(context),
|
||||
icon: const Icon(Icons.calendar_month)),
|
||||
),
|
||||
Flexible(
|
||||
flex: 3,
|
||||
child: InputDatePickerFormField(
|
||||
firstDate: DateTime.utc(1776, 7, 4),
|
||||
lastDate: DateTime.utc(2222, 2, 22),
|
||||
initialDate: datetime,
|
||||
onDateSaved: (val) => setState(
|
||||
() => txn.datetime = val.millisecondsSinceEpoch)),
|
||||
),
|
||||
],
|
||||
),
|
||||
TextFormField(
|
||||
initialValue: car.plate,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'License Plate',
|
||||
labelText: 'Cost',
|
||||
),
|
||||
onSaved: (val) => setState(() => car.plate = val),
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a license plate';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
initialValue: "0",
|
||||
onSaved: (val) =>
|
||||
setState(() => txn.cost = double.parse(val ?? "0")),
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
TextFormField(
|
||||
initialValue: car.mileage.toString(),
|
||||
@@ -703,37 +793,41 @@ class _NewTxnState extends State<NewTxn> {
|
||||
labelText: 'Mileage',
|
||||
),
|
||||
onSaved: (val) =>
|
||||
setState(() => car.mileage = int.parse(val ?? "")),
|
||||
// inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly] //try this?
|
||||
setState(() => txn.mileage = int.parse(val ?? "")),
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter car\'s current mileage';
|
||||
return "Must be >= current mileage";
|
||||
} else if (int.parse(value) < car.mileage!.toInt()) {
|
||||
return 'Must be >= current mileage';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Note',
|
||||
),
|
||||
onSaved: (val) => setState(() => txn.note = val),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
final updateForm = _formKey.currentState!;
|
||||
if (updateForm.validate()) {
|
||||
//validate form
|
||||
updateForm
|
||||
.save(); //save values (reqd before putting them anywhere)
|
||||
/* setState(() {
|
||||
//modify state of car (necessary?)
|
||||
car = garage._cars[carIndex];
|
||||
car
|
||||
vin: car.vin,
|
||||
nickname: car.nickname,
|
||||
plate: car.plate,
|
||||
mileage: car.mileage);
|
||||
}); //implement Provider */
|
||||
final form = _formKey.currentState!;
|
||||
if (form.validate()) {
|
||||
form.save();
|
||||
setState(() {
|
||||
txn.datetime = DateTime.now().millisecondsSinceEpoch;
|
||||
txn.carid = car.id;
|
||||
car.mileage = txn.mileage; // update car mileage too
|
||||
});
|
||||
var garage = context.read<GarageModel>();
|
||||
garage.update(car, carIndex);
|
||||
updateForm.reset();
|
||||
Navigator.pushNamed(context, '/');
|
||||
print(txn.toMap());
|
||||
garage.insertTxn(txn);
|
||||
form.reset();
|
||||
Navigator.pop(context);
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
@@ -745,30 +839,6 @@ class _NewTxnState extends State<NewTxn> {
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => VoidCallback),
|
||||
onLongPress: () {
|
||||
garage.delete(car);
|
||||
Navigator.pushNamed(context, '/');
|
||||
},
|
||||
child: Row(children: const <Widget>[
|
||||
Icon(Icons.delete),
|
||||
Text('Delete'),
|
||||
]),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: (() => VoidCallback),
|
||||
child: Row(children: const <Widget>[
|
||||
Icon(Icons.sync_alt),
|
||||
Text('enable/disable'),
|
||||
]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user