Flutter之交互事件

目录:

1、点击事件标准案例

1.1、效果图

2.1、代码实现



dart 复制代码
class FavoriteWidget extends StatefulWidget {
  const FavoriteWidget({super.key});

  @override
  State<FavoriteWidget> createState() => _FavoriteWidgetState();
}
class _FavoriteWidgetState extends State<FavoriteWidget> {
 bool _isFavorited = true;
 int _favoriteCount = 41;
 
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          padding: const EdgeInsets.all(0),
          child: IconButton(
            padding: const EdgeInsets.all(0),
            alignment: Alignment.center,
            icon:
                (_isFavorited
                    ? const Icon(Icons.star)
                    : const Icon(Icons.star_border)),
            color: Colors.red[500],
            onPressed: _toggleFavorite,
          ),
        ),
        SizedBox(width: 18, child: SizedBox(child: Text('$_favoriteCount'))),
      ],
    );
  }

 void _toggleFavorite() {
  setState(() {
    if (_isFavorited) {
      _favoriteCount -= 1;
      _isFavorited = false;
    } else {
      _favoriteCount += 1;
      _isFavorited = true;
    }
  });
}
}

这个就是在主widget 树中使用自定义的widget组件去展示。

待完善。。。。。。

相关推荐
zzq199615 分钟前
Android framework 开发者模式下,如何修改动画过度模式
android
木叶丸21 分钟前
Flutter 生命周期完全指南
android·flutter·ios
AxureMost22 分钟前
Axure-9高级教程:Axure函数使用手册-免费
交互·axure
阿幸软件杂货间27 分钟前
阿幸课堂随机点名
android·开发语言·javascript
没有了遇见32 分钟前
Android 渐变色整理之功能实现<二>文字,背景,边框,进度条等
android
用户74279877375933 分钟前
[flutter翻书效果] 用flutter实现一个书籍翻页效果
flutter
没有了遇见2 小时前
Android RecycleView 条目进入和滑出屏幕的渐变阴影效果
android
站在巨人肩膀上的码农2 小时前
去掉长按遥控器power键后提示关机、飞行模式的弹窗
android·安卓·rk·关机弹窗·power键·长按·飞行模式弹窗
呼啦啦--隔壁老王2 小时前
屏幕旋转流程
android
人生何处不修行3 小时前
实战:Android 15 (API 35) 适配 & 构建踩坑全记录
android