Changed seperate string lists to one server list, added that every 5 seconds the icons are randomized finished the generate icon method #8

Merged
Hugo Bayer merged 1 commit from Hugo/0128/randomIcons into master 2020-01-28 09:12:41 +00:00
2 changed files with 75 additions and 28 deletions

View file

@ -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,);
}

View file

@ -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<ServerList> {
void _incrementCounter() {
setState(() {});
List<Server> 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<ServerList> {
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
onPressed: _updateUI,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
List<Widget> _createChildren() {
List<String> shownIdentifier = [
"example.com",
"youtube.com",
"My VPS",
"Raspberry Pi"
];
List<String> url = [
"http://example.com",
"https://youtube.com",
"https://vps1.kescher.at",
"https://pi.kescher.at/isup"
];
List<String> status = ["ON", "OFF?", "ON", "ON"];
return new List<Widget>.generate(shownIdentifier.length, (int index) {
return new List<Widget>.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<ServerList> {
child: Align(
alignment: Alignment.centerLeft,
child: Text(
shownIdentifier[index],
server[index].displayName,
style: TextStyle(fontSize: 24),
),
),
@ -109,7 +146,7 @@ class _ServerListState extends State<ServerList> {
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<ServerList> {
});
}
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,
);
}
}