Skip to content

Commit f73570a

Browse files
committed
趣味绘制 蒲丰投针试验
1 parent 44043da commit f73570a

File tree

8 files changed

+217
-19
lines changed

8 files changed

+217
-19
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
> 当前Flutter 版本
2626
2727
```
28-
Flutter 2.10.3 • channel stable • https://github.com/flutter/flutter.git
29-
Framework • revision 7e9793dee1 (4 weeks ago) • 2022-03-02 11:23:12 -0600
30-
Engine • revision bd539267b4
31-
Tools • Dart 2.16.1 • DevTools 2.9.2
28+
Flutter 2.10.5 • channel stable • https://github.com/flutter/flutter.git
29+
Framework • revision 5464c5bac7 (10 days ago) • 2022-04-18 09:55:37 -0700
30+
Engine • revision 57d3bac3dd
31+
Tools • Dart 2.16.2 • DevTools 2.9.2
3232
```
3333

3434
---

lib/app/views/navigation/bloc_wrapper.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,13 @@ class _BlocWrapperState extends State<BlocWrapper> {
4242
providers: [
4343
BlocProvider<GlobalBloc>(create: (_) => GlobalBloc(storage)..add(const EventInitApp())),
4444
BlocProvider<WidgetsBloc>(create: (_) => WidgetsBloc(repository: repository)),
45-
BlocProvider<WidgetDetailBloc>(
46-
create: (_) => WidgetDetailBloc(repository: repository)),
47-
BlocProvider<CategoryBloc>(
48-
create: (_) => categoryBloc),
49-
BlocProvider<LikeWidgetBloc>(
50-
create: (_) => LikeWidgetBloc(repository: repository)),
45+
BlocProvider<WidgetDetailBloc>(create: (_) => WidgetDetailBloc(repository: repository)),
46+
BlocProvider<CategoryBloc>(create: (_) => categoryBloc),
47+
BlocProvider<LikeWidgetBloc>(create: (_) => LikeWidgetBloc(repository: repository)),
5148
BlocProvider<RegisterBloc>(create: (_) => RegisterBloc()),
5249
BlocProvider<LoginBloc>(create: (_) => LoginBloc(authenticBloc: authBloc)),
5350
BlocProvider<AuthenticBloc>(create: (_) => authBloc),
54-
BlocProvider<CategoryWidgetBloc>(
55-
create: (_) => CategoryWidgetBloc(categoryBloc: categoryBloc)),
51+
BlocProvider<CategoryWidgetBloc>(create: (_) => CategoryWidgetBloc(categoryBloc: categoryBloc)),
5652
BlocProvider<PointBloc>(create: (_) => PointBloc()),
5753
BlocProvider<UpdateBloc>(create: (_) => UpdateBloc()),
5854
BlocProvider<GalleryUnitBloc>(create: (_) => GalleryUnitBloc()..loadGalleryInfo()),

lib/app/views/navigation/unit_navigation.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ class _UnitNavigationState extends State<UnitNavigation> {
108108

109109
// 点击底部按钮事件,切换页面
110110
void _onTapBottomNav(int index) {
111-
// _controller.animateToPage(index, duration: const Duration(milliseconds: 200), curve: Curves.linear);
112111
_controller.jumpToPage(index);
113112

114113
if(!isDark){
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'package:flutter/material.dart';
2+
import 'painter.dart';
3+
import 'config.dart';
4+
5+
6+
class BufengPanel extends StatefulWidget {
7+
const BufengPanel({Key? key}) : super(key: key);
8+
9+
@override
10+
State<BufengPanel> createState() => _BufengPanelState();
11+
}
12+
13+
class _BufengPanelState extends State<BufengPanel> {
14+
final Config config = Config(
15+
size: const Size(200, 200),
16+
);
17+
18+
void addNeedle({int count = 1}) {
19+
for (int i = 0; i < count; i++) {
20+
config.addNeedle();
21+
}
22+
}
23+
24+
@override
25+
Widget build(BuildContext context) {
26+
return Stack(
27+
alignment: Alignment.topCenter,
28+
children: [
29+
Padding(
30+
padding: EdgeInsets.only(top: 10),
31+
child: CustomPaint(
32+
painter: PiPainter(config),
33+
size: config.size,
34+
),
35+
),
36+
Positioned(
37+
right: 0,
38+
top: 10,
39+
child: Row(
40+
children: [
41+
IconButton(onPressed: addNeedle, icon: const Icon(Icons.add)),
42+
IconButton(onPressed: () => addNeedle(count: 100), icon: const Icon(Icons.add_chart)),
43+
IconButton(onPressed: config.clear, icon: const Icon(Icons.refresh)),
44+
],
45+
),
46+
)
47+
],
48+
);
49+
50+
// Scaffold(
51+
// appBar: AppBar(
52+
// title: const Text('蒲丰投针试验'),
53+
// actions:
54+
// [
55+
// IconButton(onPressed: addNeedle, icon: const Icon(Icons.add)),
56+
// IconButton(onPressed: () => addNeedle(count: 100), icon: const Icon(Icons.add_chart)),
57+
// IconButton(onPressed: config.clear, icon: const Icon(Icons.refresh)),
58+
// ],
59+
// ),
60+
// body: Center(
61+
// child: ,
62+
// ),
63+
// );
64+
}
65+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import 'dart:math';
2+
3+
import 'package:flutter/cupertino.dart';
4+
5+
class Line {
6+
final Offset p0;
7+
final Offset p1;
8+
9+
Line({
10+
required this.p0,
11+
required this.p1,
12+
});
13+
14+
bool isActive(double gap) {
15+
int gapIndex0 = p0.dy ~/ gap;
16+
int gapIndex1 = p1.dy < 0 ? -1 : p1.dy ~/ gap;
17+
return gapIndex0 != gapIndex1 ||
18+
p0.dy % gap < 0.000000001 ||
19+
p1.dy % gap < 0.000000001;
20+
}
21+
}
22+
23+
class Config extends ChangeNotifier {
24+
int lineCount;
25+
Size size;
26+
List<Line> lines = [];
27+
28+
Config({
29+
this.lineCount = 8,
30+
this.size = const Size(200, 200),
31+
});
32+
33+
double get gap => size.height / lineCount;
34+
35+
@override
36+
String toString() {
37+
int n = lines.length;
38+
int m = lines.where((line) => line.isActive(gap)).length;
39+
double parserPi = m == 0 ? 0 : 2 * n / m;
40+
return '投针个数: ${lines.length}\n命中个数: $m \n'
41+
'估算圆周率 : $parserPi';
42+
}
43+
44+
void addNeedle() {
45+
Line line = _createNeedle();
46+
lines.add(line);
47+
notifyListeners();
48+
}
49+
50+
final Random _random = Random();
51+
52+
Line _createNeedle() {
53+
double x = _random.nextDouble() * size.width;
54+
double y = _random.nextDouble() * size.height;
55+
56+
double rad = 2 * pi * _random.nextDouble();
57+
double dx = gap * cos(rad);
58+
double dy = gap * sin(rad);
59+
Line line = Line(p0: Offset(x, y), p1: Offset(x + dx, y + dy));
60+
return line;
61+
}
62+
63+
void clear() {
64+
lines.clear();
65+
notifyListeners();
66+
}
67+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:flutter/material.dart';
3+
import 'config.dart';
4+
5+
class PiPainter extends CustomPainter {
6+
final Config config;
7+
8+
PiPainter(this.config) : super(repaint: config);
9+
10+
@override
11+
void paint(Canvas canvas, Size size) {
12+
print(size);
13+
Paint paint = Paint();
14+
paint.style = PaintingStyle.stroke;
15+
final double span = config.gap;
16+
drawHelpText(
17+
'$config',
18+
canvas,
19+
Offset.zero,
20+
);
21+
canvas.translate(0, size.height*0.3);
22+
23+
canvas.save();
24+
for (int i = 0; i <= config.lineCount; i++) {
25+
canvas.drawLine(Offset.zero, Offset(size.width, 0), paint);
26+
print(span*i);
27+
canvas.translate(0, span);
28+
}
29+
canvas.restore();
30+
31+
Paint needlePaint = Paint();
32+
needlePaint.style = PaintingStyle.stroke;
33+
needlePaint.strokeWidth = 1;
34+
35+
for (Line line in config.lines) {
36+
if (line.isActive(config.gap)) {
37+
needlePaint.color = Colors.red;
38+
} else {
39+
needlePaint.color = Colors.green;
40+
}
41+
canvas.drawLine(line.p0, line.p1, needlePaint);
42+
}
43+
44+
45+
}
46+
47+
@override
48+
bool shouldRepaint(covariant PiPainter oldDelegate) {
49+
return config != oldDelegate.config;
50+
}
51+
52+
final TextPainter textPainter = TextPainter(
53+
textAlign: TextAlign.start,
54+
textDirection: TextDirection.ltr,
55+
);
56+
57+
void drawHelpText(
58+
String text,
59+
Canvas canvas,
60+
Offset offset, {
61+
Color color = Colors.lightBlue,
62+
}) {
63+
textPainter.text = TextSpan(
64+
text: text,
65+
style: TextStyle(fontSize: 14, color: color),
66+
);
67+
textPainter.layout(maxWidth: 250);
68+
textPainter.paint(canvas, offset);
69+
}
70+
}

lib/painter_system/gallery_factory.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import 'base/draw_picture.dart';
2222

2323
import 'base/n_side/n_side_page.dart';
2424
import 'base/polar/polar_painter_widget.dart';
25+
import 'fun/bufeng/bufeng_panel.dart';
2526
import 'fun/dundun_view.dart';
2627
import 'fun/random_portrait.dart';
2728
import 'fun/stemp/stamp_paper.dart';
@@ -168,6 +169,12 @@ class GalleryFactory {
168169
srcUrl: '/fun/dundun_view.dart',
169170
info: "本样例是绘制 2022 年北京冬奥会吉祥物冰墩墩的形体,从中可以学到路径绘制、渐变色等知识。",
170171
content: DunDunView()),
172+
FrameShower(
173+
title: "蒲丰投针试验",
174+
author: "张风捷特烈",
175+
srcUrl: '/fun/bufeng',
176+
info: "本样实现蒲丰投针试验的测试过程,根据概率来估算圆周率。其中可以学习到一些绘制小技巧已经数据的逻辑处理。",
177+
content: BufengPanel()),
171178
FrameShower(
172179
title: "井字棋",
173180
author: "张风捷特烈",

lib/widget_system/widgets/StatefulWidget/AppBar/node2_tab.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ class _TabAppBarState extends State<TabAppBar>
3535
super.dispose();
3636
}
3737

38-
// decoration: const BoxDecoration(
39-
// image: DecorationImage(
40-
// image: AssetImage(
41-
// "assets/images/sabar.webp",
42-
// ),
43-
// fit: BoxFit.cover))
4438
@override
4539
Widget build(BuildContext context) {
4640
return SizedBox(

0 commit comments

Comments
 (0)