summaryrefslogtreecommitdiff
path: root/lib/ui.dart
blob: 5397044aca71a4db0d8461cb156be2fbf659f496 (plain)
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
import 'dart:async';

import 'package:flutter/material.dart';

import 'data.dart';

class UI extends StatelessWidget {
  const UI({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ContactList(),
    );
  }
}

class ContactList extends StatefulWidget {
  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);
        },
      ),
    );
  }
}

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