When creating a server you now have to choose from http:// or https:// and you now see what the icon means #33

Merged
Hugo Bayer merged 1 commit from Hugo/30012020/http/sDropdownAndAddedADescriptionForSatuses into master 2020-01-30 09:32:32 +00:00
2 changed files with 106 additions and 20 deletions

View file

@ -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<ProtocolDropdown> {
_ProtocolDropdownState(this.protocol);
String protocol = 'https://';
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: protocol,
iconSize: 0,
onChanged: (String newValue) {
setState(() {
protocol = newValue;
});
},
items: <String>['https://','http://']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
);
}
}

View file

@ -5,6 +5,7 @@ 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:server_pinger/protocol_dropdown.dart';
import 'package:server_pinger/server.dart'; import 'package:server_pinger/server.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -71,6 +72,7 @@ class _ServerListState extends State<ServerList> {
void _addServerForm() { void _addServerForm() {
TextEditingController serverName = TextEditingController(); TextEditingController serverName = TextEditingController();
TextEditingController uri = TextEditingController(); TextEditingController uri = TextEditingController();
ProtocolDropdown dropdown = ProtocolDropdown();
showDialog<String>( showDialog<String>(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
@ -89,12 +91,18 @@ class _ServerListState extends State<ServerList> {
), ),
controller: serverName, controller: serverName,
), ),
TextFormField( Row(
controller: uri, children: [
decoration: InputDecoration( dropdown,
labelText: 'URL', Expanded(
hintText: 'https://example.com', child: TextFormField(
), controller: uri,
decoration: InputDecoration(
hintText: 'example.com',
),
),
),
],
), ),
Padding( Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
@ -104,7 +112,8 @@ 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();
_addServer(serverName.text, uri.text); _addServer(serverName.text,
dropdown.state.protocol + uri.text);
} }
}, },
), ),
@ -134,9 +143,13 @@ class _ServerListState extends State<ServerList> {
} }
void _modifyServerForm(Server server) { void _modifyServerForm(Server server) {
TextEditingController servername = ProtocolDropdown dropdown = ProtocolDropdown(
protocol: server.uri.split('//')[0] + '//',
);
TextEditingController serverName =
TextEditingController(text: server.displayName); TextEditingController(text: server.displayName);
TextEditingController uri = TextEditingController(text: server.uri); TextEditingController uri = TextEditingController(
text: server.uri.substring(server.uri.indexOf('//') + 2));
showDialog<String>( showDialog<String>(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
@ -152,13 +165,17 @@ class _ServerListState extends State<ServerList> {
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Display name', labelText: 'Display name',
), ),
controller: servername, controller: serverName,
), ),
TextFormField( Row(
controller: uri, children: [
decoration: InputDecoration( dropdown,
labelText: 'URL', Expanded(
), child: TextFormField(
controller: uri,
),
),
],
), ),
Padding( Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
@ -179,8 +196,8 @@ class _ServerListState extends State<ServerList> {
if (_formKey.currentState.validate()) { if (_formKey.currentState.validate()) {
_formKey.currentState.save(); _formKey.currentState.save();
setState(() { setState(() {
server.displayName = servername.text; server.displayName = serverName.text;
server.uri = uri.text; server.uri = dropdown.state.protocol + uri.text;
}); });
Navigator.of(context).pop(); Navigator.of(context).pop();
_updateServerStatus(server); _updateServerStatus(server);
@ -232,12 +249,19 @@ class _ServerListState extends State<ServerList> {
border: Border(top: BorderSide(color: Colors.grey))), border: Border(top: BorderSide(color: Colors.grey))),
padding: EdgeInsets.fromLTRB(0, 0, 0, 5), padding: EdgeInsets.fromLTRB(0, 0, 0, 5),
), ),
Align(
child: Text(_getStatusString(servers[index].status)),
alignment: Alignment.centerLeft,
),
Row(children: [ Row(children: [
Expanded( Expanded(
child: Align(
alignment: Alignment.topLeft,
child: Text( child: Text(
_truncateURI(servers[index].uri), _truncateURI(servers[index].uri),
textAlign: TextAlign.left, ),
)), ),
),
IconButton( IconButton(
alignment: Alignment.center, alignment: Alignment.center,
icon: Icon( icon: Icon(
@ -496,4 +520,23 @@ class _ServerListState extends State<ServerList> {
.then((set) {}); .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 '';
}
}
} }