From 8d53602183e3f3fff5a29ab61a8b0b7d9fac5e8e Mon Sep 17 00:00:00 2001 From: kamilia Date: Tue, 24 Sep 2024 00:51:45 +0400 Subject: [PATCH] =?UTF-8?q?=D1=82=D1=8F=D0=B6=D0=BA=D0=BE=20=D0=B1=D1=8B?= =?UTF-8?q?=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/AnimalName.dart | 8 ++++ lib/animal.dart | 19 ++++++++- lib/main.dart | 94 ++++++++++++++++++++++++++++++++------------- 3 files changed, 92 insertions(+), 29 deletions(-) create mode 100644 lib/AnimalName.dart diff --git a/lib/AnimalName.dart b/lib/AnimalName.dart new file mode 100644 index 0000000..e95ef4b --- /dev/null +++ b/lib/AnimalName.dart @@ -0,0 +1,8 @@ +//Enums +enum AnimalName { + lion, + elephant, + giraffe, + tiger, + zebra, +} \ No newline at end of file diff --git a/lib/animal.dart b/lib/animal.dart index 2c229e2..71cbe14 100644 --- a/lib/animal.dart +++ b/lib/animal.dart @@ -1,6 +1,21 @@ +import 'AnimalName.dart'; + +//Classes class Animal { - final String name; + final AnimalName name; final String description; Animal({required this.name, required this.description}); -} \ No newline at end of file + + //Methods + String printAnimal() { + return 'Name: ${name.toStringName()}, Description: $description'; + } +} + + // Extension + extension AnimalNameExtension on AnimalName { + String toStringName() { + return toString().split('.').last; + } + } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index b455c6c..c95b9af 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'animal.dart'; +import 'AnimalName.dart'; void main() { runApp(const MyApp()); @@ -11,7 +12,7 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - title: 'Зоопарк', + title: 'Zoo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange), useMaterial3: true, @@ -28,7 +29,7 @@ class HomeScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text('Зоопарк'), + title: const Text('Zoo'), ), body: Center( child: ElevatedButton( @@ -41,46 +42,85 @@ class HomeScreen extends StatelessWidget { ); }, style: ElevatedButton.styleFrom( - padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 20), // Увеличиваем размер кнопки - textStyle: const TextStyle(fontSize: 22), // Увеличиваем размер текста + padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 20), + textStyle: const TextStyle(fontSize: 34), ), - child: const Text('Показать список животных'), + child: const Text('Show Animal List'), ), ), ); } } -class AnimalListScreen extends StatelessWidget { +class AnimalListScreen extends StatefulWidget { const AnimalListScreen({super.key}); @override - Widget build(BuildContext context) { - final List animals = [ - Animal(name: 'Лев', description: 'Король зверей'), - Animal(name: 'Слон', description: 'Крупнейшее наземное животное'), - Animal(name: 'Жираф', description: 'Животное с самой длинной шеей'), - Animal(name: 'Тигр', description: 'Большая кошка с полосатым мехом'), - Animal(name: 'Зебра', description: 'Полосатая лошадь'), + _AnimalListScreenState createState() => _AnimalListScreenState(); +} + +class _AnimalListScreenState extends State { + late Future> _animalsFuture; + + @override + void initState() { + super.initState(); + _animalsFuture = _fetchAnimals(); + } + //Generics (можно использовать List<>) + Future> _fetchAnimals() async { + // Future + await Future.delayed(const Duration(seconds: 2)); + + final animals = [ + Animal(name: AnimalName.lion, description: 'King of the jungle'), + Animal(name: AnimalName.elephant, description: 'Largest land animal'), + Animal(name: AnimalName.giraffe, description: 'Animal with the longest neck'), + Animal(name: AnimalName.tiger, description: 'Big cat with striped fur'), + Animal(name: AnimalName.zebra, description: 'Striped horse'), ]; + // Loops + for (var animal in animals) { + print(animal.printAnimal()); + } + + return animals; + } + + @override + Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text('Список животных'), + title: const Text('Animal List'), ), - body: ListView.builder( - itemCount: animals.length, - itemBuilder: (context, index) { - return ListTile( - title: Text( - animals[index].name, - style: const TextStyle(fontSize: 24), // Увеличиваем размер текста - ), - subtitle: Text( - animals[index].description, - style: const TextStyle(fontSize: 20), // Увеличиваем размер текста - ), - ); + body: FutureBuilder>( + future: _animalsFuture, + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + return const Center(child: CircularProgressIndicator()); + } else if (snapshot.hasError) { + return Center(child: Text('Error: ${snapshot.error}')); + } else if (!snapshot.hasData || snapshot.data!.isEmpty) { + return const Center(child: Text('No animals found.')); + } else { + final animals = snapshot.data!; + return ListView.builder( + itemCount: animals.length, + itemBuilder: (context, index) { + return ListTile( + title: Text( + animals[index].name.toString().split('.').last, + style: const TextStyle(fontSize: 34), + ), + subtitle: Text( + animals[index].description, + style: const TextStyle(fontSize: 26), + ), + ); + }, + ); + } }, ), );