123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import 'package:flutter/material.dart';
- import 'package:flutter_slidable/flutter_slidable.dart';
- import 'package:provider/provider.dart';
- import 'package:video/model/urlModel.dart';
- import 'package:video/model/video.dart';
- import 'package:video/services/favoriteData.dart';
- import 'package:video/services/type.dart';
- import 'package:video/widget/easyCard.dart';
- class SlidableWidget extends StatefulWidget {
- final Video video;
- final List<Video> favoriteList;
- SlidableWidget({this.video, this.favoriteList});
- @override
- _SlidableWidgetState createState() => _SlidableWidgetState();
- }
- class _SlidableWidgetState extends State<SlidableWidget> {
- SlidableController slidableController;
- Video get video => widget.video;
- List<Video> get favoriteList => widget.favoriteList;
- bool isShow = false;
- @override
- void initState() {
- super.initState();
- slidableController = SlidableController();
- isShow =
- favoriteList.where((element) => element.url == video.url).length > 0;
- }
- @override
- void didChangeDependencies() {
- super.didChangeDependencies();
- final _url = Provider.of<UrlInfo>(context).url;
- if (_url == video.url) {
- setState(() {
- isShow = false;
- });
- }
- }
- @override
- Widget build(BuildContext context) {
- return Slidable(
- key: Key(video.title),
- controller: slidableController,
- closeOnScroll: true,
- actionPane: SlidableDrawerActionPane(),
- actionExtentRatio: 0.25,
- child: EasyCard(
- prefixBadge: Colors.black38,
- title: video.title,
- time: video.time,
- timeColor: Colors.black,
- suffixIcon: Icons.chevron_right,
- suffixIconColor: Colors.black38,
- url: video.url,
- video: video,
- ),
- secondaryActions: <Widget>[
- IconSlideAction(
- closeOnTap: true,
- caption: '收藏',
- color: Colors.blueGrey,
- icon: isShow ? Icons.favorite : Icons.favorite_border,
- onTap: () async {
- if (isShow) {
- await FavoriteData().deleteVideo(video.url, DataType().favorite);
- Provider.of<UrlInfo>(context, listen: false)
- .changVideo(video, false);
- setState(() {
- isShow = false;
- });
- } else {
- await FavoriteData().insertVideo(
- new Video(
- title: video.title,
- time: video.time,
- url: video.url,
- type: DataType().favorite),
- );
- Provider.of<UrlInfo>(context, listen: false)
- .changVideo(video, true);
- setState(() {
- isShow = true;
- });
- }
- },
- )
- ],
- );
- }
- }
|