Flutter 114: 圖解自定義 ACEProgressPainter 對比進度圖

????小菜今天繪制一個簡單的 收入-支出 進度對比圖;大致效果是在兩個梯形中進行簡單的內容展示;為了提高可復用性,小菜預先設定如下規則;

  • 左右兩側按比例展示對應尺寸,并注意大比例異常情況
  • 左右兩側內容顏色支持自定義
  • 左右兩側文字顏色內容支持自定義
  • 左右兩側支持填充和邊框兩種樣式

ACEProgressPainter

????小菜確定了設定的規則,接下來就是實操了,主要是通過 Canvas 進行繪制,再分為繪制圖形和繪制文字兩部分;

Canvas.drawPath 繪制梯形(三角形)

1. 根據比例繪制梯形

????小菜預設一個左側提醒比例,其中比例是以屏幕寬度整體計算,位于梯形中位線上,其中梯形角度預設為 45度 角,這樣根據梯形高度即可計算梯形位置;而右側梯形類似,注意與左側梯形間隔的 spaceWidth 寬度即可;

// 左側
canvas.drawPath(
    Path()..moveTo(_spaceWidth * 0.5 + _strokeWidth, _strokeWidth * 0.5)
      ..lineTo(_spaceWidth * 0.5 + _strokeWidth, _height - _strokeWidth * 0.5)
      ..lineTo(size.width * leftProgress + _height * 0.5 - _spaceWidth * sqrt(2) - _strokeWidth * sqrt(2), _height - _strokeWidth * 0.5)
      ..lineTo(size.width * leftProgress + _height * 0.5 - _spaceWidth * sqrt(2) - _strokeWidth * sqrt(2) - _height + _strokeWidth, _strokeWidth * 0.5)
      ..lineTo(_spaceWidth * 0.5 + _strokeWidth, _strokeWidth * 0.5)
      ..close(),
    _paint..color = leftColor ?? _kProLeftColor);
// 右側
canvas.drawPath(
    Path()..moveTo(size.width - _kProPaddingWidth - _strokeWidth * 0.5, Offset.zero.dy)
      ..lineTo(size.width - _kProPaddingWidth - _strokeWidth * 0.5, _height)
      ..lineTo(size.width * leftProgress + _height * 0.5 + _spaceWidth * 0.5 + _strokeWidth, _height)
      ..lineTo(size.width * leftProgress - _height * 0.5 + _spaceWidth * 0.5 + _strokeWidth, Offset.zero.dy)
      ..lineTo(size.width - _kProPaddingWidth - _strokeWidth * 0.5, Offset.zero.dy)
      ..close(),
    _paint..color = rightColor ?? _kProRightColor);

2. 異常比例

????對于比例過小或過大的情況,小菜計劃展示一個固定的三角形,并且在此狀況下不進行文字繪制;

// 左側
if ((size.width * leftProgress + _height * 0.5 - _spaceWidth * 0.5 - _strokeWidth) <= _height) {
  _leftPath.lineTo(
      _height + _kProPaddingWidth + _strokeWidth * 0.5, _height);
} else if ((size.width * leftProgress + _height * 0.5) >= size.width - _height) {
  _leftPath.lineTo(
      size.width - _spaceWidth - _strokeWidth * 2 - _kProPaddingWidth, _height);
  _leftPath.lineTo(
      size.width - _spaceWidth - _strokeWidth * 2 - _height - _kProPaddingWidth, Offset.zero.dy);
} else {
  _leftPath.lineTo(
      size.width * leftProgress + _height * 0.5 - _spaceWidth * 0.5 - _strokeWidth, _height);
  _leftPath.lineTo(
      size.width * leftProgress - _height * 0.5 - _spaceWidth * 0.5 - _strokeWidth, Offset.zero.dy);
}
// 右側
if ((size.width * leftProgress + _height * 0.5 - _spaceWidth * 0.5 - _strokeWidth) <= _height) {
  _rightPath.lineTo(
      _height + _spaceWidth + _strokeWidth * 2 + _kProPaddingWidth, _height);
  _rightPath.lineTo(
      _spaceWidth + _strokeWidth * 2 + _kProPaddingWidth, Offset.zero.dy);
} else if ((size.width * leftProgress + _height * 0.5) >= size.width - _height) {
  _rightPath.lineTo(
      size.width - _height - _strokeWidth * 0.5 - _kProPaddingWidth, Offset.zero.dy);
} else {
  _rightPath.lineTo(
      size.width * leftProgress + _height * 0.5 + _spaceWidth * 0.5 + _strokeWidth, _height);
  _rightPath.lineTo(
      size.width * leftProgress - _height * 0.5 + _spaceWidth * 0.5 + _strokeWidth, Offset.zero.dy);
}

