PIbd31_Razubaev.S.M._PMU/lib/presentation/home_page/home_page.dart

121 lines
3.3 KiB
Dart
Raw Normal View History

2024-10-04 14:11:06 +04:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2024-10-16 12:08:15 +04:00
import 'package:flutter_app/data/repositories/manga_repository.dart';
import 'package:flutter_app/data/repositories/mock_repository.dart';
import 'package:flutter_app/presentation/details_page/details_page.dart';
2024-10-04 14:11:06 +04:00
import '../../domain/models/carddata.dart';
part 'card.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme
.of(context)
.colorScheme
.inversePrimary,
title: Text(widget.title),
),
body: const Body(),
);
}
}
2024-10-16 12:08:15 +04:00
class Body extends StatefulWidget {
2024-10-04 14:11:06 +04:00
const Body({super.key});
2024-10-16 12:08:15 +04:00
@override
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
final MangaRepository repo = MangaRepository();
var data = MangaRepository().loadData();
2024-10-04 14:11:06 +04:00
void _showSnackbar(BuildContext context, String title, bool isLiked) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
2024-10-16 12:08:15 +04:00
'Лайк на $title ${isLiked ? 'поставлен' : 'убран'}',
style: Theme
.of(context)
.textTheme
.bodyLarge,
2024-10-04 14:11:06 +04:00
),
backgroundColor: Colors.orangeAccent,
duration: const Duration(seconds: 1),
));
});
}
2024-10-16 12:08:15 +04:00
void _navToDetails(BuildContext context, CardData data) {
Navigator.push(
context, CupertinoPageRoute(builder: (context) => DetailsPage(data)));
2024-10-04 14:11:06 +04:00
}
2024-10-16 12:08:15 +04:00
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(12),
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(
mainAxisAlignment: MainAxisAlignment.center,
children: snapshot.data!.map((data) {
return _Card.fromData(data,
onLike: (String title, bool isLiked) =>
_showSnackbar(context, title, isLiked),
onTap: () => _navToDetails(context, data),);
}).toList() ?? [],
)
: const CircularProgressIndicator(),
),
),
),
)
],
));
}}