video_detail_provider.dart 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'package:news_app/constant/api_const.dart';
  3. import 'package:news_app/model/video_new_model.dart';
  4. import '../http/http_util.dart';
  5. import '../http/model_parser.dart';
  6. /// @author: bo.zeng
  7. /// @email: cnhbwds@gmail.com
  8. /// @date: 2025 2025/4/16 20:20
  9. /// @description:
  10. class VideoDetailProvider extends Notifier<VideoNewModel> {
  11. @override
  12. VideoNewModel build() {
  13. return VideoNewModel();
  14. }
  15. Future<void> fetchVideoDetail(String videoId) async {
  16. final jsonData = await HttpUtil().get(
  17. apiVideoDetail,
  18. queryParameters: {"vid": videoId},
  19. );
  20. final data = ModelParser.parseObject<VideoNewModel>(
  21. jsonData,
  22. VideoNewModel.fromJson,
  23. );
  24. state = data;
  25. }
  26. void likeVideo(bool current) {
  27. if (current) {
  28. state = state.copyWith(likeCount: state.likeCount! - 1, isLiked: false);
  29. } else {
  30. state = state.copyWith(likeCount: state.likeCount! + 1, isLiked: true);
  31. }
  32. }
  33. void favoriteVideo(bool current) {
  34. if (current) {
  35. state = state.copyWith(
  36. favoriteCount: state.favoriteCount! - 1,
  37. isFavorite: false,
  38. );
  39. } else {
  40. state = state.copyWith(
  41. favoriteCount: state.favoriteCount! + 1,
  42. isFavorite: true,
  43. );
  44. }
  45. }
  46. }