Styling changes #21

Merged
Jeremy Kescher merged 5 commits from jeremy/0128/styling™ into master 2020-01-29 10:38:20 +00:00
4 changed files with 136 additions and 249 deletions

View file

@ -1,86 +0,0 @@
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

View file

@ -5,23 +5,11 @@ import 'serverlist.dart';
void main() => runApp(MyApp()); void main() => runApp(MyApp());
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Server pinger', title: 'Server pinger',
theme: ThemeData( theme: ThemeData(primaryColor: Colors.red.shade900),
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primaryColor: Colors.red.shade900
),
home: ServerList(title: 'Server list'), home: ServerList(title: 'Server list'),
); );
} }

View file

@ -1,4 +1,11 @@
enum ServerStatus { unknown, ok, notok, timeout, dnserror } enum ServerStatus {
unknown,
ok,
notOk,
timeout,
dnsError,
clientConnectivityIssues
}
class Server { class Server {
String displayName; String displayName;

View file

@ -61,36 +61,12 @@ class _ServerListState extends State<ServerList> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title), title: Text(widget.title),
), ),
body: SingleChildScrollView( body: SingleChildScrollView(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column( child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: _createChildren(), children: _createChildren(),
), ),
@ -124,8 +100,7 @@ class _ServerListState extends State<ServerList> {
labelStyle: TextStyle(fontSize: 18.0), labelStyle: TextStyle(fontSize: 18.0),
onTap: _addServer, onTap: _addServer,
), ),
],*/ ],*/ // This trailing comma makes auto-formatting nicer for build methods.
// This trailing comma makes auto-formatting nicer for build methods.
); );
} }
@ -133,13 +108,13 @@ class _ServerListState extends State<ServerList> {
final _formKey = GlobalKey<FormState>(); final _formKey = GlobalKey<FormState>();
void _addServerForm() { void _addServerForm() {
TextEditingController servername = TextEditingController(); TextEditingController serverName = TextEditingController();
TextEditingController uri = TextEditingController(); TextEditingController uri = TextEditingController();
showDialog<String>( showDialog<String>(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
return AlertDialog( return AlertDialog(
title: Text('Adding a server'), title: Text('Add server'),
content: SingleChildScrollView( content: SingleChildScrollView(
child: Form( child: Form(
key: _formKey, key: _formKey,
@ -148,15 +123,15 @@ class _ServerListState extends State<ServerList> {
children: <Widget>[ children: <Widget>[
TextFormField( TextFormField(
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Custom Name', labelText: 'Display name',
hintText: 'Optional', hintText: 'Optional',
), ),
controller: servername, controller: serverName,
), ),
TextFormField( TextFormField(
controller: uri, controller: uri,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'URI', labelText: 'URL',
hintText: 'https://example.com', hintText: 'https://example.com',
), ),
), ),
@ -169,9 +144,9 @@ class _ServerListState extends State<ServerList> {
uri.text.isNotEmpty) { uri.text.isNotEmpty) {
_formKey.currentState.save(); _formKey.currentState.save();
_addServer( _addServer(
servername.text.isEmpty serverName.text.isEmpty
? uri.text ? uri.text
: servername.text, : serverName.text,
uri.text); uri.text);
} }
}, },
@ -200,13 +175,14 @@ class _ServerListState extends State<ServerList> {
} }
void _modifyServerForm(Server server) { void _modifyServerForm(Server server) {
TextEditingController servername = TextEditingController(text: server.displayName); TextEditingController servername =
TextEditingController(text: server.displayName);
TextEditingController uri = TextEditingController(text: server.uri); TextEditingController uri = TextEditingController(text: server.uri);
showDialog<String>( showDialog<String>(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
return AlertDialog( return AlertDialog(
title: Text('Adding a server'), title: Text('Edit server'),
content: SingleChildScrollView( content: SingleChildScrollView(
child: Form( child: Form(
key: _formKey, key: _formKey,
@ -215,14 +191,14 @@ class _ServerListState extends State<ServerList> {
children: <Widget>[ children: <Widget>[
TextFormField( TextFormField(
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Custom Name', labelText: 'Display name',
), ),
controller: servername, controller: servername,
), ),
TextFormField( TextFormField(
controller: uri, controller: uri,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'URI', labelText: 'URL',
), ),
), ),
Padding( Padding(
@ -262,6 +238,7 @@ class _ServerListState extends State<ServerList> {
}, },
); );
} }
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: [
@ -292,18 +269,24 @@ 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),
), ),
Column(children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
servers[index].uri,
textAlign: TextAlign.left,
),
),
Row(children: [ Row(children: [
Expanded( Expanded(
child: IconButton( child: Text(
alignment: Alignment.centerLeft, _truncateURI(servers[index].uri),
textAlign: TextAlign.left,
)),
IconButton(
alignment: Alignment.center,
icon: Icon(
Icons.edit,
color: Colors.grey,
),
onPressed: () {
_modifyServerForm(servers[index]);
},
),
IconButton(
alignment: Alignment.center,
icon: Icon( icon: Icon(
Icons.delete_forever, Icons.delete_forever,
color: Colors.red, color: Colors.red,
@ -356,17 +339,6 @@ class _ServerListState extends State<ServerList> {
); );
}, },
), ),
),
IconButton(
icon: Icon(
Icons.edit,
color: Colors.grey,
),
onPressed: () {
_modifyServerForm(servers[index]);
},
),
]),
]), ]),
], ],
); );
@ -422,59 +394,59 @@ class _ServerListState extends State<ServerList> {
try { try {
parsedUri = Uri.parse(uri); parsedUri = Uri.parse(uri);
} catch (FormatException) { } catch (FormatException) {
return ServerStatus.dnserror; return ServerStatus.dnsError;
} }
try { try {
// Check if host is IP address // Check if host is IP address
InternetAddress(parsedUri.host); InternetAddress(parsedUri.host);
} catch (ArgumentError) /* Host is a DNS name */ { } catch (ArgumentError) /* Host is a DNS name */ {
var v4recs, v6recs;
try { try {
var v4recs = await DnsUtils.lookupRecord(parsedUri.host, RRecordType.A); v4recs = await DnsUtils.lookupRecord(parsedUri.host, RRecordType.A);
var v6recs = v6recs = await DnsUtils.lookupRecord(parsedUri.host, RRecordType.AAAA);
await DnsUtils.lookupRecord(parsedUri.host, RRecordType.AAAA); } catch (SocketException) {
return ServerStatus.clientConnectivityIssues;
}
var v4 = await _resolveHostname(v4recs, RRecordType.A); var v4 = await _resolveHostname(v4recs, RRecordType.A);
var v6 = await _resolveHostname(v6recs, RRecordType.AAAA); var v6 = await _resolveHostname(v6recs, RRecordType.AAAA);
if (v4 != "" && v6 != "") { if (v4 == null && v6 == null) {
return ServerStatus.dnserror; return ServerStatus.dnsError;
}
} catch (SocketException) {
return ServerStatus.dnserror;
} }
} }
try {
return await _checkResponse(parsedUri); return await _checkResponse(parsedUri);
} catch (TimeoutException) {
return ServerStatus.timeout;
}
} }
Icon _generateIcon(Server server) { Icon _generateIcon(Server server) {
var icon, color; var icon, color;
switch (server.status) { switch (server.status) {
case ServerStatus.unknown: case ServerStatus.unknown:
icon = Icons.timer; icon = Icons.cloud_queue;
color = Colors.blueGrey.shade400; color = Colors.blueGrey.shade400;
break; break;
case ServerStatus.ok: case ServerStatus.ok:
icon = Icons.check_box; icon = Icons.cloud;
color = Colors.green.shade400; color = Colors.green.shade400;
break; break;
case ServerStatus.notok: case ServerStatus.notOk:
icon = Icons.indeterminate_check_box; icon = Icons.cloud_off;
color = Colors.red; color = Colors.red.shade400;
break; break;
case ServerStatus.timeout: case ServerStatus.timeout:
icon = Icons.timer_off; icon = Icons.timer_off;
color = Colors.yellowAccent.shade700; color = Colors.yellowAccent.shade700;
break; break;
case ServerStatus.dnserror: case ServerStatus.dnsError:
icon = Icons.dns; icon = Icons.dns;
color = Colors.red.shade400; color = Colors.red.shade400;
break; break;
case ServerStatus.clientConnectivityIssues:
icon = Icons.signal_wifi_off;
color = Colors.red.shade400;
break;
default: default:
icon = Icons.indeterminate_check_box; icon = Icons.device_unknown;
color = Colors.red; color = Colors.blueGrey.shade400;
break; break;
} }
return new Icon(icon, color: color); return new Icon(icon, color: color);
@ -482,7 +454,7 @@ class _ServerListState extends State<ServerList> {
static _resolveHostname(List<RRecord> records, RRecordType type) { static _resolveHostname(List<RRecord> records, RRecordType type) {
String ipAddress; String ipAddress;
if (records == null) return ServerStatus.dnserror; if (records == null) return null;
records.forEach((record) { records.forEach((record) {
if (ipAddress != null) { if (ipAddress != null) {
@ -490,24 +462,30 @@ class _ServerListState extends State<ServerList> {
} }
if (ipAddress == null) { if (ipAddress == null) {
// ignore: unrelated_type_equality_checks
if (record.rType == type) {
ipAddress = record.data; ipAddress = record.data;
} }
}
}); });
if (ipAddress == null) ipAddress = "";
return ipAddress; return ipAddress;
} }
static Future<ServerStatus> _checkResponse(Uri uri) async { static Future<ServerStatus> _checkResponse(Uri uri) async {
var response = await HttpUtils.getForFullResponse(uri.toString()) var response;
try {
response = await HttpUtils.getForFullResponse(uri.toString())
.timeout(new Duration(seconds: 3)); .timeout(new Duration(seconds: 3));
} catch (TimeoutException) {
return ServerStatus.timeout;
}
if (response.statusCode > 199 && response.statusCode < 300) { if (response.statusCode > 199 && response.statusCode < 300) {
return ServerStatus.ok; return ServerStatus.ok;
} else { } else {
return ServerStatus.notok; return ServerStatus.notOk;
} }
} }
String _truncateURI(String uri) {
if (uri.length < 100) return uri;
return uri.substring(0, 100) + "...";
}
} }