简体   繁体   English

Flutter - 如何为日历中的日期制作自定义下划线?

[英]Flutter - how do i make a custom underline for a date in my calendar?

I want to make this design with an underline for a holiday in my calendar widget.我想在我的日历小部件中为假期添加下划线。 在此处输入图像描述

This is what I have so far.这就是我到目前为止所拥有的。 I don't know how to add spacing and round off the sides of the underline.我不知道如何添加间距和四舍五入下划线的两侧。 在此处输入图像描述

I am using SfDateRangePicker, here is the code so far:我正在使用 SfDateRangePicker,这是到目前为止的代码:

              monthCellStyle: DateRangePickerMonthCellStyle(
              specialDatesTextStyle: TextStyle(
                  fontStyle: FontStyle.normal,
                  fontSize: 13,
                  fontWeight: FontWeight.w500,
                  color: Colors.black,
                  decoration: TextDecoration.underline,
                  decorationThickness: 4,
                  decorationColor: Colors.red[800]),

不幸的是syncfusion不是开源的,所以不能真正测试这个,但他们似乎有一个例子here

I don't know if it will be enough for what you want to accomplish, but you can get around this problem with this style.我不知道这是否足以满足您想要完成的任务,但是您可以使用这种样式解决此问题。 If you decrease the value "-2" the spacing will increase.如果减小值“-2”,间距将增加。
OBS: [credits]: Is it possible to underline text with some height between text and underline line? OBS:[学分]: 是否可以在文本和下划线之间有一定高度的文本下划线?

 Text(
      "Your Text here",
      style: TextStyle(
        shadows: [
         Shadow(color: Colors.black, 
                offset: Offset(0, -2))
                ],
        color: Colors.transparent,
        decoration: TextDecoration.underline,
        decorationColor: Colors.blue,
        decorationThickness: 4,
        decorationStyle: TextDecorationStyle.solid,
                ),
              ),

I ended up following Mathiew's answer to create the design i needed.我最终按照 Mathiew 的回答来创建我需要的设计。 This is how i got it to look:这就是我的样子: 在此处输入图像描述

For anyone interested here is the code i ended up using:对于任何对此感兴趣的人,我最终使用的代码是:

 class _MonthCellDecoration extends Decoration {
  const _MonthCellDecoration(this.showIndicator,
      {this.borderColor, this.backgroundColor, this.indicatorColor});

  final Color borderColor;
  final Color backgroundColor;
  final bool showIndicator;
  final Color indicatorColor;

  @override
  BoxPainter createBoxPainter([VoidCallback onChanged]) {
    return _MonthCellDecorationPainter(showIndicator,
        borderColor: borderColor,
        backgroundColor: backgroundColor,
        indicatorColor: indicatorColor);
  }
}

class _MonthCellDecorationPainter extends BoxPainter {
  _MonthCellDecorationPainter(this.showIndicator,
      {this.borderColor, this.backgroundColor, this.indicatorColor});

  final Color borderColor;
  final Color backgroundColor;
  final bool showIndicator;
  final Color indicatorColor;

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final Rect bounds = offset & configuration.size;
    _drawDecoration(canvas, bounds);
  }

  void _drawDecoration(Canvas canvas, Rect bounds) {
    final Paint paint = Paint()..color = backgroundColor;
    canvas.drawRRect(
        RRect.fromRectAndRadius(bounds, const Radius.circular(5)), paint);
    paint.style = PaintingStyle.stroke;
    paint.strokeWidth = 3;
    paint.strokeCap = StrokeCap.round;
    if (borderColor != null) {
      paint.color = borderColor;
      canvas.drawRRect(
          RRect.fromRectAndRadius(bounds, const Radius.circular(5)), paint);
    }

    if (showIndicator) {
      paint.color = indicatorColor;
      paint.style = PaintingStyle.fill;
      canvas.drawLine(Offset(bounds.left + 18, bounds.bottom - 7),
          Offset(bounds.right - 18, bounds.bottom - 7), paint);
    }
  }
}

And to use it just add this code to the monthCellStyle property要使用它,只需将此代码添加到 monthCellStyle 属性

specialDatesDecoration: _MonthCellDecoration(true,
   backgroundColor: Colors.transparent,
   indicatorColor: Colors.red),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM