PMU/lib/main.dart
2024-10-08 23:25:53 +04:00

106 lines
2.6 KiB
Dart

import 'package:flutter/material.dart';
import 'card_repository.dart';
import 'card_item.dart';
import 'card_model.dart'; // Убедитесь, что этот импорт присутствует
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Card App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CardListScreen(),
);
}
}
class CardListScreen extends StatefulWidget {
@override
_CardListScreenState createState() => _CardListScreenState();
}
class _CardListScreenState extends State<CardListScreen> {
List<CardModel> cards = [];
List<CardModel> filteredCards = [];
bool isLoading = true;
String searchQuery = '';
@override
void initState() {
super.initState();
fetchCards();
}
Future<void> fetchCards() async {
final repository = CardRepository();
try {
final fetchedCards = await repository.fetchCards();
setState(() {
cards = fetchedCards;
filteredCards = cards;
isLoading = false;
});
} catch (e) {
setState(() {
isLoading = false;
});
}
}
void filterCards(String query) {
setState(() {
searchQuery = query;
filteredCards = cards.where((card) {
return card.value.toLowerCase().contains(query.toLowerCase()) ||
card.suit.toLowerCase().contains(query.toLowerCase());
}).toList();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Колода карт'),
bottom: PreferredSize(
preferredSize: Size.fromHeight(50.0),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(
hintText: 'Поиск по значению или масти',
border: OutlineInputBorder(),
),
onChanged: filterCards,
),
),
),
),
body: isLoading
? Center(child: CircularProgressIndicator())
: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1.9,
),
itemCount: filteredCards.length,
itemBuilder: (context, index) {
final card = filteredCards[index];
return CardItem(
suit: card.suit,
value: card.value,
imageUrl: card.imageUrl,
);
},
),
);
}
}