我需要 lil 帮助在不使用谷歌地图的折线的情况下在 2 点之间绘制虚线。 我尝试使用画布,它确实但不完全从位置下方和上方开始。
现在你可以看到 2 点以后它会超过 2 点。如果有人帮助我实现这一目标,我真的很感激。
我需要 lil 帮助在不使用谷歌地图的折线的情况下在 2 点之间绘制虚线。 我尝试使用画布,它确实但不完全从位置下方和上方开始。
现在你可以看到 2 点以后它会超过 2 点。如果有人帮助我实现这一目标,我真的很感激。
我使用https://pub.dev/packages/flutter_dash制作了几乎相同的小部件,您还可以根据自己的风格自定义此小部件。
这是代码,希望对您有所帮助。
Column(children: <Widget>[
Container(
margin: EdgeInsets.only(top: 16),
height: 25,
width: 25,
decoration: BoxDecoration(
shape: BoxShape.circle,
border:
Border.all(width: 1.5, color: Colors.greenAccent)),
),
Dash(
direction: Axis.vertical,
length: 130,
dashLength: 15,
dashColor: grey),
Container(
height: 25,
width: 25,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
border: Border.all(width: 2, color: red)),
child: Container(
height: 20,
),
),
],
),
我认为绘图比创建更多容器(如上述容器)更有效。如果不想使用该库,可以使用我下面的方法,只需添加几行:
创建类DashedLineVerticalPainter
:
class DashedLineVerticalPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
double dashHeight = 5, dashSpace = 3, startY = 0;
final paint = Paint()
..color = Colors.grey[300]
..strokeWidth = 1;
while (startY < size.height) {
canvas.drawLine(Offset(0, startY), Offset(0, startY + dashHeight), paint);
startY += dashHeight + dashSpace;
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
并使用它:
CustomPaint(
size: Size(1, double.infinity),
painter: DashedLineVerticalPainter()
)
结果:
class _MyWidgetState extends State<MyWidget> {
List<Model> list = [];
@override
void initState() {
super.initState();
list.add(Model("Hyderabad", Colors.red));
list.add(Model("Visakhapatnam", Colors.green));
list.add(Model("Vijayawada", Colors.blue));
}
void addNew() {
setState(() {
list.add(Model("Karnool", Colors.black));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title:
Text('Custom Stepper', style: TextStyle(color: Colors.white)),
actions: [
IconButton(
icon: Icon(Icons.add_circle, color: Colors.white),
onPressed: addNew)
]),
body: Container(
padding: EdgeInsets.all(15),
color: Colors.white,
child: ListView.builder(
itemCount: list.length,
itemBuilder: (con, ind) {
return ind != 0
? Column(mainAxisSize: MainAxisSize.min, children: [
Row(children: [
Column(
children: List.generate(
3,
(ii) => Padding(
padding: EdgeInsets.only(
left: 10, right: 10, top: 5, bottom: 5),
child: Container(
height: 3,
width: 2,
color: Colors.grey,
)),
),
),
Expanded(
child: Container(
color: Colors.grey.withAlpha(60),
height: 0.5,
padding: EdgeInsets.only(
left: 10,
right: 20,
),
))
]),
Row(children: [
Icon(Icons.location_on, color: list[ind].color),
Text(list[ind].address,
style: TextStyle(color: list[ind].color))
])
])
: Row(children: [
Icon(Icons.location_on, color: list[ind].color),
Text(list[ind].address,
style: TextStyle(color: list[ind].color))
]);
})));
}
}
class Model {
String address;
double lat;
double long;
Color color;
//Other fields if needed....
Model(this.address, this.color);
//initialise other fields so on....
}
return Container(
child: Row(
children: <Widget>[
Column(children: <Widget>[
Icon(Icons.radio_button_checked,color: Colors.orange,)
Dash(
direction: Axis.vertical,
length: 20,
dashLength: 5,
thickness:3.0,
dashColor: Colors.grey[400]),
Icon(Icons.location_on,color: Colors.blue,)
],
),
Column(children: <Widget>[
Text("Some text"),
Divider(),
Text("Some Text")
],
),
],
)
);