diff options
| author | xengineering <me@xengineering.eu> | 2026-01-05 14:08:11 +0100 |
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2026-01-05 14:08:11 +0100 |
| commit | 14d7ef0101400f531092ee4abfc42e9a0dfd218e (patch) | |
| tree | c0e86ab105c6c54cee7de946bebf3082181b7ac0 /lib/ui.dart | |
| parent | e6b7714df1dec9db53066c6303452ce0c45a5099 (diff) | |
| download | sia-app-14d7ef0101400f531092ee4abfc42e9a0dfd218e.tar sia-app-14d7ef0101400f531092ee4abfc42e9a0dfd218e.tar.zst sia-app-14d7ef0101400f531092ee4abfc42e9a0dfd218e.zip | |
Split code into three Dart files
This follows recommendations [1] from the flutter documentation.
[1]: https://docs.flutter.dev/app-architecture/concepts
Diffstat (limited to 'lib/ui.dart')
| -rw-r--r-- | lib/ui.dart | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/ui.dart b/lib/ui.dart new file mode 100644 index 0000000..5397044 --- /dev/null +++ b/lib/ui.dart @@ -0,0 +1,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), + ); + } +} |
