slidable.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_slidable/flutter_slidable.dart';
  3. import 'package:provider/provider.dart';
  4. import 'package:video/model/urlModel.dart';
  5. import 'package:video/model/video.dart';
  6. import 'package:video/services/favoriteData.dart';
  7. import 'package:video/services/type.dart';
  8. import 'package:video/widget/easyCard.dart';
  9. class SlidableWidget extends StatefulWidget {
  10. final Video video;
  11. final List<Video> favoriteList;
  12. SlidableWidget({this.video, this.favoriteList});
  13. @override
  14. _SlidableWidgetState createState() => _SlidableWidgetState();
  15. }
  16. class _SlidableWidgetState extends State<SlidableWidget> {
  17. SlidableController slidableController;
  18. Video get video => widget.video;
  19. List<Video> get favoriteList => widget.favoriteList;
  20. bool isShow = false;
  21. @override
  22. void initState() {
  23. super.initState();
  24. slidableController = SlidableController();
  25. isShow =
  26. favoriteList.where((element) => element.url == video.url).length > 0;
  27. }
  28. @override
  29. void didChangeDependencies() {
  30. super.didChangeDependencies();
  31. final _url = Provider.of<UrlInfo>(context).url;
  32. if (_url == video.url) {
  33. setState(() {
  34. isShow = false;
  35. });
  36. }
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return Slidable(
  41. key: Key(video.title),
  42. controller: slidableController,
  43. closeOnScroll: true,
  44. actionPane: SlidableDrawerActionPane(),
  45. actionExtentRatio: 0.25,
  46. child: EasyCard(
  47. prefixBadge: Colors.black38,
  48. title: video.title,
  49. time: video.time,
  50. timeColor: Colors.black,
  51. suffixIcon: Icons.chevron_right,
  52. suffixIconColor: Colors.black38,
  53. url: video.url,
  54. video: video,
  55. ),
  56. secondaryActions: <Widget>[
  57. IconSlideAction(
  58. closeOnTap: true,
  59. caption: '收藏',
  60. color: Colors.blueGrey,
  61. icon: isShow ? Icons.favorite : Icons.favorite_border,
  62. onTap: () async {
  63. if (isShow) {
  64. await FavoriteData().deleteVideo(video.url, DataType().favorite);
  65. Provider.of<UrlInfo>(context, listen: false)
  66. .changVideo(video, false);
  67. setState(() {
  68. isShow = false;
  69. });
  70. } else {
  71. await FavoriteData().insertVideo(
  72. new Video(
  73. title: video.title,
  74. time: video.time,
  75. url: video.url,
  76. type: DataType().favorite),
  77. );
  78. Provider.of<UrlInfo>(context, listen: false)
  79. .changVideo(video, true);
  80. setState(() {
  81. isShow = true;
  82. });
  83. }
  84. },
  85. )
  86. ],
  87. );
  88. }
  89. }