import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; 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'; 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 createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme .of(context) .colorScheme .inversePrimary, title: Text(widget.title), ), body: const Body(), ); } } class Body extends StatefulWidget { const Body({super.key}); @override State createState() => _BodyState(); } class _BodyState extends State { final MangaRepository repo = MangaRepository(); var data = MangaRepository().loadData(); void _showSnackbar(BuildContext context, String title, bool isLiked) { WidgetsBinding.instance.addPostFrameCallback((_) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text( 'Лайк на $title ${isLiked ? 'поставлен' : 'убран'}', style: Theme .of(context) .textTheme .bodyLarge, ), backgroundColor: Colors.orangeAccent, duration: const Duration(seconds: 1), )); }); } void _navToDetails(BuildContext context, CardData data) { Navigator.push( context, CupertinoPageRoute(builder: (context) => DetailsPage(data))); } @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?>( 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(), ), ), ), ) ], )); }}