diff options
| -rw-r--r-- | lib/data.dart | 27 | ||||
| -rw-r--r-- | lib/ui.dart | 10 |
2 files changed, 18 insertions, 19 deletions
diff --git a/lib/data.dart b/lib/data.dart index 93c523d..c0d640f 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -3,12 +3,12 @@ import 'dart:async'; import 'package:flutter/foundation.dart'; class AppState with ChangeNotifier { - List<Contact> contacts = <Contact>[ - Contact(address: "Living Room Window", isOpen: false), - Contact(address: "Front Door", isOpen: true), - Contact(address: "Back Door", isOpen: false), - Contact(address: "Garage Window", isOpen: true), - ]; + Map<String, bool> contacts = <String, bool>{ + 'Living Room Window': false, + 'Front Door': true, + 'Back Door': false, + 'Garage Window': true, + }; Timer? _timer; @@ -23,8 +23,12 @@ class AppState with ChangeNotifier { return; } - contacts[0].isOpen = !contacts[0].isOpen; - notifyListeners(); + const String choosen = 'Living Room Window'; + bool? state = contacts[choosen]; + if (state != null) { + contacts[choosen] = !state; + notifyListeners(); + } } @override @@ -33,10 +37,3 @@ class AppState with ChangeNotifier { super.dispose(); } } - -class Contact { - final String address; - bool isOpen; - - Contact({required this.address, required this.isOpen}); -} diff --git a/lib/ui.dart b/lib/ui.dart index d4618cf..4a513c6 100644 --- a/lib/ui.dart +++ b/lib/ui.dart @@ -26,13 +26,15 @@ class ContactList extends StatelessWidget { return ListView.builder( itemCount: state.contacts.length, itemBuilder: (BuildContext context, int index) { - final Contact contact = state.contacts[index]; + MapEntry<String, bool> data = state.contacts.entries.elementAt(index); + String address = data.key; + bool isOpen = data.value; return ListTile( leading: Icon( - contact.isOpen ? Icons.meeting_room : Icons.door_front_door, - color: contact.isOpen ? Colors.red : Colors.green, + isOpen ? Icons.meeting_room : Icons.door_front_door, + color: isOpen ? Colors.red : Colors.green, ), - title: Text(contact.address), + title: Text(address), ); }, ); |
