You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter中为Widget实现丝带环绕样式?是否有可用包?

在Flutter中实现Widget环绕丝带样式的方案

一、自定义实现(无需第三方包)

如果想要完全自定义丝带的样式和位置,可以通过Stack、Positioned和Transform组合实现,以右上角斜跨丝带为例:

import 'dart:math';
import 'package:flutter/material.dart';

class RibbonWidget extends StatelessWidget {
  const RibbonWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Stack(
      clipBehavior: Clip.none, // 允许丝带超出父Widget范围显示
      children: [
        // 你的目标Widget(示例用Card)
        Card(
          elevation: 4,
          margin: const EdgeInsets.all(20),
          child: const SizedBox(
            width: 220,
            height: 160,
            child: Center(child: Text('带丝带的内容')),
          ),
        ),
        // 丝带组件
        Positioned(
          top: -8,
          right: -8,
          child: Transform.rotate(
            angle: pi / 4, // 旋转45度实现斜跨效果
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 28, vertical: 3),
              color: Colors.redAccent,
              child: const Text(
                'NEW',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

你可以通过调整Positioned的top/right参数、Transform的旋转角度、容器的padding和颜色,来适配不同的丝带位置和样式需求。如果需要更复杂的形状,还可以用CustomPaint绘制自定义路径的丝带。

二、第三方包推荐

如果不想自己写代码,以下几个第三方包可以快速实现丝带效果:

1. ribbon_widget

这是一个功能完善的丝带包,支持多种丝带位置(顶部/底部、左侧/右侧)、自定义颜色、文本样式和角度。

使用示例:

import 'package:flutter/material.dart';
import 'package:ribbon_widget/ribbon_widget.dart';

class RibbonExample extends StatelessWidget {
  const RibbonExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Ribbon(
      text: 'POPULAR',
      textStyle: const TextStyle(color: Colors.white),
      color: Colors.blue,
      position: RibbonPosition.topEnd, // 丝带位置:右上角
      elevation: 2,
      child: Card(
        child: const SizedBox(width: 220, height: 160, child: Center(child: Text('示例内容'))),
      ),
    );
  }
}

安装方式:在pubspec.yaml中添加依赖:

dependencies:
  ribbon_widget: ^1.1.0 # 替换为最新版本

2. flutter_ribbon

轻量型丝带包,支持自定义丝带的形状、偏移量和样式,适合快速集成基础丝带效果。

使用示例:

import 'package:flutter/material.dart';
import 'package:flutter_ribbon/flutter_ribbon.dart';

class FlutterRibbonExample extends StatelessWidget {
  const FlutterRibbonExample({super.key});

  @override
  Widget build(BuildContext context) {
    return RibbonLabel(
      label: 'HOT',
      color: Colors.orange,
      child: Card(
        child: const SizedBox(width: 220, height: 160, child: Center(child: Text('示例内容'))),
      ),
    );
  }
}

安装方式:在pubspec.yaml中添加依赖:

dependencies:
  flutter_ribbon: ^0.1.0 # 替换为最新版本

内容的提问来源于stack exchange,提问作者BHARATH T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 11:55:26