zoukankan      html  css  js  c++  java
  • Flutter_02_封装一个简单的收藏按钮

    import 'package:flutter/material.dart';
    import 'package:flutter_go/utils/useDebounce.dart';
    
    /// 自定义收藏组件
    /// isFavorite  默认是否收藏
    /// favoriteNum 默认收藏次数
    class FavoriteWidget extends StatefulWidget {
      final bool isFavorite;
      final int favoriteNum;
    
      FavoriteWidget({
        Key? key,
        required this.isFavorite,
        required this.favoriteNum,
      }) : super(key: key);
    
      @override
      _FavoriteState createState() => _FavoriteState();
    }
    
    class _FavoriteState extends State<FavoriteWidget> {
      late bool _isFavorite;
      late int _favoriteNum;
    
      @override
      void initState() {
        super.initState();
        _isFavorite = widget.isFavorite;
        _favoriteNum = widget.favoriteNum;
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: InkWell(
            onTap: useDebounce(_favorite, delay: Duration(milliseconds: 300)),
            splashColor: Colors.transparent,
            highlightColor: Colors.transparent,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Icon(
                  _isFavorite ? Icons.favorite : Icons.favorite_border,
                  color: _isFavorite ? Colors.deepOrange : null,
                  size: 18,
                ),
                Text(
                  "$_favoriteNum",
                  style: TextStyle(fontSize: 16),
                ),
              ],
            ),
          ),
        );
      }
    
      /// 收藏&取消收藏
      void _favorite() {
        setState(() {
          _isFavorite ? _favoriteNum -= 1 : _favoriteNum += 1;
          _isFavorite = !_isFavorite;
        });
      }
    }
    
    
  • 相关阅读:
    Java序列化原理
    分库分表
    数据库索引
    监听TCP端口号:从简单Socket到NIO到Netty
    如何保证缓存与数据库的双写一致性
    代理模式:静态代理、JDK动态代理、Cglib动态代理
    Redis发布订阅(Pub-Sub)模式
    Redis分片机制(Sharding)
    Redis高可用性:主从、哨兵和集群
    Redis简介
  • 原文地址:https://www.cnblogs.com/carp-li/p/15106413.html
Copyright © 2011-2022 走看看