1
1
import 'package:flutter/material.dart' ;
2
+ import 'package:flutter_bloc/flutter_bloc.dart' ;
2
3
import 'package:tmdb_flutter/app/api/models/movie_responses.dart' ;
4
+ import 'package:tmdb_flutter/app/cubit/favorite_movies_cubit.dart' ;
3
5
4
- class DetailsPage extends StatelessWidget {
6
+ class DetailsPage extends StatefulWidget {
5
7
const DetailsPage ({required this .movie, super .key});
6
8
7
9
final Movie movie;
8
10
11
+ @override
12
+ State <DetailsPage > createState () => _DetailsPageState ();
13
+ }
14
+
15
+ class _DetailsPageState extends State <DetailsPage > {
16
+ bool _isFavorite = false ;
17
+
18
+ @override
19
+ void initState () {
20
+ super .initState ();
21
+ _checkFavoriteStatus ();
22
+ }
23
+
24
+ Future <void > _checkFavoriteStatus () async {
25
+ final isFavorite = await context.read <FavoriteMoviesCubit >().isMovieFavorite (widget.movie.id);
26
+ setState (() {
27
+ _isFavorite = isFavorite;
28
+ });
29
+ }
30
+
9
31
@override
10
32
Widget build (BuildContext context) {
11
33
return Scaffold (
@@ -27,7 +49,7 @@ class DetailsPage extends StatelessWidget {
27
49
),
28
50
image: DecorationImage (
29
51
image: NetworkImage (
30
- 'https://image.tmdb.org/t/p/w500${movie .backdropPath ?? movie .posterPath }' ,
52
+ 'https://image.tmdb.org/t/p/w500${widget . movie .backdropPath ?? widget . movie .posterPath }' ,
31
53
),
32
54
fit: BoxFit .cover,
33
55
),
@@ -45,14 +67,17 @@ class DetailsPage extends StatelessWidget {
45
67
top: 16 ,
46
68
right: 16 ,
47
69
child: _CircleButton (
48
- icon: Icons .favorite_border,
49
- onTap: () {},
70
+ icon: _isFavorite ? Icons .favorite : Icons .favorite_border,
71
+ onTap: () async {
72
+ await context.read <FavoriteMoviesCubit >().toggleFavorite (widget.movie);
73
+ _checkFavoriteStatus ();
74
+ },
50
75
),
51
76
),
52
77
Positioned (
53
78
left: 24 ,
54
79
bottom: 0 ,
55
- child: _RatingIndicator (percent: movie.voteAverage / 10 ),
80
+ child: _RatingIndicator (percent: widget. movie.voteAverage / 10 ),
56
81
),
57
82
const Positioned (
58
83
left: 100 ,
0 commit comments