diff --git a/lib/serverlist.dart b/lib/serverlist.dart index b5bc1e8..276a6ae 100644 --- a/lib/serverlist.dart +++ b/lib/serverlist.dart @@ -27,9 +27,7 @@ class ServerList extends StatefulWidget { } class _ServerListState extends State { - - - List server = [ + List servers = [ new Server( "example.com", "http://example.com", @@ -51,27 +49,34 @@ class _ServerListState extends State { Timer timer; Random rnd = new Random(); + @override + void initState() { + super.initState(); - _ServerListState() { + _updateAllServers(); timer = new Timer.periodic(new Duration(seconds: 5), (timer) { - server.forEach(_updateServerStatus); + _updateAllServers(); }); } - void _updateServerStatus(Server server) { - setState(() { - server.status = - ServerStatus.values[rnd.nextInt(ServerStatus.values.length - 1) + 1]; + Future _updateAllServers() async { + servers.forEach(_updateServerStatus); + } + + void _updateServerStatus(Server s) async { + checkStatus(s.uri).then((value) { + setState(() { + checkStatus(s.uri).then((value){ + s.status=value; + }); + }); }); } void _updateUI() { - setState(() { - - }); + setState(() {}); } - @override Widget build(BuildContext context) { // This method is rerun every time setState is called, for instance as done @@ -116,15 +121,11 @@ class _ServerListState extends State { ); } - List _createChildren() { - return new List.generate(server.length, (int index) { + return new List.generate(servers.length, (int index) { return Container( height: 75, - width: MediaQuery - .of(context) - .size - .width, + width: MediaQuery.of(context).size.width, margin: EdgeInsets.all(2.0), padding: EdgeInsets.all(10.0), decoration: BoxDecoration( @@ -138,7 +139,7 @@ class _ServerListState extends State { child: Align( alignment: Alignment.centerLeft, child: Text( - server[index].displayName, + servers[index].displayName, style: TextStyle(fontSize: 24), ), ), @@ -147,7 +148,7 @@ class _ServerListState extends State { width: 75, child: Align( alignment: Alignment.center, - child: _generateIcon(server[index]), + child: _generateIcon(servers[index]), // child: Text( // status[index], // style: TextStyle(fontSize: 24), @@ -167,15 +168,13 @@ class _ServerListState extends State { print("The URI was invalid, returning DNS_ERROR"); return ServerStatus.dnserror; } - if (parsedUri.isScheme("http") || parsedUri.isScheme("http")) { - - } + if (parsedUri.isScheme("http") || parsedUri.isScheme("http")) {} try { InternetAddress ia = InternetAddress(parsedUri.host); - parsedUri.scheme + //parsedUri.scheme } catch (ArgumentError) { - List record = await DnsUtils.lookupRecord( - parsedUri.host, RRecordType.ANY); + List record = + await DnsUtils.lookupRecord(parsedUri.host, RRecordType.ANY); } return ServerStatus.responding; @@ -198,4 +197,4 @@ class _ServerListState extends State { color: Colors.red, ); } -} \ No newline at end of file +}