This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
LocalSpend-Tracker/lib/common/widgets/organisations_dialog.dart

169 lines
5.9 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:local_spend/common/apifunctions/find_organisations.dart';
class FindOrganisations {
TextField getSearchBar(TextEditingController controller, String hintText) {
return TextField(
controller: controller,
decoration: InputDecoration(
hintText: hintText,
icon: Icon(Icons.search),
),
);
}
2019-08-07 13:02:36 +00:00
// todo: get all organisations, favourites and all data from one 'organisations' class or similar
// eg items: organisations.getFavourites().orderBy(name),
Future<Organisation> dialog(context) {
2019-08-09 13:32:47 +00:00
bool _searchEnabled = false;
2019-08-07 14:32:52 +00:00
TextEditingController searchBarText = new TextEditingController();
var organisations = new Organisations();
var listTitle = "All Organisations";
2019-08-12 09:42:17 +00:00
// var organisationsList = organisations.getTestData();
var organisationsList = List<Organisation>();
2019-08-07 14:32:52 +00:00
2019-08-12 09:15:36 +00:00
Future<int> _submitSearch(String search) async {
2019-08-09 13:32:47 +00:00
_searchEnabled = false;
2019-08-07 14:32:52 +00:00
listTitle = "Results for \'" + search + "\'";
2019-08-12 09:15:36 +00:00
var futureOrgs = await organisations.findOrganisations(search);
// futureOrgs.then((value) {
// debugPrint("There are " + futureOrgs.length.toString() +
// " payees matching the query \'" + search + "\'.");
2019-08-12 09:15:36 +00:00
organisationsList = futureOrgs;
2019-08-09 13:32:47 +00:00
_searchEnabled = true;
2019-08-12 09:15:36 +00:00
return futureOrgs.length;
// });
2019-08-07 14:32:52 +00:00
}
_chosenOrg(Organisation chosen) {
// debugPrint(chosen.name + " tapped");
}
return showDialog<Organisation>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
2019-08-07 14:32:52 +00:00
return StatefulBuilder(
builder: (context, setState) {
return SimpleDialog(
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
child: Row(
children: [
Container(
2019-08-09 13:32:47 +00:00
width: 140,
2019-08-07 14:32:52 +00:00
height: 50,
child: TextField(
controller: searchBarText,
decoration: InputDecoration(
hintText: "Payee Name",
),
2019-08-09 13:32:47 +00:00
onChanged: (value) {
if (value.length > 0) {
_searchEnabled = true;
} else {
_searchEnabled = false;
}
2019-08-12 14:18:01 +00:00
setState(() => {_searchEnabled});
2019-08-07 14:32:52 +00:00
},
2019-08-12 14:18:01 +00:00
onSubmitted: (_) {
2019-08-09 13:32:47 +00:00
if (_searchEnabled) {
2019-08-12 09:15:36 +00:00
var result = _submitSearch(searchBarText.text);
result.then((_) {
setState(() {});
});
2019-08-09 13:32:47 +00:00
}
},
2019-08-07 14:32:52 +00:00
),
),
Container(
width: 80,
padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
2019-08-09 13:32:47 +00:00
2019-08-12 14:18:01 +00:00
child: RaisedButton(
onPressed: (() {
if (_searchEnabled) {
var result = _submitSearch(searchBarText.text);
result.then((_) {
setState(() {});
});
}
}),
child: Icon(Icons.search, color: Colors.white),
color: _searchEnabled ? Colors.blue : Colors.blue[200],
// make inactive when search in progress as activity indicator
2019-08-07 14:32:52 +00:00
),
),
],
),
),
Container(
padding: EdgeInsets.fromLTRB(20, 20, 20, 0),
child: Text(
listTitle,
style: new TextStyle(
fontSize: 23, fontWeight: FontWeight.bold),
),
),
Container(
padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
width: MediaQuery
.of(context)
.size
.width,
2019-08-07 14:32:52 +00:00
height: MediaQuery
.of(context)
.size
.height * 0.67,
child: Material(
shadowColor: Colors.transparent,
color: Colors.transparent,
child: ListView.builder(
itemCount: organisationsList.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
leading: Icon(Icons.person),
title: Text(organisationsList[index].name, style: new TextStyle(fontSize: 18)),
subtitle: Text(organisationsList[index].postcode.toUpperCase()),
// trailing: Icon(Icons.arrow_forward_ios),
// onTap: _chosenOrg(organisationsList[index]),
onTap: (){
Navigator.of(context).pop(organisationsList[index]);
_chosenOrg(organisationsList[index]);
},
onLongPress: (){
// show more details about the organisation in a new dialog
},
),
2019-08-07 14:32:52 +00:00
);
},
),
),
),
2019-08-07 14:32:52 +00:00
// help button for if org not listed
// cancel and ok buttons
2019-08-07 14:32:52 +00:00
],
);
},
);
},
);
}
}