pmu_flutter/lec4/lib/view/home_page/home_page.dart

96 lines
2.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:test_app/domain/card.dart';
import 'package:test_app/view/details_page/details_page.dart';
import 'package:test_app/view/home_page/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> {
final Color _color = Colors.orangeAccent;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _color,
title: Text(widget.title),
),
body: const Body(),
);
}
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
final data = [
CardData(
'Hello',
descriptionText: 'Hello text',
imageUrl:
'https://unsplash.com/photos/6GMq7AGxNbE/download?ixid=M3wxMjA3fDB8MXxzZWFyY2h8Mnx8cmFjY29vbnxlbnwwfHx8fDE3Mjc5MTExNTl8MA&force=true&w=640',
),
CardData(
'Hello 2',
descriptionText: 'Hello 2 text',
icon: Icons.add,
imageUrl:
'https://unsplash.com/photos/UopR2NUBYek/download?ixid=M3wxMjA3fDB8MXxzZWFyY2h8MTF8fHJhY2Nvb258ZW58MHx8fHwxNzI3OTExMTU5fDA&force=true&w=640',
),
CardData(
'Hello 3',
descriptionText: 'Hello 3 text',
icon: Icons.add,
imageUrl:
'https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Raccoon_on_Log.jpg/640px-Raccoon_on_Log.jpg',
),
];
return Center(
child: SingleChildScrollView(
padding: const EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 8),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data
.map((data) => HomeCard.fromData(
data,
onLike: (String title, bool isLiked) =>
_showSnackBar(context, title, isLiked),
onTap: () => _navigateToDetails(context, data),
))
.toList(),
),
),
);
}
void _showSnackBar(BuildContext context, String title, bool isLiked) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'$title is ${isLiked ? 'liked' : 'disliked'}!',
style: Theme.of(context).textTheme.bodyLarge,
),
backgroundColor: Colors.orangeAccent,
duration: const Duration(seconds: 1),
));
});
}
void _navigateToDetails(BuildContext context, CardData data) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DetailsPage(data)),
);
}
}