暫無描述

detail_page.dart 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. class DetailPage extends StatefulWidget {
  4. final String title;
  5. final String texto;
  6. final String categoria;
  7. final String imagePath;
  8. const DetailPage({
  9. super.key,
  10. required this.title,
  11. required this.texto,
  12. required this.categoria,
  13. this.imagePath = 'assets/icons/default_icon.jpg',
  14. });
  15. @override
  16. State<DetailPage> createState() => _DetailPageState();
  17. }
  18. class _DetailPageState extends State<DetailPage> {
  19. bool _isFavorite = false; // Inicializado con valor por defecto
  20. @override
  21. void initState() {
  22. super.initState();
  23. _loadFavorite();
  24. }
  25. Future<void> _loadFavorite() async {
  26. final prefs = await SharedPreferences.getInstance();
  27. setState(() {
  28. _isFavorite = prefs.getBool('fav_${widget.title}') ?? false;
  29. });
  30. }
  31. Future<void> _toggleFavorite() async {
  32. final prefs = await SharedPreferences.getInstance();
  33. setState(() {
  34. _isFavorite = !_isFavorite;
  35. prefs.setBool('fav_${widget.title}', _isFavorite);
  36. });
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return Scaffold(
  41. appBar: AppBar(
  42. flexibleSpace: Padding(
  43. padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top - 20),
  44. child: Image.asset('assets/header_image.png', fit: BoxFit.cover),
  45. ),
  46. toolbarHeight: MediaQuery.of(context).size.height * 0.19,
  47. title: Text(
  48. widget.categoria,
  49. style: const TextStyle(color: Colors.white, fontSize: 16),
  50. ),
  51. actions: [
  52. IconButton(
  53. icon: Icon(
  54. _isFavorite ? Icons.favorite : Icons.favorite_border,
  55. color: _isFavorite ? Colors.red : Colors.white,
  56. ),
  57. onPressed: _toggleFavorite,
  58. ),
  59. ],
  60. ),
  61. body: Container(
  62. decoration: const BoxDecoration(color: Color.fromARGB(255, 254, 242, 221)),
  63. child: Padding(
  64. padding: const EdgeInsets.all(16.0),
  65. child: SingleChildScrollView(
  66. child: Column(
  67. crossAxisAlignment: CrossAxisAlignment.start,
  68. children: [
  69. Row(
  70. children: [
  71. Container(
  72. width: 80,
  73. height: 80,
  74. decoration: BoxDecoration(
  75. color: Colors.white,
  76. borderRadius: BorderRadius.circular(40),
  77. ),
  78. child: ClipRRect(
  79. borderRadius: BorderRadius.circular(40),
  80. child: Image.asset(
  81. widget.imagePath,
  82. fit: BoxFit.cover,
  83. ),
  84. ),
  85. ),
  86. const SizedBox(width: 16),
  87. Expanded(
  88. child: Text(
  89. widget.title,
  90. style: const TextStyle(
  91. fontSize: 20,
  92. fontWeight: FontWeight.bold,
  93. color: Colors.black87,
  94. ),
  95. ),
  96. ),
  97. ],
  98. ),
  99. const SizedBox(height: 20),
  100. Container(
  101. padding: const EdgeInsets.all(16),
  102. decoration: BoxDecoration(
  103. color: Colors.white,
  104. borderRadius: BorderRadius.circular(12),
  105. ),
  106. child: Text(
  107. widget.texto,
  108. style: const TextStyle(fontSize: 16, height: 1.5),
  109. textAlign: TextAlign.justify,
  110. ),
  111. ),
  112. ],
  113. ),
  114. ),
  115. ),
  116. ),
  117. );
  118. }
  119. }