тяжко было
This commit is contained in:
parent
c44fecc1b7
commit
8d53602183
8
lib/AnimalName.dart
Normal file
8
lib/AnimalName.dart
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
//Enums
|
||||||
|
enum AnimalName {
|
||||||
|
lion,
|
||||||
|
elephant,
|
||||||
|
giraffe,
|
||||||
|
tiger,
|
||||||
|
zebra,
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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,46 +42,85 @@ 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>>(
|
||||||
itemCount: animals.length,
|
future: _animalsFuture,
|
||||||
itemBuilder: (context, index) {
|
builder: (context, snapshot) {
|
||||||
return ListTile(
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
title: Text(
|
return const Center(child: CircularProgressIndicator());
|
||||||
animals[index].name,
|
} else if (snapshot.hasError) {
|
||||||
style: const TextStyle(fontSize: 24), // Увеличиваем размер текста
|
return Center(child: Text('Error: ${snapshot.error}'));
|
||||||
),
|
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
||||||
subtitle: Text(
|
return const Center(child: Text('No animals found.'));
|
||||||
animals[index].description,
|
} else {
|
||||||
style: const TextStyle(fontSize: 20), // Увеличиваем размер текста
|
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),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user