131 lines
3.9 KiB
Dart
131 lines
3.9 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter/cupertino.dart';
|
||
import 'package:mobiles_labs_5th_semester/data/repositories/mock_repository.dart';
|
||
import 'package:mobiles_labs_5th_semester/domain/models/game.dart';
|
||
import 'package:mobiles_labs_5th_semester/presentation/details_page/details_page.dart';
|
||
|
||
import '../../data/repositories/games_repository.dart';
|
||
|
||
part 'gameCard.dart';
|
||
|
||
class MyHomePage extends StatefulWidget {
|
||
const MyHomePage({super.key, required this.title});
|
||
|
||
final String title;
|
||
|
||
@override
|
||
State<MyHomePage> createState() => _MyHomePageState();
|
||
}
|
||
|
||
class _MyHomePageState extends State<MyHomePage> {
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
backgroundColor: Color.fromARGB(255, 46, 65, 80),
|
||
appBar: AppBar(
|
||
backgroundColor: Color.fromARGB(255, 56, 90, 128),
|
||
title: Text(
|
||
widget.title,
|
||
style: const TextStyle(
|
||
color: Colors.white, fontWeight: FontWeight.bold),
|
||
),
|
||
),
|
||
body: const Body());
|
||
}
|
||
}
|
||
|
||
//!!!скорее всего надо будет заменить на stateful
|
||
class Body extends StatefulWidget {
|
||
const Body({super.key});
|
||
|
||
@override
|
||
State<Body> createState() => _BodyState();
|
||
}
|
||
|
||
class _BodyState extends State<Body> {
|
||
late Future<List<GameData>?> data;
|
||
final searchController = TextEditingController();
|
||
final repo = GamesRepository();
|
||
|
||
@override
|
||
void initState() {
|
||
data = repo.loadData(null);
|
||
super.initState();
|
||
}
|
||
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Column(
|
||
children: [
|
||
Padding(
|
||
padding: const EdgeInsets.only(top: 12, left: 12, right: 12),
|
||
child: CupertinoSearchTextField(
|
||
backgroundColor: Colors.white,
|
||
style: TextStyle(color: Color.fromARGB(255, 46, 65, 80)),
|
||
// borderRadius: const BorderRadiusTween(2.0),
|
||
controller: searchController,
|
||
onChanged: (search) {
|
||
setState(() {
|
||
data = repo.loadData(search);
|
||
});
|
||
},
|
||
),
|
||
),
|
||
Expanded(
|
||
child: Center(
|
||
child: FutureBuilder<List<GameData>?>(
|
||
future: data,
|
||
builder: (context, snapshot) => SingleChildScrollView(
|
||
child: snapshot.hasData
|
||
? Column(
|
||
children: snapshot.data?.map((data) {
|
||
return _GameCard.fromData(
|
||
data,
|
||
onLike: (String title, bool isLiked) =>
|
||
_showSnackBar(context, title, isLiked),
|
||
onTap: () => _navToDetails(context, data),
|
||
);
|
||
}).toList() ??
|
||
[])
|
||
: const CircularProgressIndicator()
|
||
),
|
||
),
|
||
),
|
||
),
|
||
],
|
||
);
|
||
}
|
||
|
||
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||
content: Text(
|
||
isLiked
|
||
? 'Вы поставили лайк игре "$title"'
|
||
: 'Вы убрали лайк у игры "$title"',
|
||
style: Theme.of(context)
|
||
.textTheme
|
||
.bodyLarge
|
||
?.copyWith(color: Colors.pink, fontWeight: FontWeight.bold),
|
||
textAlign: TextAlign.center,
|
||
),
|
||
backgroundColor: Colors.white,
|
||
duration: const Duration(seconds: 1),
|
||
));
|
||
});
|
||
}
|
||
|
||
void _navToDetails(BuildContext context, GameData data) {
|
||
Navigator.push(
|
||
context,
|
||
CupertinoPageRoute(builder: (context) => DetailsPage(data.id ?? 0)),
|
||
);
|
||
}
|
||
}
|