Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
#53. Completing example with receipt CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
matei-tm committed Apr 23, 2019
1 parent d216ed1 commit 54ac04f
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 30 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Dart",
"program": "example/lib/main.dart",
"request": "launch",
"type": "dart"
}
]
}
3 changes: 2 additions & 1 deletion example/lib/fragments/contact/receipt_add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ class _ReceiptAddPageState extends State<ReceiptAddPage> {
// },
// ),
TextFormField(
//initialValue: "2061-07-28 17:50:03", // DateTime.now().add(Duration(days: 30)).toIso8601String(),
decoration: InputDecoration(
hintText: "expiration Date (format: 2012-02-27 13:27:00)",
hintText: "expiration Date (format: 2071-05-30 17:50:03)",
labelText: "expiration Date*",
),
validator: (value) {
Expand Down
190 changes: 190 additions & 0 deletions example/lib/fragments/contact/receipt_edit.dart
Original file line number Diff line number Diff line change
@@ -1 +1,191 @@

import 'package:example/main.adapter.g.m8.dart';
import 'package:example/models/receipt.dart';
import 'package:example/models/receipt.g.m8.dart';
import 'package:flutter/material.dart';

class ReceiptEditPage extends StatefulWidget {
final Receipt currentReceipt;

ReceiptEditPage({this.currentReceipt});

_ReceiptEditPageState createState() => _ReceiptEditPageState(currentReceipt);
}

class _ReceiptEditPageState extends State<ReceiptEditPage> {
final _formKey = GlobalKey<FormState>();

Receipt _stateReceipt;
String title;

DatabaseHelper _db = DatabaseHelper();

_ReceiptEditPageState(this._stateReceipt);

@override
void initState() {
super.initState();

_stateReceipt = _stateReceipt ??
ReceiptProxy(
isBio: true,
expirationDate: null,
quantity: null,
numberOfItems: null,
storageTemperature: null,
description: null);
}

Future<void> submit() async {
if (this._formKey.currentState.validate()) {
_formKey.currentState.save();

_db = DatabaseHelper();

int id = await _db.updateReceipt(_stateReceipt);
_stateReceipt.id = id;

Navigator.of(context).pop(_stateReceipt);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title ?? "Edit Receipt"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.check),
onPressed: submit,
),
],
),
body: Stack(
children: <Widget>[
Container(
padding: EdgeInsets.all(20.0),
child: Form(
autovalidate: false,
key: _formKey,
child: ListView(
shrinkWrap: true,
children: <Widget>[
TextFormField(
initialValue: _stateReceipt.description,
decoration: InputDecoration(
hintText: "Description",
labelText: "Description*",
),
validator: (value) {
if (value.isEmpty) {
return "Please enter description";
}
},
onSaved: (String value) {
_stateReceipt.description = value;
},
),
// TextFormField(
// decoration: new InputDecoration(
// hintText: "decomposing Duration as Duration",
// labelText: "decomposing Duration*",
// ),
// validator: (value) {
// if (value.isEmpty) {
// return "Please enter decomposingDuration";
// }
// },
// onSaved: (String value) {
// _stateReceipt.decomposingDuration =
// Duration(hours: int.parse(value));
// },
// ),
TextFormField(
initialValue: _stateReceipt.expirationDate.toIso8601String(),
decoration: InputDecoration(
hintText: "expiration Date (format: 2071-05-30 17:50:03)",
labelText: "expiration Date*",
),
validator: (value) {
if (value.isEmpty) {
return "Please enter expirationDate";
}
},
onSaved: (String value) {
_stateReceipt.expirationDate = DateTime.parse(value);
},
),
SwitchListTile(
title: const Text('Is Bio'),
value: _stateReceipt.isBio,
onChanged: (bool val) =>
setState(() => _stateReceipt.isBio = val)),
TextFormField(
initialValue: _stateReceipt.numberOfItems.toString(),
decoration: InputDecoration(
hintText: "number of Items as int",
labelText: "number of Items*",
),
validator: (value) {
if (value.isEmpty) {
return "Please enter numberOfItems";
}
},
onSaved: (String value) {
_stateReceipt.numberOfItems = int.parse(value);
},
),
// TextFormField(
// decoration: new InputDecoration(
// hintText: "numberOfMolecules as bigInt",
// labelText: "numberOfMolecules*",
// ),
// validator: (value) {
// if (value.isEmpty) {
// return "Please enter numberOfMolecules";
// }
// },
// onSaved: (String value) {
// _stateReceipt.numberOfMolecules = BigInt.parse(value);
// },
// ),
TextFormField(
initialValue: _stateReceipt.quantity.toString(),
decoration: InputDecoration(
hintText: "quantity as double",
labelText: "quantity*",
),
validator: (value) {
if (value.isEmpty) {
return "Please enter quantity";
}
},
onSaved: (String value) {
_stateReceipt.quantity = double.parse(value);
},
),
TextFormField(
initialValue: _stateReceipt.storageTemperature.toString(),
decoration: InputDecoration(
hintText: "storage Temperature as num",
labelText: "storage Temperature*",
),
validator: (value) {
if (value.isEmpty) {
return "Please enter storage Temperature";
}
},
onSaved: (String value) {
_stateReceipt.storageTemperature = num.parse(value);
},
),
],
),
),
),
],
),
);
}
}
13 changes: 8 additions & 5 deletions example/lib/fragments/contact/receipt_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
typedef ReceiptRowActionCallback = void Function(Receipt receipt);

