2019-05-08 18:54:14 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2019-05-10 11:43:45 +00:00
|
|
|
import 'package:local_spend/common/apifunctions/submit_receipt_api.dart';
|
2019-05-08 18:54:14 +00:00
|
|
|
import 'package:local_spend/common/functions/show_dialog_single_button.dart';
|
|
|
|
import 'package:local_spend/common/platform/platform_scaffold.dart';
|
|
|
|
import 'package:local_spend/common/widgets/basic_drawer.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2019-05-16 16:04:08 +00:00
|
|
|
import 'package:intl/intl.dart';
|
2019-05-10 11:43:45 +00:00
|
|
|
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
|
2019-07-08 10:53:10 +00:00
|
|
|
import 'package:local_spend/common/apifunctions/find_organisations.dart';
|
2019-05-08 18:54:14 +00:00
|
|
|
|
|
|
|
const URL = "https://flutter.io/";
|
2019-07-05 21:56:15 +00:00
|
|
|
const demonstration = false;
|
2019-05-08 18:54:14 +00:00
|
|
|
|
|
|
|
class ReceiptPage extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() {
|
|
|
|
return new ReceiptPageState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReceiptPageState extends State<ReceiptPage> {
|
2019-05-10 11:43:45 +00:00
|
|
|
final TextEditingController _timeController = TextEditingController();
|
|
|
|
final TextEditingController _amountController = TextEditingController();
|
|
|
|
final TextEditingController _essentialController = TextEditingController();
|
|
|
|
final TextEditingController _recurringController = TextEditingController();
|
|
|
|
final TextEditingController _typeController = TextEditingController();
|
|
|
|
final TextEditingController _orgController = TextEditingController();
|
2019-05-08 18:54:14 +00:00
|
|
|
|
2019-07-05 21:56:15 +00:00
|
|
|
FocusNode focusNode; // added so focus can move automatically
|
|
|
|
|
2019-05-16 16:04:08 +00:00
|
|
|
DateTime date;
|
|
|
|
|
2019-05-08 18:54:14 +00:00
|
|
|
Future launchURL(String url) async {
|
|
|
|
if (await canLaunch(url)) {
|
|
|
|
await launch(url, forceSafariVC: true, forceWebView: true);
|
|
|
|
} else {
|
|
|
|
showDialogSingleButton(
|
|
|
|
context,
|
|
|
|
"Unable to reach your website.",
|
|
|
|
"Currently unable to reach the website $URL. Please try again at a later time.",
|
|
|
|
"OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2019-05-10 11:43:45 +00:00
|
|
|
_saveCurrentRoute("/ReceiptPageState");
|
2019-07-05 21:56:15 +00:00
|
|
|
|
|
|
|
focusNode = FocusNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
|
|
|
|
focusNode.dispose(); //disposes focus node when form disposed
|
2019-05-08 18:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_saveCurrentRoute(String lastRoute) async {
|
|
|
|
SharedPreferences preferences = await SharedPreferences.getInstance();
|
|
|
|
await preferences.setString('LastPageRoute', lastRoute);
|
|
|
|
}
|
|
|
|
|
2019-07-05 21:56:15 +00:00
|
|
|
void submitReceipt(String amount, String time, String orgName) async {
|
2019-07-05 14:34:39 +00:00
|
|
|
SystemChannels.textInput.invokeMethod('TextInput.hide');
|
|
|
|
|
|
|
|
if (demonstration)
|
|
|
|
{
|
|
|
|
await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
// return object of type Dialog
|
|
|
|
return AlertDialog(
|
|
|
|
title: new Text("Success"),
|
|
|
|
content: new Text("Recepit successfully submitted."),
|
|
|
|
actions: <Widget>[
|
|
|
|
// usually buttons at the bottom of the dialog
|
|
|
|
new FlatButton(
|
|
|
|
child: new Text("OK"),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
).then((_) {
|
|
|
|
Navigator.of(context).pushNamed('/HomePage');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Receipt receipt = new Receipt();
|
|
|
|
|
|
|
|
// setting up 'receipt'
|
|
|
|
receipt.amount = amount;
|
2019-07-05 21:56:15 +00:00
|
|
|
receipt.time = formatDate(time);
|
|
|
|
receipt.organisationName = orgName;
|
|
|
|
|
|
|
|
// receipt.essential = convertBoolToString(toConvert)
|
2019-07-05 14:34:39 +00:00
|
|
|
|
|
|
|
//TODO: initialise receipt with correct values from form
|
|
|
|
|
|
|
|
// receipt.category = category;
|
|
|
|
// receipt.etc = etc;
|
|
|
|
|
|
|
|
submitReceiptAPI(context, receipt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 21:56:15 +00:00
|
|
|
String convertBoolToString(bool toConvert) {
|
|
|
|
if (toConvert)
|
|
|
|
{
|
|
|
|
return "true";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
String formatDate(String date) {
|
|
|
|
// return "";
|
|
|
|
// should be in format:
|
|
|
|
// yyyy-MM-ddThh:mm:00.000+01:00
|
|
|
|
// eg 2019-07-05T10:24:00.000+01.00 (real life example, works)
|
|
|
|
|
|
|
|
// current format = "dd/MM/yyyy 'at' hh:mm"
|
|
|
|
|
|
|
|
var components = new List(5);
|
|
|
|
|
|
|
|
components[0] = (date.substring(0,2)); // dd
|
|
|
|
components[1] = (date.substring(3,5)); // MM
|
|
|
|
components[2] = (date.substring(6,10)); // yyyy
|
|
|
|
components[3] = (date.substring(14,16)); // hh
|
|
|
|
components[4] = (date.substring(17,19)); // mm
|
|
|
|
|
|
|
|
//print(components);
|
|
|
|
|
|
|
|
return (components[2] + "-" + components[1] + "-" + components[0]
|
|
|
|
+ "T" + components[3] + ":" + components[4] + ":00.000+01:00");
|
|
|
|
|
|
|
|
// Yes, there is probably a function to convert dates, but I didn't
|
|
|
|
// know that before writing this and it's done now so I'm keeping it.
|
|
|
|
}
|
|
|
|
|
2019-05-08 18:54:14 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
var drawer = Drawer();
|
|
|
|
return WillPopScope(
|
|
|
|
onWillPop: () {
|
|
|
|
if (Navigator.canPop(context)) {
|
|
|
|
Navigator.of(context).pushNamedAndRemoveUntil(
|
|
|
|
'/HomePage', (Route<dynamic> route) => false);
|
|
|
|
} else {
|
|
|
|
Navigator.of(context).pushReplacementNamed('/HomePage');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: PlatformScaffold(
|
|
|
|
drawer: BasicDrawer(),
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(
|
2019-05-10 11:43:45 +00:00
|
|
|
"Submit Receipt",
|
2019-05-08 18:54:14 +00:00
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 30.0,
|
|
|
|
color: Colors.black,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
centerTitle: true,
|
|
|
|
iconTheme: IconThemeData(color: Colors.black),
|
|
|
|
),
|
|
|
|
body: Container(
|
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(30.0, 0.0, 30.0, 0.0),
|
|
|
|
child: ListView(
|
|
|
|
children: <Widget>[
|
2019-07-05 21:56:15 +00:00
|
|
|
// Container(
|
|
|
|
// alignment: Alignment.topCenter,
|
|
|
|
// child: Padding(
|
|
|
|
// padding: EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 15.0),
|
|
|
|
// child: Text(
|
|
|
|
// "Required fields are in bold",
|
|
|
|
// style: TextStyle(fontSize: 20.0, color: Colors.black),
|
|
|
|
// ),
|
|
|
|
// )),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0),
|
|
|
|
child : Text(
|
|
|
|
"Time of Transaction",
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
|
|
|
color: Colors.black,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
|
|
|
),
|
2019-05-16 16:04:08 +00:00
|
|
|
DateTimePickerFormField(
|
|
|
|
inputType: InputType.both,
|
2019-07-05 21:56:15 +00:00
|
|
|
format: DateFormat("dd/MM/yyyy 'at' hh:mm"),
|
2019-05-16 16:04:08 +00:00
|
|
|
editable: true,
|
2019-05-21 14:30:14 +00:00
|
|
|
controller: _timeController,
|
2019-05-16 16:04:08 +00:00
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: 'Date/Time of Transaction', hasFloatingPlaceholder: false),
|
|
|
|
onChanged: (dt) => setState(() => date = dt),
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
|
|
|
Padding(
|
2019-07-05 21:56:15 +00:00
|
|
|
padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0),
|
2019-05-08 18:54:14 +00:00
|
|
|
child: Text(
|
2019-05-10 11:43:45 +00:00
|
|
|
"Amount",
|
2019-05-08 18:54:14 +00:00
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
|
|
|
color: Colors.black,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
|
|
|
|
child: TextField(
|
2019-05-10 11:43:45 +00:00
|
|
|
controller: _amountController,
|
2019-05-08 18:54:14 +00:00
|
|
|
decoration: InputDecoration(
|
2019-05-16 16:04:08 +00:00
|
|
|
hintText: 'Value in £',
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
2019-07-05 12:39:24 +00:00
|
|
|
// obscureText: true,
|
|
|
|
autocorrect: false,
|
|
|
|
keyboardType: TextInputType.number,
|
2019-05-08 18:54:14 +00:00
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
2019-05-16 16:04:08 +00:00
|
|
|
color: Colors.grey[800],
|
2019-05-08 18:54:14 +00:00
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
2019-07-05 12:39:24 +00:00
|
|
|
onSubmitted: (_) {
|
2019-07-05 21:56:15 +00:00
|
|
|
FocusScope.of(context).requestFocus(focusNode);
|
|
|
|
// submitReceipt(_amountController.text, _timeController.text);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0),
|
|
|
|
|
|
|
|
child : Container (
|
|
|
|
height: 22, // this should be the same height as text
|
|
|
|
|
|
|
|
child : ListView(
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
children: <Widget>[
|
|
|
|
|
|
|
|
Container(
|
|
|
|
child: Text(
|
|
|
|
"Organization Name",
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
|
|
|
color: Colors.black,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
Container(
|
|
|
|
child : Padding(
|
2019-07-08 10:17:45 +00:00
|
|
|
padding: EdgeInsets.fromLTRB(5,0,0,4), // sorry about hardcoded constraints
|
2019-07-05 21:56:15 +00:00
|
|
|
child: FlatButton(
|
|
|
|
onPressed: () {
|
|
|
|
debugPrint("TODO: 'find organisation' dialog");
|
2019-07-08 10:17:45 +00:00
|
|
|
|
|
|
|
findOrganisations(_orgController.text);
|
2019-07-08 10:53:10 +00:00
|
|
|
|
2019-07-05 21:56:15 +00:00
|
|
|
},
|
|
|
|
child: Text("Find",
|
|
|
|
style:
|
|
|
|
TextStyle(color: Colors.blue, fontSize: 18.0)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
|
|
|
|
child: TextField(
|
|
|
|
controller: _orgController,
|
|
|
|
focusNode: focusNode,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: 'Eg. Pear Trading',
|
|
|
|
),
|
|
|
|
// obscureText: true,
|
|
|
|
autocorrect: true,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
|
|
|
color: Colors.grey[800],
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
|
|
|
onSubmitted: (_) {
|
|
|
|
submitReceipt(_amountController.text,
|
|
|
|
_timeController.text, _orgController.text);
|
2019-07-08 10:53:10 +00:00
|
|
|
// TODO: make sure organisation is valid
|
2019-07-05 21:56:15 +00:00
|
|
|
// TODO: Add 'find organisation' button which displays a dialog to, well, find the organisation's address or manual entry
|
2019-07-05 12:39:24 +00:00
|
|
|
},
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
|
|
|
),
|
2019-07-05 14:34:39 +00:00
|
|
|
|
|
|
|
Padding(
|
2019-07-05 21:56:15 +00:00
|
|
|
padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0),
|
2019-07-05 14:34:39 +00:00
|
|
|
|
|
|
|
child : Container (
|
|
|
|
height: 18,
|
|
|
|
|
|
|
|
child : ListView(
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
child: Text(
|
|
|
|
"Essential Purchase",
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
|
|
|
color: Colors.black,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
Container(
|
|
|
|
child : Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(20.0, 0.0, 0, 0),
|
|
|
|
|
2019-07-05 21:56:15 +00:00
|
|
|
child: Checkbox(value:
|
|
|
|
_essentialController.text.toLowerCase() == 'true',
|
|
|
|
onChanged: (bool newValue) {
|
2019-07-05 14:34:39 +00:00
|
|
|
setState(() {
|
2019-07-05 21:56:15 +00:00
|
|
|
_essentialController.text =
|
|
|
|
convertBoolToString(newValue);
|
2019-07-05 14:34:39 +00:00
|
|
|
});
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
|
2019-05-08 18:54:14 +00:00
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0.0, 70.0, 0.0, 0.0),
|
|
|
|
child: Container(
|
|
|
|
height: 65.0,
|
|
|
|
child: RaisedButton(
|
|
|
|
onPressed: () {
|
2019-07-05 21:56:15 +00:00
|
|
|
submitReceipt(_amountController.text, _timeController.text, _orgController.text);
|
2019-05-08 18:54:14 +00:00
|
|
|
},
|
2019-05-21 14:30:14 +00:00
|
|
|
child: Text("SUBMIT",
|
2019-05-08 18:54:14 +00:00
|
|
|
style:
|
|
|
|
TextStyle(color: Colors.white, fontSize: 22.0)),
|
|
|
|
color: Colors.blue,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|