1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'data.dart';
class Sia extends StatelessWidget {
const Sia({super.key});
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<AppState>(
create: (BuildContext context) => AppState(),
child: MaterialApp(
home: Consumer<AppState>(
builder: (_, AppState provider, _) {
if (provider.onConnectionPage) {
return const ConnectionPage();
}
return const DevicesPage();
}
),
),
);
}
}
class ConnectionPage extends StatelessWidget {
const ConnectionPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Connection")),
body: const Padding(
padding: EdgeInsets.all(16.0),
child: TextField(
decoration: InputDecoration(
labelText: "Server name",
hintText: "iot.example.org",
border: OutlineInputBorder(),
),
),
),
bottomNavigationBar: const ConnectionStatus(),
);
}
}
class DevicesPage extends StatelessWidget {
const DevicesPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Contacts")),
body: const Column(
children: <Widget>[
Expanded(child: ContactList()),
],
),
bottomNavigationBar: const ConnectionStatus(),
);
}
}
class ContactList extends StatelessWidget {
const ContactList({super.key});
@override
Widget build(BuildContext context) {
return Consumer<AppState>(
builder: (BuildContext context, AppState state, Widget? child) {
return ListView.builder(
itemCount: state.contacts.length,
itemBuilder: (BuildContext context, int index) {
MapEntry<String, bool> data = state.contacts.entries.elementAt(index);
String address = data.key;
bool isOpen = data.value;
return ListTile(
leading: Icon(
isOpen ? Icons.meeting_room : Icons.door_front_door,
color: isOpen ? Colors.red : Colors.green,
),
title: Text(address),
);
},
);
},
);
}
}
class ConnectionStatus extends StatelessWidget {
const ConnectionStatus({super.key});
@override
Widget build(BuildContext context) {
return SafeArea(
child: Consumer<AppState>(
builder: (BuildContext context, AppState state, Widget? child) {
Icon icon;
Text text;
if (state.brokerConnected && state.serverConnected) {
icon = const Icon(Icons.cloud, color: Colors.green);
text = const Text('Connected');
} else if (state.brokerConnected && !state.serverConnected) {
icon = const Icon(Icons.cloud_off, color: Colors.orange);
text = const Text('Connection issue');
} else {
icon = const Icon(Icons.cloud_off, color: Colors.red);
text = const Text('Disconnected');
}
return Row(
children: <Widget>[
Expanded(
child: ListTile(
leading: icon,
title: text,
trailing: ElevatedButton(
onPressed: state.togglePage,
child: const Text('switch view'),
),
),
),
],
);
},
),
);
}
}
|