diff --git a/lib/serverlist.dart b/lib/serverlist.dart index 379455f..50e3a6e 100644 --- a/lib/serverlist.dart +++ b/lib/serverlist.dart @@ -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 { void _settings() {} final _formKey = GlobalKey(); - void _addServer() { + void _addServerForm() { TextEditingController servername = TextEditingController(); TextEditingController uri = TextEditingController(); showDialog( @@ -162,12 +160,18 @@ class _ServerListState extends State { mainAxisSize: MainAxisSize.min, children: [ 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 { 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 { ); } + 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 _createChildren() { List list = new List.generate(servers.length, (int index) { Widget info = Row(children: [ @@ -222,11 +240,84 @@ class _ServerListState extends State { 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( + 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: [ + 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 { size: 42, color: Colors.black, ), - onPressed: _addServer, + onPressed: _addServerForm, ), ), ); @@ -381,8 +472,7 @@ class _ServerListState extends State { 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: {"Location": location}) .timeout(new Duration(seconds: 3));