added the ability to delete Servers #16

Merged
Hugo Bayer merged 1 commits from Hugo/103029012020/MadeEntriesDeletable into master 2020-01-29 09:31:33 +00:00
1 changed files with 108 additions and 18 deletions

View File

@ -1,11 +1,9 @@
import 'dart:async';
import 'dart:math';
import 'dart:io';
import 'dart:math';
import 'package:basic_utils/basic_utils.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 'server.dart';
@ -147,7 +145,7 @@ class _ServerListState extends State<ServerList> {
void _settings() {}
final _formKey = GlobalKey<FormState>();
void _addServer() {
void _addServerForm() {
TextEditingController servername = TextEditingController();
TextEditingController uri = TextEditingController();
showDialog<String>(
@ -162,12 +160,18 @@ class _ServerListState extends State<ServerList> {
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextFormField(
controller: uri,
decoration: InputDecoration(
labelText: 'Custom Name',
hintText: 'Optional',
),
controller: servername,
),
TextFormField(
controller: uri,
decoration: InputDecoration(
labelText: 'Custom Name', hintText: 'Optional'),
controller: servername,
labelText: 'URI',
hintText: 'https://example.com',
),
),
Padding(
padding: const EdgeInsets.all(8.0),
@ -177,10 +181,11 @@ class _ServerListState extends State<ServerList> {
if (_formKey.currentState.validate() &&
uri.text.isNotEmpty) {
_formKey.currentState.save();
Server server = Server(uri.text, uri.text);
servers.add(server);
_updateServerStatus(server);
Navigator.of(context).pop();
_addServer(
servername.text.isEmpty
? uri.text
: 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> list = new List<Widget>.generate(servers.length, (int index) {
Widget info = Row(children: [
@ -222,11 +240,84 @@ class _ServerListState extends State<ServerList> {
Container(
decoration: BoxDecoration(
border: Border(top: BorderSide(color: Colors.grey))),
padding: EdgeInsets.fromLTRB(0, 0, 0, 5),
),
Text(
servers[index].uri,
textAlign: TextAlign.left,
),
Column(children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
servers[index].uri,
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,
color: Colors.black,
),
onPressed: _addServer,
onPressed: _addServerForm,
),
),
);
@ -381,8 +472,7 @@ class _ServerListState extends State<ServerList> {
var location = uri.path;
for (int i = 0; i < 5; i++) {
Response response = await HttpUtils.getForFullResponse(
scheme + "://" + host,
var response = await HttpUtils.getForFullResponse(scheme + "://" + host,
headers: <String, String>{"Location": location})
.timeout(new Duration(seconds: 3));