flutter - 如何在容器中心垂直和水平地画一条线?
问题描述
我想标记屏幕的确切中心。我将如何绘制两条线,一条垂直居中,一条水平居中?
解决方案
使用以下使用自定义painter的代码,您可以通过更改AKCustomPainter类中Paint类的color属性来改变线条的颜色
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
void main() => runApp(Demo());
class Demo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SnackBar Demo',
home: Scaffold(
appBar: AppBar(
title: Text('Demo'),
),
body: Container(
child: CustomPaint(
size: Size(800,500), //You can Replace this with your desired WIDTH and HEIGHT
painter: AKCustomPainter(),
)),
),
);
}
}
class AKCustomPainter extends CustomPainter{
@override
void paint(Canvas canvas, Size size) {
// Vertical line
Paint paint_0 = new Paint()
..color = Color.fromARGB(255, 33, 150, 243)
..style = PaintingStyle.stroke
..strokeWidth = 1.34;
Path path_0 = Path();
path_0.moveTo(size.width*0.50,0);
path_0.lineTo(size.width*0.50,size.height);
canvas.drawPath(path_0, paint_0);
// Horizontal line
Paint paint_1 = new Paint()
..color = Color.fromARGB(255, 33, 150, 243)
..style = PaintingStyle.stroke
..strokeWidth = 1.34;
Path path_1 = Path();
path_1.moveTo(0,size.height*0.50);
path_1.lineTo(size.width,size.height*0.50);
canvas.drawPath(path_1, paint_1);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
推荐阅读
- .net - Exchange 服务问题 .net Core
- java - 用 MouseListener 覆盖方法
- python - 在第一个地精之后,第二个地精不是随机生成的
- excel - 如何提取单元格公式参考中的值 - Office 365
- python - 有没有办法改变 my_sprite 或 my_group 来改变显示的动画?
- ocaml - 代码错误:“这是一个无法应用的功能”
- angular - 在 Angular8 的动态视图中添加友好的 Url 以使用 SEO
- javascript - React/JSX 中的三元运算符不起作用
- python - 如何从 Tkinter 类中检索一个值并在另一个类中使用它?
- python - PyArray_SimpleNewFromData 从 uint8_t 到 NPY_UINT16。适用于linux但不适用于windows