summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/data.dart11
-rw-r--r--lib/main.dart9
-rw-r--r--lib/ui.dart74
-rw-r--r--pubspec.lock16
-rw-r--r--pubspec.yaml1
5 files changed, 53 insertions, 58 deletions
diff --git a/lib/data.dart b/lib/data.dart
index f2e3531..5cd59ec 100644
--- a/lib/data.dart
+++ b/lib/data.dart
@@ -1,3 +1,14 @@
+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),
+ ];
+}
+
class Contact {
final String address;
bool isOpen;
diff --git a/lib/main.dart b/lib/main.dart
index 805ef69..1601965 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -1,7 +1,14 @@
import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
import 'ui.dart';
+import 'data.dart';
void main() {
- runApp(const UI());
+ runApp(
+ ChangeNotifierProvider<AppState>(
+ create: (BuildContext context) => AppState(),
+ child: const UI(),
+ ),
+ );
}
diff --git a/lib/ui.dart b/lib/ui.dart
index 5397044..d4618cf 100644
--- a/lib/ui.dart
+++ b/lib/ui.dart
@@ -1,6 +1,5 @@
-import 'dart:async';
-
import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
import 'data.dart';
@@ -15,69 +14,30 @@ class UI extends StatelessWidget {
}
}
-class ContactList extends StatefulWidget {
+class ContactList extends StatelessWidget {
const ContactList({super.key});
@override
- State<ContactList> createState() => _ContactListState();
-}
-
-class _ContactListState extends State<ContactList> {
- late final List<Contact> contacts;
- Timer? _timer;
-
- @override
- void initState() {
- super.initState();
-
- 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),
- ];
-
- _timer = Timer.periodic(const Duration(seconds: 1), (_) {
- setState(() {
- contacts[0].isOpen = !contacts[0].isOpen;
- });
- });
- }
-
- @override
- void dispose() {
- _timer?.cancel();
- super.dispose();
- }
-
- @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Contacts")),
- body: ListView.builder(
- itemCount: contacts.length,
- itemBuilder: (BuildContext context, int index) {
- final Contact contact = contacts[index];
- return ContactTile(contact: contact);
+ body: Consumer<AppState>(
+ builder: (BuildContext context, AppState state, Widget? child) {
+ return ListView.builder(
+ itemCount: state.contacts.length,
+ itemBuilder: (BuildContext context, int index) {
+ final Contact contact = state.contacts[index];
+ return ListTile(
+ leading: Icon(
+ contact.isOpen ? Icons.meeting_room : Icons.door_front_door,
+ color: contact.isOpen ? Colors.red : Colors.green,
+ ),
+ title: Text(contact.address),
+ );
+ },
+ );
},
),
);
}
}
-
-class ContactTile extends StatelessWidget {
- final Contact contact;
-
- const ContactTile({super.key, required this.contact});
-
- @override
- Widget build(BuildContext context) {
- return ListTile(
- leading: Icon(
- contact.isOpen ? Icons.meeting_room : Icons.door_front_door,
- color: contact.isOpen ? Colors.red : Colors.green,
- ),
- title: Text(contact.address),
- );
- }
-}
diff --git a/pubspec.lock b/pubspec.lock
index 6a6a462..250f0cd 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -147,6 +147,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "10.11.3"
+ nested:
+ dependency: transitive
+ description:
+ name: nested
+ sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
+ url: "https://pub.dev"
+ source: hosted
+ version: "1.0.0"
path:
dependency: transitive
description:
@@ -155,6 +163,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.1"
+ provider:
+ dependency: "direct main"
+ description:
+ name: provider
+ sha256: "4e82183fa20e5ca25703ead7e05de9e4cceed1fbd1eadc1ac3cb6f565a09f272"
+ url: "https://pub.dev"
+ source: hosted
+ version: "6.1.5+1"
sky_engine:
dependency: transitive
description: flutter
diff --git a/pubspec.yaml b/pubspec.yaml
index 395aeac..1114aab 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -10,6 +10,7 @@ dependencies:
flutter:
sdk: flutter
mqtt_client: ^10.11.3
+ provider: ^6.1.5
dev_dependencies:
flutter_test: