2024-10-04 20:36:43 +04:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-10-04 23:46:07 +04:00
|
|
|
import 'package:flutter_project/data/repositories/anime_repository.dart';
|
2024-10-04 20:36:43 +04:00
|
|
|
import 'package:flutter_project/domain/models/card.dart';
|
|
|
|
import 'package:flutter_project/views/details_page/details_page.dart';
|
|
|
|
|
|
|
|
part 'card.dart';
|
|
|
|
|
|
|
|
class HomePage extends StatefulWidget {
|
|
|
|
const HomePage({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<HomePage> createState() => _HomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
|
|
title: Text(
|
|
|
|
'Item list',
|
|
|
|
),
|
|
|
|
),
|
2024-10-04 23:46:07 +04:00
|
|
|
body: Body());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 00:59:39 +04:00
|
|
|
class Body extends StatefulWidget {
|
2024-10-04 23:46:07 +04:00
|
|
|
const Body({super.key});
|
|
|
|
|
|
|
|
@override
|
2024-10-05 00:59:39 +04:00
|
|
|
State<Body> createState() => _BodyState();
|
|
|
|
}
|
2024-10-04 23:46:07 +04:00
|
|
|
|
2024-10-05 00:59:39 +04:00
|
|
|
class _BodyState extends State<Body> {
|
|
|
|
final AnimeRepository repo = AnimeRepository();
|
|
|
|
var data = AnimeRepository().loadData();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
|
|
|
|
child: CupertinoSearchTextField(
|
|
|
|
onChanged: (search) {
|
|
|
|
setState(() {
|
|
|
|
data = repo.loadData(q: search);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Center(
|
|
|
|
child: FutureBuilder<List<CardData>?>(
|
|
|
|
future: data,
|
|
|
|
builder: (context, snapshot) => SingleChildScrollView(
|
|
|
|
child: snapshot.hasData
|
|
|
|
? Column(
|
|
|
|
children: snapshot.data
|
|
|
|
?.map((data) => _Card.withData(
|
|
|
|
data,
|
|
|
|
onLike: (String title, bool isLiked) =>
|
|
|
|
_showSnackBar(
|
|
|
|
context, isLiked, title),
|
|
|
|
onTap: () => _navToDetails(context, data),
|
|
|
|
))
|
|
|
|
.toList() ??
|
|
|
|
[])
|
|
|
|
: const CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-10-04 23:46:07 +04:00
|
|
|
),
|
2024-10-05 00:59:39 +04:00
|
|
|
],
|
2024-10-04 23:46:07 +04:00
|
|
|
);
|
2024-10-04 20:36:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void _showSnackBar(BuildContext context, bool isLiked, String title) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(
|
|
|
|
isLiked ? "You liked $title" : "Like removed from $title",
|
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
|
|
),
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
|
|
duration: const Duration(milliseconds: 1200),
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _navToDetails(BuildContext context, CardData d) {
|
|
|
|
Navigator.push(
|
|
|
|
context, CupertinoPageRoute(builder: (context) => DetailsPage(d)));
|
|
|
|
}
|
|
|
|
}
|