gpt4 book ai didi

dart - Flutter:创建时间线 UI

转载 作者:IT老高 更新时间:2023-10-28 12:40:52 31 4
gpt4 key购买 nike

我正在尝试制作如下的时间线 UI:

enter image description here


但我最终做了以下事情:

enter image description here

当我的描述文本的行数没有增加时,我想增加垂直分隔符的高度。我该怎么做?

这里是 link for the code

最佳答案

我也喜欢 Osama 的回答,但这是我的快速自定义实现。它使用 CustomPainter 来绘制线条。

import 'package:flutter/material.dart';

class Timeline extends StatelessWidget {
const Timeline({
Key? key,
required this.children,
this.indicators,
this.isLeftAligned = true,
this.itemGap = 12.0,
this.gutterSpacing = 4.0,
this.padding = const EdgeInsets.all(8),
this.controller,
this.lineColor = Colors.grey,
this.physics,
this.shrinkWrap = true,
this.primary = false,
this.reverse = false,
this.indicatorSize = 30.0,
this.lineGap = 4.0,
this.indicatorColor = Colors.blue,
this.indicatorStyle = PaintingStyle.fill,
this.strokeCap = StrokeCap.butt,
this.strokeWidth = 2.0,
this.style = PaintingStyle.stroke,
}) : itemCount = children.length,
assert(itemGap >= 0),
assert(lineGap >= 0),
assert(indicators == null || children.length == indicators.length),
super(key: key);

final List<Widget> children;
final double itemGap;
final double gutterSpacing;
final List<Widget>? indicators;
final bool isLeftAligned;
final EdgeInsets padding;
final ScrollController? controller;
final int itemCount;
final ScrollPhysics? physics;
final bool shrinkWrap;
final bool primary;
final bool reverse;

final Color lineColor;
final double lineGap;
final double indicatorSize;
final Color indicatorColor;
final PaintingStyle indicatorStyle;
final StrokeCap strokeCap;
final double strokeWidth;
final PaintingStyle style;

@override
Widget build(BuildContext context) {
return ListView.separated(
padding: padding,
separatorBuilder: (_, __) => SizedBox(height: itemGap),
physics: physics,
shrinkWrap: shrinkWrap,
itemCount: itemCount,
controller: controller,
reverse: reverse,
primary: primary,
itemBuilder: (context, index) {
final child = children[index];
final _indicators = indicators;

Widget? indicator;
if (_indicators != null) {
indicator = _indicators[index];
}

final isFirst = index == 0;
final isLast = index == itemCount - 1;

final timelineTile = <Widget>[
CustomPaint(
foregroundPainter: _TimelinePainter(
hideDefaultIndicator: indicator != null,
lineColor: lineColor,
indicatorColor: indicatorColor,
indicatorSize: indicatorSize,
indicatorStyle: indicatorStyle,
isFirst: isFirst,
isLast: isLast,
lineGap: lineGap,
strokeCap: strokeCap,
strokeWidth: strokeWidth,
style: style,
itemGap: itemGap,
),
child: SizedBox(
height: double.infinity,
width: indicatorSize,
child: indicator,
),
),
SizedBox(width: gutterSpacing),
Expanded(child: child),
];

return IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children:
isLeftAligned ? timelineTile : timelineTile.reversed.toList(),
),
);
},
);
}
}

class _TimelinePainter extends CustomPainter {
_TimelinePainter({
required this.hideDefaultIndicator,
required this.indicatorColor,
required this.indicatorStyle,
required this.indicatorSize,
required this.lineGap,
required this.strokeCap,
required this.strokeWidth,
required this.style,
required this.lineColor,
required this.isFirst,
required this.isLast,
required this.itemGap,
}) : linePaint = Paint()
..color = lineColor
..strokeCap = strokeCap
..strokeWidth = strokeWidth
..style = style,
circlePaint = Paint()
..color = indicatorColor
..style = indicatorStyle;

final bool hideDefaultIndicator;
final Color indicatorColor;
final PaintingStyle indicatorStyle;
final double indicatorSize;
final double lineGap;
final StrokeCap strokeCap;
final double strokeWidth;
final PaintingStyle style;
final Color lineColor;
final Paint linePaint;
final Paint circlePaint;
final bool isFirst;
final bool isLast;
final double itemGap;

@override
void paint(Canvas canvas, Size size) {
final indicatorRadius = indicatorSize / 2;
final halfItemGap = itemGap / 2;
final indicatorMargin = indicatorRadius + lineGap;

final top = size.topLeft(Offset(indicatorRadius, 0.0 - halfItemGap));
final centerTop = size.centerLeft(
Offset(indicatorRadius, -indicatorMargin),
);

final bottom = size.bottomLeft(Offset(indicatorRadius, 0.0 + halfItemGap));
final centerBottom = size.centerLeft(
Offset(indicatorRadius, indicatorMargin),
);

if (!isFirst) canvas.drawLine(top, centerTop, linePaint);
if (!isLast) canvas.drawLine(centerBottom, bottom, linePaint);

if (!hideDefaultIndicator) {
final Offset offsetCenter = size.centerLeft(Offset(indicatorRadius, 0));

canvas.drawCircle(offsetCenter, indicatorRadius, circlePaint);
}
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}

你可以这样调用它:

Timeline(
children: <Widget>[
Container(height: 100, color: color),
Container(height: 50, color: color),
Container(height: 200, color: color),
Container(height: 100, color: color),
],
indicators: <Widget>[
Icon(Icons.access_alarm),
Icon(Icons.backup),
Icon(Icons.accessibility_new),
Icon(Icons.access_alarm),
],
),

An Image of a timeline mobile ui

关于dart - Flutter:创建时间线 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49635381/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com