тяжко было

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 {
final String name;
final AnimalName name;
final String 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 '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<Animal> animals = [
Animal(name: 'Лев', description: 'Король зверей'),
Animal(name: 'Слон', description: 'Крупнейшее наземное животное'),
Animal(name: 'Жираф', description: 'Животное с самой длинной шеей'),
Animal(name: 'Тигр', description: 'Большая кошка с полосатым мехом'),
Animal(name: 'Зебра', description: 'Полосатая лошадь'),
_AnimalListScreenState createState() => _AnimalListScreenState();
}
class _AnimalListScreenState extends State<AnimalListScreen> {
late Future<List<Animal>> _animalsFuture;
@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(
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<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,
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),
),
);
},
);
}
},
),
);