From f73570af2958e873fae33d005f8200c3130bed12 Mon Sep 17 00:00:00 2001 From: toly <1981462002@qq.com> Date: Sat, 30 Apr 2022 23:49:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=A3=E5=91=B3=E7=BB=98=E5=88=B6=20?= =?UTF-8?q?=E8=92=B2=E4=B8=B0=E6=8A=95=E9=92=88=E8=AF=95=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +-- lib/app/views/navigation/bloc_wrapper.dart | 12 ++-- lib/app/views/navigation/unit_navigation.dart | 1 - .../fun/bufeng/bufeng_panel.dart | 65 +++++++++++++++++ lib/painter_system/fun/bufeng/config.dart | 67 ++++++++++++++++++ lib/painter_system/fun/bufeng/painter.dart | 70 +++++++++++++++++++ lib/painter_system/gallery_factory.dart | 7 ++ .../StatefulWidget/AppBar/node2_tab.dart | 6 -- 8 files changed, 217 insertions(+), 19 deletions(-) create mode 100644 lib/painter_system/fun/bufeng/bufeng_panel.dart create mode 100644 lib/painter_system/fun/bufeng/config.dart create mode 100644 lib/painter_system/fun/bufeng/painter.dart diff --git a/README.md b/README.md index f68695f..378b87b 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,10 @@ > 当前Flutter 版本 ``` -Flutter 2.10.3 • channel stable • https://github.com/flutter/flutter.git -Framework • revision 7e9793dee1 (4 weeks ago) • 2022-03-02 11:23:12 -0600 -Engine • revision bd539267b4 -Tools • Dart 2.16.1 • DevTools 2.9.2 +Flutter 2.10.5 • channel stable • https://github.com/flutter/flutter.git +Framework • revision 5464c5bac7 (10 days ago) • 2022-04-18 09:55:37 -0700 +Engine • revision 57d3bac3dd +Tools • Dart 2.16.2 • DevTools 2.9.2 ``` --- diff --git a/lib/app/views/navigation/bloc_wrapper.dart b/lib/app/views/navigation/bloc_wrapper.dart index 2f810e4..45e2d09 100644 --- a/lib/app/views/navigation/bloc_wrapper.dart +++ b/lib/app/views/navigation/bloc_wrapper.dart @@ -42,17 +42,13 @@ class _BlocWrapperState extends State { providers: [ BlocProvider(create: (_) => GlobalBloc(storage)..add(const EventInitApp())), BlocProvider(create: (_) => WidgetsBloc(repository: repository)), - BlocProvider( - create: (_) => WidgetDetailBloc(repository: repository)), - BlocProvider( - create: (_) => categoryBloc), - BlocProvider( - create: (_) => LikeWidgetBloc(repository: repository)), + BlocProvider(create: (_) => WidgetDetailBloc(repository: repository)), + BlocProvider(create: (_) => categoryBloc), + BlocProvider(create: (_) => LikeWidgetBloc(repository: repository)), BlocProvider(create: (_) => RegisterBloc()), BlocProvider(create: (_) => LoginBloc(authenticBloc: authBloc)), BlocProvider(create: (_) => authBloc), - BlocProvider( - create: (_) => CategoryWidgetBloc(categoryBloc: categoryBloc)), + BlocProvider(create: (_) => CategoryWidgetBloc(categoryBloc: categoryBloc)), BlocProvider(create: (_) => PointBloc()), BlocProvider(create: (_) => UpdateBloc()), BlocProvider(create: (_) => GalleryUnitBloc()..loadGalleryInfo()), diff --git a/lib/app/views/navigation/unit_navigation.dart b/lib/app/views/navigation/unit_navigation.dart index 61ca498..83eb306 100644 --- a/lib/app/views/navigation/unit_navigation.dart +++ b/lib/app/views/navigation/unit_navigation.dart @@ -108,7 +108,6 @@ class _UnitNavigationState extends State { // 点击底部按钮事件,切换页面 void _onTapBottomNav(int index) { - // _controller.animateToPage(index, duration: const Duration(milliseconds: 200), curve: Curves.linear); _controller.jumpToPage(index); if(!isDark){ diff --git a/lib/painter_system/fun/bufeng/bufeng_panel.dart b/lib/painter_system/fun/bufeng/bufeng_panel.dart new file mode 100644 index 0000000..2366d01 --- /dev/null +++ b/lib/painter_system/fun/bufeng/bufeng_panel.dart @@ -0,0 +1,65 @@ +import 'package:flutter/material.dart'; +import 'painter.dart'; +import 'config.dart'; + + +class BufengPanel extends StatefulWidget { + const BufengPanel({Key? key}) : super(key: key); + + @override + State createState() => _BufengPanelState(); +} + +class _BufengPanelState extends State { + final Config config = Config( + size: const Size(200, 200), + ); + + void addNeedle({int count = 1}) { + for (int i = 0; i < count; i++) { + config.addNeedle(); + } + } + + @override + Widget build(BuildContext context) { + return Stack( + alignment: Alignment.topCenter, + children: [ + Padding( + padding: EdgeInsets.only(top: 10), + child: CustomPaint( + painter: PiPainter(config), + size: config.size, + ), + ), + Positioned( + right: 0, + top: 10, + child: Row( + children: [ + IconButton(onPressed: addNeedle, icon: const Icon(Icons.add)), + IconButton(onPressed: () => addNeedle(count: 100), icon: const Icon(Icons.add_chart)), + IconButton(onPressed: config.clear, icon: const Icon(Icons.refresh)), + ], + ), + ) + ], + ); + + // Scaffold( + // appBar: AppBar( + // title: const Text('蒲丰投针试验'), + // actions: + // [ + // IconButton(onPressed: addNeedle, icon: const Icon(Icons.add)), + // IconButton(onPressed: () => addNeedle(count: 100), icon: const Icon(Icons.add_chart)), + // IconButton(onPressed: config.clear, icon: const Icon(Icons.refresh)), + // ], + // ), + // body: Center( + // child: , + // ), + // ); + } +} diff --git a/lib/painter_system/fun/bufeng/config.dart b/lib/painter_system/fun/bufeng/config.dart new file mode 100644 index 0000000..c5dcf9c --- /dev/null +++ b/lib/painter_system/fun/bufeng/config.dart @@ -0,0 +1,67 @@ +import 'dart:math'; + +import 'package:flutter/cupertino.dart'; + +class Line { + final Offset p0; + final Offset p1; + + Line({ + required this.p0, + required this.p1, + }); + + bool isActive(double gap) { + int gapIndex0 = p0.dy ~/ gap; + int gapIndex1 = p1.dy < 0 ? -1 : p1.dy ~/ gap; + return gapIndex0 != gapIndex1 || + p0.dy % gap < 0.000000001 || + p1.dy % gap < 0.000000001; + } +} + +class Config extends ChangeNotifier { + int lineCount; + Size size; + List lines = []; + + Config({ + this.lineCount = 8, + this.size = const Size(200, 200), + }); + + double get gap => size.height / lineCount; + + @override + String toString() { + int n = lines.length; + int m = lines.where((line) => line.isActive(gap)).length; + double parserPi = m == 0 ? 0 : 2 * n / m; + return '投针个数: ${lines.length}\n命中个数: $m \n' + '估算圆周率 : $parserPi'; + } + + void addNeedle() { + Line line = _createNeedle(); + lines.add(line); + notifyListeners(); + } + + final Random _random = Random(); + + Line _createNeedle() { + double x = _random.nextDouble() * size.width; + double y = _random.nextDouble() * size.height; + + double rad = 2 * pi * _random.nextDouble(); + double dx = gap * cos(rad); + double dy = gap * sin(rad); + Line line = Line(p0: Offset(x, y), p1: Offset(x + dx, y + dy)); + return line; + } + + void clear() { + lines.clear(); + notifyListeners(); + } +} diff --git a/lib/painter_system/fun/bufeng/painter.dart b/lib/painter_system/fun/bufeng/painter.dart new file mode 100644 index 0000000..e15cf9e --- /dev/null +++ b/lib/painter_system/fun/bufeng/painter.dart @@ -0,0 +1,70 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'config.dart'; + +class PiPainter extends CustomPainter { + final Config config; + + PiPainter(this.config) : super(repaint: config); + + @override + void paint(Canvas canvas, Size size) { + print(size); + Paint paint = Paint(); + paint.style = PaintingStyle.stroke; + final double span = config.gap; + drawHelpText( + '$config', + canvas, + Offset.zero, + ); + canvas.translate(0, size.height*0.3); + + canvas.save(); + for (int i = 0; i <= config.lineCount; i++) { + canvas.drawLine(Offset.zero, Offset(size.width, 0), paint); + print(span*i); + canvas.translate(0, span); + } + canvas.restore(); + + Paint needlePaint = Paint(); + needlePaint.style = PaintingStyle.stroke; + needlePaint.strokeWidth = 1; + + for (Line line in config.lines) { + if (line.isActive(config.gap)) { + needlePaint.color = Colors.red; + } else { + needlePaint.color = Colors.green; + } + canvas.drawLine(line.p0, line.p1, needlePaint); + } + + + } + + @override + bool shouldRepaint(covariant PiPainter oldDelegate) { + return config != oldDelegate.config; + } + + final TextPainter textPainter = TextPainter( + textAlign: TextAlign.start, + textDirection: TextDirection.ltr, + ); + + void drawHelpText( + String text, + Canvas canvas, + Offset offset, { + Color color = Colors.lightBlue, + }) { + textPainter.text = TextSpan( + text: text, + style: TextStyle(fontSize: 14, color: color), + ); + textPainter.layout(maxWidth: 250); + textPainter.paint(canvas, offset); + } +} diff --git a/lib/painter_system/gallery_factory.dart b/lib/painter_system/gallery_factory.dart index ef65a83..6bce9f3 100644 --- a/lib/painter_system/gallery_factory.dart +++ b/lib/painter_system/gallery_factory.dart @@ -22,6 +22,7 @@ import 'base/draw_picture.dart'; import 'base/n_side/n_side_page.dart'; import 'base/polar/polar_painter_widget.dart'; +import 'fun/bufeng/bufeng_panel.dart'; import 'fun/dundun_view.dart'; import 'fun/random_portrait.dart'; import 'fun/stemp/stamp_paper.dart'; @@ -168,6 +169,12 @@ class GalleryFactory { srcUrl: '/fun/dundun_view.dart', info: "本样例是绘制 2022 年北京冬奥会吉祥物冰墩墩的形体,从中可以学到路径绘制、渐变色等知识。", content: DunDunView()), + FrameShower( + title: "蒲丰投针试验", + author: "张风捷特烈", + srcUrl: '/fun/bufeng', + info: "本样实现蒲丰投针试验的测试过程,根据概率来估算圆周率。其中可以学习到一些绘制小技巧已经数据的逻辑处理。", + content: BufengPanel()), FrameShower( title: "井字棋", author: "张风捷特烈", diff --git a/lib/widget_system/widgets/StatefulWidget/AppBar/node2_tab.dart b/lib/widget_system/widgets/StatefulWidget/AppBar/node2_tab.dart index 6f4e921..29301e1 100755 --- a/lib/widget_system/widgets/StatefulWidget/AppBar/node2_tab.dart +++ b/lib/widget_system/widgets/StatefulWidget/AppBar/node2_tab.dart @@ -35,12 +35,6 @@ class _TabAppBarState extends State super.dispose(); } -// decoration: const BoxDecoration( -// image: DecorationImage( -// image: AssetImage( -// "assets/images/sabar.webp", -// ), -// fit: BoxFit.cover)) @override Widget build(BuildContext context) { return SizedBox(