Geen omschrijving

custom_info_card.dart 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import 'detail_page.dart';
  4. class CustomInfoCard extends StatefulWidget {
  5. final String imagePath;
  6. final String title;
  7. final String subtitle;
  8. final String texto;
  9. final bool isFavorite;
  10. const CustomInfoCard({
  11. super.key,
  12. required this.imagePath,
  13. required this.title,
  14. required this.subtitle,
  15. required this.texto,
  16. this.isFavorite = false,
  17. });
  18. @override
  19. State<CustomInfoCard> createState() => _CustomInfoCardState();
  20. }
  21. class _CustomInfoCardState extends State<CustomInfoCard> {
  22. late bool _isFavorite;
  23. @override
  24. void initState() {
  25. super.initState();
  26. _isFavorite = widget.isFavorite;
  27. if (!widget.isFavorite) _loadFavorite();
  28. }
  29. Future<void> _loadFavorite() async {
  30. final prefs = await SharedPreferences.getInstance();
  31. setState(() {
  32. _isFavorite = prefs.getBool('fav_${widget.title}') ?? false;
  33. });
  34. }
  35. Future<void> _toggleFavorite() async {
  36. final prefs = await SharedPreferences.getInstance();
  37. setState(() {
  38. _isFavorite = !_isFavorite;
  39. prefs.setBool('fav_${widget.title}', _isFavorite);
  40. });
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return GestureDetector(
  45. onTap: () {
  46. Navigator.push(
  47. context,
  48. MaterialPageRoute(
  49. builder: (context) => DetailPage(
  50. title: widget.title,
  51. texto: widget.texto,
  52. ),
  53. ),
  54. );
  55. },
  56. child: SizedBox(
  57. width: double.infinity,
  58. height: 130,
  59. child: Stack(
  60. children: [
  61. Card(
  62. margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
  63. shape: RoundedRectangleBorder(
  64. borderRadius: BorderRadius.circular(30),
  65. ),
  66. color: const Color.fromARGB(255, 254, 242, 221),
  67. child: Padding(
  68. padding: const EdgeInsets.only(right: 8),
  69. child: Row(
  70. children: [
  71. Container(
  72. width: 90,
  73. height: 90,
  74. decoration: BoxDecoration(
  75. color: Colors.white,
  76. borderRadius: BorderRadius.circular(36),
  77. ),
  78. child: ClipRRect(
  79. borderRadius: BorderRadius.circular(36),
  80. child: Image.asset(
  81. widget.imagePath,
  82. fit: BoxFit.cover,
  83. width: 90,
  84. height: 90,
  85. ),
  86. ),
  87. ),
  88. const SizedBox(width: 16),
  89. Expanded(
  90. child: Column(
  91. mainAxisAlignment: MainAxisAlignment.center,
  92. crossAxisAlignment: CrossAxisAlignment.start,
  93. children: [
  94. Text(
  95. widget.title,
  96. style: const TextStyle(
  97. fontSize: 16,
  98. fontWeight: FontWeight.bold,
  99. ),
  100. ),
  101. const SizedBox(height: 4),
  102. Text(
  103. widget.subtitle,
  104. style: TextStyle(
  105. fontSize: 14,
  106. color: Colors.grey[600],
  107. ),
  108. ),
  109. ],
  110. ),
  111. ),
  112. ],
  113. ),
  114. ),
  115. ),
  116. Positioned(
  117. top: 12,
  118. right: 28,
  119. child: IconButton(
  120. icon: Icon(
  121. _isFavorite ? Icons.favorite : Icons.favorite_border,
  122. color: _isFavorite ? Colors.red : Colors.grey[400],
  123. ),
  124. onPressed: _toggleFavorite,
  125. ),
  126. ),
  127. ],
  128. ),
  129. ),
  130. );
  131. }
  132. }