class ReceiptRow extends StatelessWidget {
static const double height = 192.0;
static const double height = 240.0;

final RoundedRectangleBorder shape = const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
Expand Down Expand Up @@ -68,8 +68,11 @@ class ReceiptRow extends StatelessWidget {
),
),
//Text("${receipt.decomposingDuration}"),
Text("${receipt?.expirationDate}"),
Text("${receipt?.isBio}"),
Text("Expiration Date: ${receipt?.expirationDate}"),
Text("Is Bio: ${receipt?.isBio}"),
Text("Number of Items: ${receipt?.numberOfItems}"),
Text("Quantity: ${receipt?.quantity}"),
Text("Storage Temperature: ${receipt?.storageTemperature}\u00b0"),
],
),
),
Expand All @@ -84,14 +87,14 @@ class ReceiptRow extends StatelessWidget {
child: Text(
"Edit",
),
textColor: Colors.amber.shade500,
textColor: Theme.of(context).accentColor,
onPressed: _getHandler(onPressedUpdate),
),
FlatButton(
child: Text(
"Delete",
),
textColor: Colors.amber.shade500,
textColor: Theme.of(context).accentColor,
onPressed: _getHandler(onPressedDelete),
),
],
Expand Down
35 changes: 27 additions & 8 deletions example/lib/fragments/receipts_fragment.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import 'package:example/fragments/contact/receipt_row.dart';
import 'package:example/main.adapter.g.m8.dart';
import 'package:example/models/receipt.dart';
import 'package:example/routes/enhanced_route.dart';
import 'package:flutter/material.dart';

class ReceiptsFragment extends StatefulWidget {
final Key _parentScaffoldKey;
ReceiptsFragment(this._parentScaffoldKey);

_ReceiptsFragmentState createState() => _ReceiptsFragmentState();
_ReceiptsFragmentState createState() =>
_ReceiptsFragmentState(_parentScaffoldKey);
}

