Skip to content

Commit 1203bf4

Browse files
authored
add: 百格齐放 (#10)
1 parent 4b3d7dd commit 1203bf4

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import 'dart:math' as math;
2+
3+
import 'package:flutter/cupertino.dart';
4+
import 'package:flutter/material.dart';
5+
6+
void main() {
7+
runApp(MyApp());
8+
}
9+
10+
class MyApp extends StatelessWidget {
11+
@override
12+
Widget build(BuildContext context) {
13+
return MaterialApp(
14+
title: 'Flutter Demo',
15+
home: HomePage(),
16+
);
17+
}
18+
}
19+
20+
class HomePage extends StatefulWidget {
21+
@override
22+
HomePageState createState() => HomePageState();
23+
}
24+
25+
class HomePageState extends State<HomePage> {
26+
List<int> upRatio = <int>[2, 5, 8, 3, 6, 9, 1, 4, 7];
27+
List<List<int>> radioList = [];
28+
int belowCount = 7;
29+
30+
@override
31+
void initState() {
32+
super.initState();
33+
//初始化比例数组
34+
for (final int radio in upRatio) {
35+
radioList.add(<int>[radio, 10 - radio]);
36+
}
37+
}
38+
39+
@override
40+
void dispose() {
41+
super.dispose();
42+
}
43+
44+
List<Widget> _upList() {
45+
//支持总条数为 m,且每条比例条的区域数为 n 的比例条,并要求每个区域的比例均可以通过数组控制;
46+
47+
final List<Widget> tList = [];
48+
for (final List<int> list in radioList) {
49+
tList.add(
50+
Expanded(
51+
child: Column(
52+
children: _item(list),
53+
),
54+
),
55+
);
56+
}
57+
return tList;
58+
}
59+
60+
List<Widget> _item(List<int> list) {
61+
final List<Widget> itemList = [];
62+
for (int i = 0; i < list.length; i++) {
63+
itemList.add(
64+
Expanded(
65+
child: Container(
66+
color: RandomColor.getColor(),
67+
child: Align(
68+
alignment: Alignment.center,
69+
child: Text(
70+
list[i].toString(),
71+
),
72+
),
73+
),
74+
flex: list[i],
75+
),
76+
);
77+
}
78+
return itemList;
79+
}
80+
81+
@override
82+
Widget build(BuildContext context) {
83+
return Scaffold(
84+
body: Flex(
85+
direction: MediaQuery.of(context).orientation == Orientation.portrait ? Axis.vertical : Axis.horizontal,
86+
children: [
87+
Expanded(
88+
child: Row(
89+
children: _upList(),
90+
),
91+
),
92+
Expanded(
93+
child: LayoutBuilder(
94+
builder: (BuildContext context, BoxConstraints constraints) {
95+
print('maxHeight: ${constraints.maxHeight}');
96+
print('maxWidth: ${constraints.maxWidth}');
97+
return CustomPaint(
98+
size: Size(constraints.maxWidth, constraints.maxHeight),
99+
painter: SPainter(belowCount),
100+
);
101+
},
102+
),
103+
),
104+
],
105+
),
106+
);
107+
}
108+
}
109+
110+
class RandomColor {
111+
const RandomColor._();
112+
113+
static final math.Random _random = math.Random();
114+
115+
static Color getColor() {
116+
return Color.fromARGB(
117+
255,
118+
_random.nextInt(255),
119+
_random.nextInt(255),
120+
_random.nextInt(255),
121+
);
122+
}
123+
}
124+
125+
class SPainter extends CustomPainter {
126+
SPainter(this.count);
127+
128+
int count;
129+
130+
@override
131+
void paint(Canvas canvas, Size size) {
132+
final Paint paint = Paint()
133+
..style = PaintingStyle.stroke
134+
..color = Colors.black
135+
..strokeWidth = 1.0
136+
..isAntiAlias = true;
137+
138+
final double h = size.height;
139+
final double w = size.width;
140+
final double divWidth = w / (count + 1);
141+
final double divHeight = h / (count + 1);
142+
// //辅助线
143+
// for(int i = 1; i <= count; i++){
144+
// final double dy = divHeight * i;
145+
// canvas.drawLine(Offset(0, dy), Offset(w, dy), paint);
146+
// }
147+
// for(int i = 1; i <= count; i++){
148+
// final double dx = divWidth * i;
149+
// canvas.drawLine(Offset(dx, 0), Offset(dx, h), paint);
150+
// }
151+
paint.style = PaintingStyle.fill;
152+
//从左上到右下
153+
for (int i = 1; i <= count; i++) {
154+
paint.color = RandomColor.getColor();
155+
canvas.drawRect(
156+
Rect.fromCenter(
157+
center: Offset(divWidth * i, divHeight * i),
158+
width: divWidth * 2,
159+
height: divHeight * 2,
160+
),
161+
paint,
162+
);
163+
}
164+
//从右上到左下
165+
for (int i = 1; i <= count; i++) {
166+
paint.color = RandomColor.getColor();
167+
canvas.drawRect(
168+
Rect.fromCenter(
169+
center: Offset(divWidth * (count + 1 - i), divHeight * i),
170+
width: divWidth * 2,
171+
height: divHeight * 2,
172+
),
173+
paint,
174+
);
175+
}
176+
}
177+
178+
@override
179+
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
180+
}

0 commit comments

Comments
 (0)