From 14d7ef0101400f531092ee4abfc42e9a0dfd218e Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 5 Jan 2026 14:08:11 +0100 Subject: Split code into three Dart files This follows recommendations [1] from the flutter documentation. [1]: https://docs.flutter.dev/app-architecture/concepts --- lib/data.dart | 6 ++++ lib/main.dart | 89 ++--------------------------------------------------------- lib/ui.dart | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 87 deletions(-) create mode 100644 lib/data.dart create mode 100644 lib/ui.dart diff --git a/lib/data.dart b/lib/data.dart new file mode 100644 index 0000000..f2e3531 --- /dev/null +++ b/lib/data.dart @@ -0,0 +1,6 @@ +class Contact { + final String address; + bool isOpen; + + Contact({required this.address, required this.isOpen}); +} diff --git a/lib/main.dart b/lib/main.dart index fd48c14..805ef69 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,92 +1,7 @@ -import 'dart:async'; - import 'package:flutter/material.dart'; +import 'ui.dart'; + void main() { runApp(const UI()); } - -class UI extends StatelessWidget { - const UI({super.key}); - - @override - Widget build(BuildContext context) { - return const MaterialApp( - home: ContactList(), - ); - } -} - -class Contact { - final String address; - bool isOpen; - - Contact({required this.address, required this.isOpen}); -} - -class ContactList extends StatefulWidget { - const ContactList({super.key}); - - @override - State createState() => _ContactListState(); -} - -class _ContactListState extends State { - late final List contacts; - Timer? _timer; - - @override - void initState() { - super.initState(); - - contacts = [ - 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), - ); - } -} 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 createState() => _ContactListState(); +} + +class _ContactListState extends State { + late final List contacts; + Timer? _timer; + + @override + void initState() { + super.initState(); + + contacts = [ + 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), + ); + } +} -- cgit v1.2.3-70-g09d2