added the ability to delete Servers #16

Merged
Hugo Bayer merged 1 commit from Hugo/103029012020/MadeEntriesDeletable into master 2020-01-29 09:31:33 +00:00

View file

@ -1,11 +1,9 @@
import 'dart:async'; import 'dart:async';
import 'dart:math';
import 'dart:io'; import 'dart:io';
import 'dart:math';
import 'package:basic_utils/basic_utils.dart'; import 'package:basic_utils/basic_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:http/src/response.dart';
import 'package:server_pinger/server.dart'; import 'package:server_pinger/server.dart';
import 'server.dart'; import 'server.dart';
@ -147,7 +145,7 @@ class _ServerListState extends State<ServerList> {
void _settings() {} void _settings() {}
final _formKey = GlobalKey<FormState>(); final _formKey = GlobalKey<FormState>();
void _addServer() { void _addServerForm() {
TextEditingController servername = TextEditingController(); TextEditingController servername = TextEditingController();
TextEditingController uri = TextEditingController(); TextEditingController uri = TextEditingController();
showDialog<String>( showDialog<String>(
@ -162,12 +160,18 @@ class _ServerListState extends State<ServerList> {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: <Widget>[ children: <Widget>[
TextFormField( TextFormField(
controller: uri, decoration: InputDecoration(
labelText: 'Custom Name',
hintText: 'Optional',
),
controller: servername,
), ),
TextFormField( TextFormField(
controller: uri,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Custom Name', hintText: 'Optional'), labelText: 'URI',
controller: servername, hintText: 'https://example.com',
),
), ),
Padding( Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
@ -177,10 +181,11 @@ class _ServerListState extends State<ServerList> {
if (_formKey.currentState.validate() && if (_formKey.currentState.validate() &&
uri.text.isNotEmpty) { uri.text.isNotEmpty) {
_formKey.currentState.save(); _formKey.currentState.save();
Server server = Server(uri.text, uri.text); _addServer(
servers.add(server); servername.text.isEmpty
_updateServerStatus(server); ? uri.text
Navigator.of(context).pop(); : servername.text,
uri.text);
} }
}, },
), ),
@ -194,6 +199,19 @@ class _ServerListState extends State<ServerList> {
); );
} }
void _addServer(String customName, String uri) {
Server server = Server(customName, uri);
servers.add(server);
_updateServerStatus(server);
Navigator.of(context).pop();
}
void _removeServer(int index) {
setState(() {
servers.removeAt(index);
});
}
List<Widget> _createChildren() { List<Widget> _createChildren() {
List<Widget> list = new List<Widget>.generate(servers.length, (int index) { List<Widget> list = new List<Widget>.generate(servers.length, (int index) {
Widget info = Row(children: [ Widget info = Row(children: [
@ -222,11 +240,84 @@ class _ServerListState extends State<ServerList> {
Container( Container(
decoration: BoxDecoration( decoration: BoxDecoration(
border: Border(top: BorderSide(color: Colors.grey))), border: Border(top: BorderSide(color: Colors.grey))),
padding: EdgeInsets.fromLTRB(0, 0, 0, 5),
), ),
Text( Column(children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
servers[index].uri, servers[index].uri,
textAlign: TextAlign.left, textAlign: TextAlign.left,
), ),
),
Row(children: [
Expanded(
child: IconButton(
alignment: Alignment.centerLeft,
icon: Icon(
Icons.delete_forever,
color: Colors.red,
),
onPressed: () {
showDialog<String>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Are you shure you want to delete ' +
servers[index].displayName +
'?'),
content: Form(
key: _formKey,
child: Row(
children: <Widget>[
Expanded(
child: FlatButton(
child: Text(
'Yes',
style: TextStyle(
color: Colors.black,
fontSize: 24,
),
),
onPressed: () {
_removeServer(index);
Navigator.of(context).pop();
},
),
),
FlatButton(
child: Text(
'No',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 24,
),
),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
),
);
},
);
},
),
),
IconButton(
icon: Icon(
Icons.edit,
color: Colors.grey,
),
onPressed: () {
_removeServer(index);
},
),
]),
]),
], ],
); );
} }
@ -270,7 +361,7 @@ class _ServerListState extends State<ServerList> {
size: 42, size: 42,
color: Colors.black, color: Colors.black,
), ),
onPressed: _addServer, onPressed: _addServerForm,
), ),
), ),
); );
@ -381,8 +472,7 @@ class _ServerListState extends State<ServerList> {
var location = uri.path; var location = uri.path;
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
Response response = await HttpUtils.getForFullResponse( var response = await HttpUtils.getForFullResponse(scheme + "://" + host,
scheme + "://" + host,
headers: <String, String>{"Location": location}) headers: <String, String>{"Location": location})
.timeout(new Duration(seconds: 3)); .timeout(new Duration(seconds: 3));