тяжко было

This commit is contained in:
kamilia 2024-09-24 00:51:45 +04:00
parent c44fecc1b7
commit 8d53602183
3 changed files with 92 additions and 29 deletions

8
lib/AnimalName.dart Normal file
View File

@ -0,0 +1,8 @@
//Enums
enum AnimalName {
lion,
elephant,
giraffe,
tiger,
zebra,
}

View File

@ -1,6 +1,21 @@
import 'AnimalName.dart';
//Classes
class Animal { class Animal {
final String name; final AnimalName name;
final String description; final String description;
Animal({required this.name, required this.description}); Animal({required this.name, required this.description});
//Methods
String printAnimal() {
return 'Name: ${name.toStringName()}, Description: $description';
}
} }
// Extension
extension AnimalNameExtension on AnimalName {
String toStringName() {
return toString().split('.').last;
}
}

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'animal.dart'; import 'animal.dart';
import 'AnimalName.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
@ -11,7 +12,7 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Зоопарк', title: 'Zoo',
theme: ThemeData( theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange), colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange),
useMaterial3: true, useMaterial3: true,
@ -28,7 +29,7 @@ class HomeScreen extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('Зоопарк'), title: const Text('Zoo'),
), ),
body: Center( body: Center(
child: ElevatedButton( child: ElevatedButton(
@ -41,47 +42,86 @@ class HomeScreen extends StatelessWidget {
); );
}, },
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 20), // Увеличиваем размер кнопки padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 20),
textStyle: const TextStyle(fontSize: 22), // Увеличиваем размер текста 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}); const AnimalListScreen({super.key});
@override @override
Widget build(BuildContext context) { _AnimalListScreenState createState() => _AnimalListScreenState();
final List<Animal> animals = [ }
Animal(name: 'Лев', description: 'Король зверей'),
Animal(name: 'Слон', description: 'Крупнейшее наземное животное'), class _AnimalListScreenState extends State<AnimalListScreen> {
Animal(name: 'Жираф', description: 'Животное с самой длинной шеей'), late Future<List<Animal>> _animalsFuture;
Animal(name: 'Тигр', description: 'Большая кошка с полосатым мехом'),
Animal(name: 'Зебра', description: 'Полосатая лошадь'), @override
void initState() {
super.initState();
_animalsFuture = _fetchAnimals();
}
//Generics (можно использовать List<>)
Future<List<Animal>> _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( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('Список животных'), title: const Text('Animal List'),
), ),
body: ListView.builder( body: FutureBuilder<List<Animal>>(
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, itemCount: animals.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
return ListTile( return ListTile(
title: Text( title: Text(
animals[index].name, animals[index].name.toString().split('.').last,
style: const TextStyle(fontSize: 24), // Увеличиваем размер текста style: const TextStyle(fontSize: 34),
), ),
subtitle: Text( subtitle: Text(
animals[index].description, animals[index].description,
style: const TextStyle(fontSize: 20), // Увеличиваем размер текста style: const TextStyle(fontSize: 26),
), ),
); );
}, },
);
}
},
), ),
); );
} }