PIbd-32_Shabunov_O.A._Mobil.../lib/presentation/home_page/home_page.dart

115 lines
3.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_android_app/domain/models/card.dart';
import 'package:flutter_android_app/presentation/details_page/details_page.dart';
import 'package:flutter_android_app/repositories/potter_repository.dart';
part 'card.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() {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Hello', style: Theme.of(context).textTheme.bodyLarge),
backgroundColor: Colors.deepPurple.shade200,
duration: const Duration(seconds: 4),
));
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple.shade200,
title: Text(widget.title),
),
body: const Body(),
);
}
}
class Body extends StatefulWidget {
const Body({super.key});
@override
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
final TextEditingController searchController = TextEditingController();
final repository = PotterRepository();
Future<List<CardData>?> data = PotterRepository().loadData();
@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(
controller: searchController,
onChanged: (search) {
setState(() {
data = repository.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(),
),
)
)
),
],
)
);
}
void _showSnackBar(BuildContext context, String title, bool isLiked) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'Card $title ${isLiked ? 'liked' : 'disliked'}',
style: Theme.of(context).textTheme.bodyLarge
),
backgroundColor: Colors.deepPurple.shade200,
duration: const Duration(seconds: 2),
));
});
}
void _navToDetails(BuildContext context, CardData data) {
Navigator.push(context, CupertinoPageRoute(builder: (context) => DetailsPage(data)));
}
}