flutter:角标

角标应该非常常见了,以小说app为例,通常会在小说封面的右上角上显示当前未读的章数。

badges

简介

Flutter的badges库是一个用于创建徽章组件的开源库。它提供了简单易用的API,使开发者可以轻松地在Flutter应用程序中添加徽章效果。

官方文档
https://pub-web.flutter-io.cn/packages/badges

安装

dart 复制代码
flutter pub add badges

示例1

dart 复制代码
Center(
        child: badges.Badge(
      badgeContent: const Text('3',style: TextStyle(fontSize: 20),),
      child: SizedBox(
        width: 200,
        height: 260,
        child: Image.asset('lib/assets/img/error.jpg',fit: BoxFit.fill,),
      ),
    ));


示例2

dart 复制代码
Center(
        child: badges.Badge(
      position: badges.BadgePosition.topStart(), // 角标的位置
      badgeContent: const Text(
        '3',
        style: TextStyle(fontSize: 20,color: Colors.white),
      ), // 内容
      onTap: () {
        print("点击事件");
      },
      //    样式
      badgeStyle: badges.BadgeStyle(
          shape: badges.BadgeShape.square, // 形状
          badgeColor: Colors.blue,
          padding: const EdgeInsets.symmetric(horizontal: 15,vertical: 5),
          borderRadius: BorderRadius.circular(10)),
      child: SizedBox(
        width: 200,
        height: 260,
        child: Image.asset(
          'lib/assets/img/error.jpg',
          fit: BoxFit.fill,
        ),
      ),
    ));

示例3

dart 复制代码
 shape: badges.BadgeShape.twitter ,


示例4

dart 复制代码
shape: badges.BadgeShape.instagram , // 形状

rotated_corner_decoration

简介

Flutter中的rotated_corner_decoration是一个用于创建旋转圆角装饰的类。它是Flutter框架中的一个内置装饰器,可以应用于各种小部件,如容器、按钮、卡片等。

官方文档
https://pub-web.flutter-io.cn/packages/rotated_corner_decoration

安装

dart 复制代码
flutter pub add rotated_corner_decoration

示例1

dart 复制代码
Container(
          width: 200,
          height: 200,
          foregroundDecoration: const RotatedCornerDecoration.withColor(
              color: Colors.red,
              badgeSize: Size(30,30)
          ),
          decoration: BoxDecoration(
            border: Border.all(color: Colors.black,width: 1)
          ),
        )


示例2

dart 复制代码
Container(
      width: 200,
      height: 200,
      foregroundDecoration: const RotatedCornerDecoration.withColor(
          color: Colors.red,
          badgeSize: Size(60, 60), // 大小
          badgePosition: BadgePosition.topStart, //位置
          spanBaselineShift: 4, // 文字距离斜边的距离
          textSpan:
              TextSpan(text: 'flutter', style: TextStyle(color: Colors.white))),
      decoration:
          BoxDecoration(border: Border.all(color: Colors.black, width: 1)),
    )


示例3

dart 复制代码
Container(
      width: 200,
      height: 200,
      foregroundDecoration: const RotatedCornerDecoration.withColor(
          color: Colors.red,
          badgeSize: Size(80, 80), // 大小
          badgePosition: BadgePosition.topStart, //位置
          spanBaselineShift: 4, // 文字距离斜边的距离
          textSpan:
              TextSpan(text: 'hello\nflutter', style: TextStyle(color: Colors.white))),
      decoration:
          BoxDecoration(border: Border.all(color: Colors.black, width: 1)),
    )
相关推荐
左手厨刀右手茼蒿2 小时前
Flutter 三方库 k_m_logic 的鸿蒙化适配指南 - 构建声明式的业务逻辑状态机、助力鸿蒙端复杂交互流程的解耦与重构
flutter·harmonyos·鸿蒙·openharmony·k_m_logic
钛态2 小时前
Flutter 三方库 result_type 深入鸿蒙强类型返回栈跨界交互适配:肃清空指针回调与运行时崩溃、大幅增注接口安全壁垒且提升多隔离桥接数据抛出健壮性-适配鸿蒙 HarmonyOS ohos
flutter·交互·harmonyos
左手厨刀右手茼蒿2 小时前
Flutter 三方库 invertible 的鸿蒙化适配指南 - 实现极致的撤销与重做流、助力鸿蒙端高交互编辑类应用开发
flutter·harmonyos·鸿蒙·openharmony
恋猫de小郭2 小时前
2026 AI 时代下,Flutter 和 Dart 的机遇和未来发展,AI 一体化
android·前端·flutter
加农炮手Jinx2 小时前
Flutter 三方库 fast_i18n 的鸿蒙化适配指南 - 掌握类型安全的国际化编译技术、助力鸿蒙应用构建全球化且极速响应的多语言交互体系
flutter·harmonyos·鸿蒙·openharmony·fast_i18n
钛态2 小时前
Flutter 三方库 test_process 的鸿蒙化适配指南 - 实现具备外部进程交互与命令行输出校验的集成测试套件、支持端侧 CLI 工具与自动化脚本协同实战
flutter·harmonyos·鸿蒙·openharmony·test_process
yeziyfx6 小时前
Flutter项目目录结构
flutter
程序员Ctrl喵8 小时前
Flutter 开发环境搭建:从零到第一次运行
flutter
帅次8 小时前
WebView 并发初始化竞争风险分析
android·xml·flutter·kotlin·webview·androidx·dalvik
Xiao正1 天前
Flutter与Rust混合开发入门指南
flutter