From cd9144ac326f4be6c682f3869feb7d91197ecb42 Mon Sep 17 00:00:00 2001 From: Hugo Bayer Date: Thu, 30 Jan 2020 10:31:44 +0100 Subject: [PATCH] when creating a server you now have to choose from http:// or https:// and you now see what the icon means --- lib/protocol_dropdown.dart | 43 ++++++++++++++++++++ lib/serverlist.dart | 83 +++++++++++++++++++++++++++++--------- 2 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 lib/protocol_dropdown.dart diff --git a/lib/protocol_dropdown.dart b/lib/protocol_dropdown.dart new file mode 100644 index 0000000..637ecda --- /dev/null +++ b/lib/protocol_dropdown.dart @@ -0,0 +1,43 @@ +import 'package:flutter/material.dart'; + +class ProtocolDropdown extends StatefulWidget { + ProtocolDropdown({Key key,this.protocol='https://'}) : super(key: key); + + final String protocol; + + _ProtocolDropdownState state; + + @override + _ProtocolDropdownState createState(){ + state = _ProtocolDropdownState(protocol); + return state; + } +} + +class _ProtocolDropdownState extends State { + + _ProtocolDropdownState(this.protocol); + String protocol = 'https://'; + + @override + Widget build(BuildContext context) { + return DropdownButton( + value: protocol, + iconSize: 0, + onChanged: (String newValue) { + setState(() { + protocol = newValue; + }); + }, + items: ['https://','http://'] + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }) + .toList(), + ); + } + +} \ No newline at end of file diff --git a/lib/serverlist.dart b/lib/serverlist.dart index bfc1f41..836afef 100644 --- a/lib/serverlist.dart +++ b/lib/serverlist.dart @@ -5,6 +5,7 @@ import 'dart:math'; import 'package:basic_utils/basic_utils.dart'; import 'package:flutter/material.dart'; +import 'package:server_pinger/protocol_dropdown.dart'; import 'package:server_pinger/server.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -71,6 +72,7 @@ class _ServerListState extends State { void _addServerForm() { TextEditingController serverName = TextEditingController(); TextEditingController uri = TextEditingController(); + ProtocolDropdown dropdown = ProtocolDropdown(); showDialog( context: context, builder: (BuildContext context) { @@ -89,12 +91,18 @@ class _ServerListState extends State { ), controller: serverName, ), - TextFormField( - controller: uri, - decoration: InputDecoration( - labelText: 'URL', - hintText: 'https://example.com', - ), + Row( + children: [ + dropdown, + Expanded( + child: TextFormField( + controller: uri, + decoration: InputDecoration( + hintText: 'example.com', + ), + ), + ), + ], ), Padding( padding: const EdgeInsets.all(8.0), @@ -104,7 +112,8 @@ class _ServerListState extends State { if (_formKey.currentState.validate() && uri.text.isNotEmpty) { _formKey.currentState.save(); - _addServer(serverName.text, uri.text); + _addServer(serverName.text, + dropdown.state.protocol + uri.text); } }, ), @@ -134,9 +143,13 @@ class _ServerListState extends State { } void _modifyServerForm(Server server) { - TextEditingController servername = + ProtocolDropdown dropdown = ProtocolDropdown( + protocol: server.uri.split('//')[0] + '//', + ); + TextEditingController serverName = TextEditingController(text: server.displayName); - TextEditingController uri = TextEditingController(text: server.uri); + TextEditingController uri = TextEditingController( + text: server.uri.substring(server.uri.indexOf('//') + 2)); showDialog( context: context, builder: (BuildContext context) { @@ -152,13 +165,17 @@ class _ServerListState extends State { decoration: InputDecoration( labelText: 'Display name', ), - controller: servername, + controller: serverName, ), - TextFormField( - controller: uri, - decoration: InputDecoration( - labelText: 'URL', - ), + Row( + children: [ + dropdown, + Expanded( + child: TextFormField( + controller: uri, + ), + ), + ], ), Padding( padding: const EdgeInsets.all(8.0), @@ -179,8 +196,8 @@ class _ServerListState extends State { if (_formKey.currentState.validate()) { _formKey.currentState.save(); setState(() { - server.displayName = servername.text; - server.uri = uri.text; + server.displayName = serverName.text; + server.uri = dropdown.state.protocol + uri.text; }); Navigator.of(context).pop(); _updateServerStatus(server); @@ -232,12 +249,19 @@ class _ServerListState extends State { border: Border(top: BorderSide(color: Colors.grey))), padding: EdgeInsets.fromLTRB(0, 0, 0, 5), ), + Align( + child: Text(_getStatusString(servers[index].status)), + alignment: Alignment.centerLeft, + ), Row(children: [ Expanded( + child: Align( + alignment: Alignment.topLeft, child: Text( - _truncateURI(servers[index].uri), - textAlign: TextAlign.left, - )), + _truncateURI(servers[index].uri), + ), + ), + ), IconButton( alignment: Alignment.center, icon: Icon( @@ -496,4 +520,23 @@ class _ServerListState extends State { .then((set) {}); }); } + + String _getStatusString(ServerStatus status) { + switch (status) { + case ServerStatus.unknown: + return 'Loading...'; + case ServerStatus.ok: + return 'Reachable'; + case ServerStatus.notOk: + return 'Unexpected response'; + case ServerStatus.timeout: + return 'Timeout'; + case ServerStatus.dnsError: + return 'DNS issues'; + case ServerStatus.clientConnectivityIssues: + return 'Connectivity issues on your device'; + default: + return ''; + } + } } -- 2.47.0