gpt4 book ai didi

dart - 如何显示带有指示进度的点的进度条?

转载 作者:IT王子 更新时间:2023-10-29 06:33:36 25 4
gpt4 key购买 nike

Progress Bar example

如何使指示进度的小部件看起来像附加的图像?

最佳答案

好吧,您实际上可以通过扩展 CustomPainter 类来自己创建它,然后使用 CustomPaint 将其插入到您的小部件树中小部件。

我实际上已经设法为您提供了一个示例,您可以根据自己的需要随意调整它。结果是这样的:

enter image description here

class MyDeliveryProgress extends StatefulWidget {
_StepperState createState() => _StepperState();
}

class _StepperState extends State<MyDeliveryProgress> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: RichText(
text: TextSpan(
text: 'Ordered',
style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
children: [
TextSpan(
text: '\n20 Dec',
style: TextStyle(fontWeight: FontWeight.normal),
),
]),
textAlign: TextAlign.center,
)),
//Text('Ordered'),
Expanded(
flex: 3,
child: IntrinsicHeight(
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child: Text(
'Shipped (${DateTime.now().day.toString()} Dec)',
style: TextStyle(fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: CustomPaint(
painter: MyProgressLine(
estDelivery: DateTime(2019, 1, 1),
shipped: DateTime(2018, 12, 20),
),
child: Container(
width: double.infinity,
height: 5.0,
),
),
),
),
],
),
)),
Expanded(
child: RichText(
text: TextSpan(
text: 'Delivery',
style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
children: [
TextSpan(
text: '\n1 Jan',
style: TextStyle(fontWeight: FontWeight.normal),
),
]),
textAlign: TextAlign.center,
)),
],
),
)));
}
}

class MyProgressLine extends CustomPainter {
MyProgressLine({this.shipped, this.estDelivery});

final DateTime shipped;
final DateTime estDelivery;

@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = Colors.green
..strokeWidth = 3.0
..style = PaintingStyle.stroke;

double endPointsRadius = 5.0;
double width = size.width;

int totalDays = ((estDelivery.millisecondsSinceEpoch - shipped.millisecondsSinceEpoch) / 86400000).floor();
int currentDay = ((DateTime.now().millisecondsSinceEpoch - shipped.millisecondsSinceEpoch) / 86400000).floor();
double stepPerDay = width / totalDays;

// Draws starting point
canvas.drawCircle(Offset.zero, endPointsRadius, paint);
canvas.drawLine(Offset(endPointsRadius, 0.0), Offset(endPointsRadius + stepPerDay * currentDay, 0.0), paint);

// Draws current progress line
paint.style = PaintingStyle.fill;
canvas.drawCircle(Offset(endPointsRadius + stepPerDay * currentDay, 0.0), 3.0, paint);

// Draws endpoint
paint.style = PaintingStyle.stroke;
paint.color = Colors.grey.withOpacity(0.5);
canvas.drawLine(Offset(endPointsRadius + stepPerDay * currentDay, 0.0), Offset(stepPerDay * totalDays, 0.0), paint);
canvas.drawCircle(Offset((stepPerDay * totalDays) + endPointsRadius, 0.0), endPointsRadius, paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}

关于dart - 如何显示带有指示进度的点的进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53912650/

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