3. 是否填充

????對于梯形內容是否填充,可以通過 Paint().style = PaintingStyle.fill / stroke 來處理,但是需要注意的是,當 Path 設置了 strokeWidth 時,其填充狀態是邊框以內的范圍,即邊框設置越粗,填充范圍越小,其繪制的整體圖形也會越大,因此在計算時需要以邊框中間位置計算;小菜為了避免填充范圍不夠,設置在 PaintingStyle.fill 時降低邊框粗細為 0.5

_paint = _paint
  ..strokeWidth = (isFill == null || isFill == false) ? _strokeWidth : 0.5
  ..style = (isFill == null || isFill == false) ? PaintingStyle.stroke : PaintingStyle.fill;

Canvas.drawParagraph 繪制文字

????之前小菜有簡單介紹過 drawParagraph 文字繪制,其關鍵是對文字屬性及定位進行處理;

1. 左側文字

????文字范圍需要在梯形內,不能超過梯形長度,因此通過計算設置 ParagraphConstraints(width: _leftTextWidth) 文字寬度范圍;通過 ParagraphStyle 設置文字屬性,包括顏色,大小是否換行等;而最后通過 canvas.drawParagraph 設置文字起始位置(可以獲取段落高度);

if (_leftTextWidth > 0.0) {
  Color _leftColor;
  if (leftTextColor != null) {
    _leftColor = leftTextColor;
  } else if (isFill != null && this.isFill == true) {
    _leftColor = Colors.white;
  } else if (leftColor != null) {
    _leftColor = leftColor;
  } else {
    _leftColor = _kProLeftColor;
  }
  ParagraphBuilder _pb = ParagraphBuilder(ParagraphStyle(
      textAlign: TextAlign.left, fontWeight: FontWeight.w500,
      fontStyle: FontStyle.normal, maxLines: 1,
      ellipsis: '...', fontSize: 16))
    ..pushStyle(ui.TextStyle(color: _leftColor))
    ..addText(leftText);
  ParagraphConstraints pc = ParagraphConstraints(width: _leftTextWidth);
  Paragraph paragraph = _pb.build()..layout(pc);
  
  canvas.drawParagraph(paragraph, Offset(_kProPaddingWidth * 2 + _strokeWidth, _height * 0.5 - paragraph.height * 0.5));
}

2. 右側文字

????右側文字相對于左側略微復雜,首先通過 ParagraphStyle.textAlign 設置文字居右,再計算右側文字寬度時注意右側文字繪制的起始位置,注意邊框寬度及兩個梯形 spaceWidth 間距;最重要的是右側要有空余,小菜通過 addPlaceholder 添加占位符;

????注意:在起始位置與屏幕右側距離差小于設置的寬度時,占位符起作用但整體范圍在屏幕外,因此注意起始位置與文字段落寬度計算正確;

if (_rightTextWidth > 0.0) {
  Color _rightColor;
  if (rightTextColor != null) {
    _rightColor = rightTextColor;
  } else if (isFill != null && this.isFill == true) {
    _rightColor = Colors.white;
  } else if (rightColor != null) {
    _rightColor = rightColor;
  } else {
    _rightColor = _kProRightColor;
  }
  ParagraphBuilder _pb = ParagraphBuilder(ParagraphStyle(
      textAlign: TextAlign.right, fontWeight: FontWeight.w500,
      fontStyle: FontStyle.normal, maxLines: 1,
      ellipsis: '...', fontSize: 16))
    ..pushStyle(ui.TextStyle(color: _rightColor))
    ..addText(rightText)
    ..addPlaceholder(_kProPaddingWidth * 2 + _strokeWidth, Offset.zero.dy, PlaceholderAlignment.middle);
  ParagraphConstraints pc = ParagraphConstraints(width: _rightTextWidth);
  Paragraph paragraph = _pb.build()..layout(pc);

  canvas.drawParagraph(paragraph, Offset(_rightStartWidth, _height * 0.5 - paragraph.height * 0.5));
}

????ACEProgressPainter 案例源碼


????小菜對于進度對比圖主要通過 Canvas 進行繪制,未單獨封裝 Widget,其中 drawParagraph 還有很多隱藏熟悉小菜未曾嘗試;如有錯誤,請多多指導!

來源: 阿策小和尚

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,663評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,125評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,506評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,614評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,402評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,934評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,021評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,168評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,690評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,596評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,288評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,027評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,404評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,662評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,398評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,743評論 2 370

推薦閱讀更多精彩內容