flutter - 如何在一行中添加相同的矩形 10 次
问题描述
我已经创建了一个小矩形,如何在一行中一个接一个地显示 10 个这样的矩形。
我希望 10 个这样的矩形一个接一个地以 aRow 方式显示,但实际上,它只显示一个,我什至尝试在行内添加画布。This is the output I got
class YourdrawRect extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
canvas.drawRect(
new Rect.fromLTRB(0.0, 0.0, 4.0, 100.0),
new Paint()..color = new Color(0xFF0099FF),
);
Padding(padding: EdgeInsets.all(8.0));
canvas.drawRect(
new Rect.fromLTRB(0.0, 0.0, 4.0, 100.0),
new Paint()..color = new Color(0xFF0099FE),
);
Padding(
padding: EdgeInsets.all(9.0),
);
}
}
解决方案
此代码应该可以工作:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: MyApp(),
));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('text'),
),
body: Container(
padding: EdgeInsets.all(32.0),
child: Center(
child: Row(
children: <Widget>[
// You can add 10 YourRect() here or use loop
Padding(
padding: const EdgeInsets.all(8.0),
child: CustomPaint(
painter: (YourRect()),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: CustomPaint(
painter: (YourRect()),
),
),
],
)),
),
);
}
}
class YourRect extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
canvas.drawRect(
new Rect.fromLTRB(0.0, 0.0, 4.0, 100.0),
new Paint()..color = new Color(0xFF0099FF),
);
}
@override
bool shouldRepaint(YourRect oldDelegate) {
return false;
}
}
推荐阅读
- css - 伪元素上的 CSS 过渡不适用于简单的背景颜色更改
- c# - 如何检查我是否有来自我的 SQL Server 数据库的图像或不使用 C#
- html - 有没有办法根据数字输入创建渐变范围滑块?
- amazon-s3 - 在 AWS SageMaker(jupyter notebook)中从 AWS S3 打开 .Parquet 文件
- mongodb - NestJS API 不适用于 MongoDB(索引键不能为空)
- java - 尝试使用将对象转换为字节数组时的频繁 GC
- react-native - react-native-svg , react-native-svg-uri 运行 app-debug.apk 或 release.apk 时出现网络错误
- r - gganimate 在帧之间更改轴
- sql-server - 同一列中的多个过滤器、聚合和分组
- python - R中的并行处理调用网状python函数