diff --git a/lib/server.dart b/lib/server.dart index c6d694f..6b42b2e 100644 --- a/lib/server.dart +++ b/lib/server.dart @@ -5,9 +5,9 @@ enum ServerStatus{ class Server { String uri; String displayName; - ServerStatus status; + ServerStatus status = ServerStatus.unknown; - Server(this.uri, this.displayName); + Server(this.displayName,this.uri,); } \ No newline at end of file diff --git a/lib/serverlist.dart b/lib/serverlist.dart index 991f0dd..1758cb6 100644 --- a/lib/serverlist.dart +++ b/lib/serverlist.dart @@ -1,6 +1,8 @@ import 'dart:async'; +import 'dart:math'; import 'package:flutter/material.dart'; +import 'package:server_pinger/server.dart'; class ServerList extends StatefulWidget { ServerList({Key key, this.title}) : super(key: key); @@ -21,10 +23,52 @@ class ServerList extends StatefulWidget { } class _ServerListState extends State { - void _incrementCounter() { - setState(() {}); + + + List server = [ + new Server( + "example.com", + "http://example.com", + ), + new Server( + "youtube.com", + "https://youtube.com", + ), + new Server( + "My VPS", + "https://vps1.kescher.at", + ), + new Server( + "Raspberry Pi", + "https://pi.kescher.at/isup", + ) + ]; + + Timer timer; + Random rnd = new Random(); + + + _ServerListState(){ + + timer = new Timer.periodic(new Duration(seconds: 5), (timer){ + server.forEach(_updateServerStatus); + }); } + void _updateServerStatus(Server server){ + setState(() { + server.status=ServerStatus.values[rnd.nextInt(ServerStatus.values.length-1)+1]; + }); + } + + void _updateUI() { + setState(() { + + }); + } + + + @override Widget build(BuildContext context) { // This method is rerun every time setState is called, for instance as done @@ -62,31 +106,24 @@ class _ServerListState extends State { ), ), floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, + onPressed: _updateUI, tooltip: 'Increment', child: Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. ); } + + List _createChildren() { - List shownIdentifier = [ - "example.com", - "youtube.com", - "My VPS", - "Raspberry Pi" - ]; - List url = [ - "http://example.com", - "https://youtube.com", - "https://vps1.kescher.at", - "https://pi.kescher.at/isup" - ]; List status = ["ON", "OFF?", "ON", "ON"]; - return new List.generate(shownIdentifier.length, (int index) { + return new List.generate(server.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( @@ -100,7 +137,7 @@ class _ServerListState extends State { child: Align( alignment: Alignment.centerLeft, child: Text( - shownIdentifier[index], + server[index].displayName, style: TextStyle(fontSize: 24), ), ), @@ -109,7 +146,7 @@ class _ServerListState extends State { width: 75, child: Align( alignment: Alignment.center, - child: _generateIcon(), + child: _generateIcon(server[index]), // child: Text( // status[index], // style: TextStyle(fontSize: 24), @@ -121,12 +158,22 @@ class _ServerListState extends State { }); } - Icon _generateIcon() { - Icon icon = new Icon( - Icons.check_box_outline_blank, - color: Colors.yellow, - ); - new Timer.periodic(new Duration(milliseconds: 500), (timer) {}); - return icon; + Icon _generateIcon(Server server) { + if (server.status == ServerStatus.unknown) + return new Icon( + Icons.check_box_outline_blank, + color: Colors.yellow, + ); + else if(server.status == ServerStatus.responding) + return new Icon( + Icons.check_box, + color: Colors.green, + ); + else + return new Icon( + Icons.indeterminate_check_box, + color: Colors.red, + ); } + }