class _ReceiptsFragmentState extends State<ReceiptsFragment> {
List<Receipt> receipts = [];

var _parentScaffoldKey;

_ReceiptsFragmentState(this._parentScaffoldKey);

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -69,6 +75,18 @@ class _ReceiptsFragmentState extends State<ReceiptsFragment> {
if (result != null) {
receipts.add(result);
}

_showMessage("Receipt added");
}

Future<void> _updateReceipt(Receipt receipt) async {
await Navigator.of(context).push(GymspectorRoute.editReceipt(receipt));

receipts.removeWhere((item) => item.id == receipt.id);
receipts.add(receipt);
setState(() {});

_showMessage("Receipt updated");
}

Future<void> _deleteReceipt(Receipt h) async {
Expand Down Expand Up @@ -102,14 +120,15 @@ class _ReceiptsFragmentState extends State<ReceiptsFragment> {
await db.deleteReceipt(h.id);
receipts.remove(h);
setState(() {});
}

Future<void> _updateReceipt(Receipt h) async {
var db = DatabaseHelper();
_showMessage("Receipt deleted");
}

await db.updateReceipt(h);
receipts.removeWhere((item) => item.id == h.id);
receipts.add(h);
setState(() {});
void _showMessage(String message) {
_parentScaffoldKey.currentState.showSnackBar(SnackBar(
key: Key('infoSnack'),
content: Text('Info: $message'),
duration: Duration(seconds: 3),
));
}
}
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:example/fragments/contact/receipt_add.dart';
import 'package:example/fragments/contact/receipt_edit.dart';
import 'package:example/main.adapter.g.m8.dart';
import 'package:example/pages/account_page.dart';
import 'package:example/pages/home_page.dart';
Expand Down Expand Up @@ -66,6 +67,7 @@ class MyHomePageState extends State<MyHomePage> {
routes: {
'/': (context) => HomePage(),
'/receipts/add': (context) => ReceiptAddPage(null),
'/receipts/edit': (context) => ReceiptEditPage(),
'start': (context) => AccountPage(null),
},
);
Expand Down
16 changes: 10 additions & 6 deletions example/lib/pages/account_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:example/main.adapter.g.m8.dart';
import 'package:example/models/user_account.g.m8.dart';
import 'package:example/routes/start_page_route.dart';
import 'package:example/routes/enhanced_route.dart';
import 'package:flutter/material.dart';

class AccountPage extends StatefulWidget {
Expand Down Expand Up @@ -173,7 +173,7 @@ class _AccountPageState extends State<AccountPage> {
}

void _addNewAccount() {
Navigator.of(context).pushReplacement(StartPageRoute(null));
Navigator.of(context).pushReplacement(GymspectorRoute(null));
}

_onTapDelete(BuildContext context) async {
Expand Down Expand Up @@ -336,10 +336,14 @@ class _AccountPageState extends State<AccountPage> {
_db = DatabaseHelper();
_db.deleteUserAccount(this._stateAccount.id);

var id = await _db.getUserAccountProxiesAll().then((v) => v.first.id);
var reminingUserAccounts = await _db.getUserAccountProxiesAll();

_db.setCurrentUserAccount(id);

Navigator.of(context).pushReplacementNamed("/");
if (reminingUserAccounts.length > 0) {
_db.setCurrentUserAccount(reminingUserAccounts.first.id);
Navigator.of(context).pushReplacementNamed("/");
} else {
Navigator.of(context).pop();
Navigator.of(context).pushReplacementNamed("start");
}
}
}
4 changes: 2 additions & 2 deletions example/lib/pages/helpers/guarded_account_state.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:example/main.adapter.g.m8.dart';
import 'package:example/models/user_account.dart';
import 'package:example/routes/start_page_route.dart';
import 'package:example/routes/enhanced_route.dart';
import 'package:flutter/material.dart';

typedef AccountActionCallback = Future<bool> Function(int);
Expand Down Expand Up @@ -67,7 +67,7 @@ abstract class GuardedAccountState<T extends StatefulWidget> extends State<T> {
}

void goToStartPage() {
Navigator.of(context).push(StartPageRoute(guardedCurrentAccount));
Navigator.of(context).push(GymspectorRoute(guardedCurrentAccount));
return;
}
}
Loading

0 comments on commit 54ac04f

Please sign in to comment.