Skip to content

Commit e5251a6

Browse files
committed
✨ 更新flutter unit mac版本
1 parent 527fa56 commit e5251a6

File tree

9 files changed

+328
-1
lines changed

9 files changed

+328
-1
lines changed

assets/flutter.db

4 KB
Binary file not shown.
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import 'dart:ui';
2+
3+
import 'package:flutter/cupertino.dart';
4+
import 'package:flutter/material.dart';
5+
6+
/// create by 张风捷特烈 on 2020/7/22
7+
/// contact me by email [email protected]
8+
/// 说明: 264 RepaintBoundary 重绘边界 为子组件创建一个单独的显示列表,提升性能。源码中在TextField、DrawerController、Scrollbar、Sliver等组件中均有应用。
9+
// {
10+
// "widgetId": 264,
11+
// "name": "RepaintBoundary基本使用",
12+
// "priority": 1,
13+
// "subtitle": "【child】 : 子组件 【Widget】\n"
14+
// "比如上面的绘制视图,即使shouldRepaint为false,在滑动中会也会不断执行paint方法,使用RepaintBoundary可以避免不必要的绘制。",
15+
// }
16+
17+
class RepaintBoundaryDemo extends StatelessWidget{
18+
@override
19+
Widget build(BuildContext context) {
20+
return RepaintBoundary(
21+
child: TempPlayBezier3Page(),
22+
);
23+
}
24+
}
25+
26+
class TempPlayBezier3Page extends StatefulWidget {
27+
@override
28+
_TempPlayBezier3PageState createState() => _TempPlayBezier3PageState();
29+
}
30+
31+
class _TempPlayBezier3PageState extends State<TempPlayBezier3Page> {
32+
List<Offset> _pos = <Offset>[];
33+
int selectPos;
34+
35+
@override
36+
void initState() {
37+
_initPoints();
38+
super.initState();
39+
}
40+
41+
void _initPoints() {
42+
_pos = List<Offset>();
43+
_pos.add(Offset(0, 0));
44+
_pos.add(Offset(60, -60));
45+
_pos.add(Offset(-90, -90));
46+
_pos.add(Offset(-120, -40));
47+
}
48+
49+
@override
50+
Widget build(BuildContext context) {
51+
return Container(
52+
height: 200,
53+
width: MediaQuery.of(context).size.width,
54+
child: CustomPaint(
55+
painter: TempBezierPainter(pos: _pos, selectPos: selectPos),
56+
),
57+
);
58+
}
59+
}
60+
61+
class TempBezierPainter extends CustomPainter {
62+
Paint _gridPaint;
63+
Path _gridPath;
64+
65+
Paint _mainPaint;
66+
Path _mainPath;
67+
int selectPos;
68+
Paint _helpPaint;
69+
70+
List<Offset> pos;
71+
72+
TempBezierPainter({this.pos, this.selectPos}) {
73+
_gridPaint = Paint()..style = PaintingStyle.stroke;
74+
_gridPath = Path();
75+
76+
_mainPaint = Paint()
77+
..color = Colors.orange
78+
..style = PaintingStyle.stroke
79+
..strokeWidth = 2;
80+
_mainPath = Path();
81+
82+
_helpPaint = Paint()
83+
..color = Colors.purple
84+
..style = PaintingStyle.stroke
85+
..strokeWidth = 2
86+
..strokeCap = StrokeCap.round;
87+
}
88+
89+
@override
90+
void paint(Canvas canvas, Size size) {
91+
print('----------Paint-------');
92+
canvas.clipRect(Offset.zero & size);
93+
canvas.translate(size.width / 2, size.height / 2);
94+
_drawGrid(canvas, size); //绘制格线
95+
_drawAxis(canvas, size); //绘制轴线
96+
97+
_mainPath.moveTo(pos[0].dx, pos[0].dy);
98+
_mainPath.cubicTo(
99+
pos[1].dx, pos[1].dy, pos[2].dx, pos[2].dy, pos[3].dx, pos[3].dy);
100+
canvas.drawPath(_mainPath, _mainPaint);
101+
_drawHelp(canvas);
102+
_drawSelectPos(canvas);
103+
}
104+
105+
@override
106+
bool shouldRepaint(CustomPainter oldDelegate) => false;
107+
108+
void _drawGrid(Canvas canvas, Size size) {
109+
_gridPaint
110+
..color = Colors.grey
111+
..strokeWidth = 0.5;
112+
_gridPath = _buildGridPath(_gridPath, size);
113+
canvas.drawPath(_buildGridPath(_gridPath, size), _gridPaint);
114+
115+
canvas.save();
116+
canvas.scale(1, -1); //沿x轴镜像
117+
canvas.drawPath(_gridPath, _gridPaint);
118+
canvas.restore();
119+
120+
canvas.save();
121+
canvas.scale(-1, 1); //沿y轴镜像
122+
canvas.drawPath(_gridPath, _gridPaint);
123+
canvas.restore();
124+
125+
canvas.save();
126+
canvas.scale(-1, -1); //沿原点镜像
127+
canvas.drawPath(_gridPath, _gridPaint);
128+
canvas.restore();
129+
}
130+
131+
void _drawAxis(Canvas canvas, Size size) {
132+
canvas.drawPoints(
133+
PointMode.lines,
134+
[
135+
Offset(-size.width / 2, 0),
136+
Offset(size.width / 2, 0),
137+
Offset(0, -size.height / 2),
138+
Offset(0, size.height / 2),
139+
Offset(0, size.height / 2),
140+
Offset(0 - 7.0, size.height / 2 - 10),
141+
Offset(0, size.height / 2),
142+
Offset(0 + 7.0, size.height / 2 - 10),
143+
Offset(size.width / 2, 0),
144+
Offset(size.width / 2 - 10, 7),
145+
Offset(size.width / 2, 0),
146+
Offset(size.width / 2 - 10, -7),
147+
],
148+
_gridPaint
149+
..color = Colors.blue
150+
..strokeWidth = 1.5);
151+
}
152+
153+
Path _buildGridPath(Path path, Size size, {step = 20.0}) {
154+
for (int i = 0; i < size.height / 2 / step; i++) {
155+
path.moveTo(0, step * i);
156+
path.relativeLineTo(size.width / 2, 0);
157+
}
158+
for (int i = 0; i < size.width / 2 / step; i++) {
159+
path.moveTo(step * i, 0);
160+
path.relativeLineTo(
161+
0,
162+
size.height / 2,
163+
);
164+
}
165+
return path;
166+
}
167+
168+
void _drawHelp(Canvas canvas) {
169+
canvas.drawPoints(PointMode.lines, pos, _helpPaint..strokeWidth = 1);
170+
canvas.drawPoints(PointMode.points, pos, _helpPaint..strokeWidth = 8);
171+
}
172+
173+
void _drawSelectPos(Canvas canvas) {
174+
if (selectPos == null) return;
175+
canvas.drawCircle(
176+
pos[selectPos],
177+
10,
178+
_helpPaint
179+
..color = Colors.green
180+
..strokeWidth = 2);
181+
}
182+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'dart:io';
2+
import 'dart:typed_data';
3+
import 'dart:ui';
4+
5+
import 'package:flutter/cupertino.dart';
6+
import 'package:flutter/material.dart';
7+
import 'package:flutter/rendering.dart';
8+
import 'package:path_provider/path_provider.dart';
9+
import 'dart:ui' as ui;
10+
import 'node1_base.dart';
11+
12+
/// create by 张风捷特烈 on 2020/7/22
13+
/// contact me by email [email protected]
14+
/// 说明:
15+
// {
16+
// "widgetId": 264,
17+
// "name": "保存Widget成为图片",
18+
// "priority": 2,
19+
// "subtitle": "通过RenderRepaintBoundary可以获取子组件的Image信息,从而获取字节保存为图片文件。",
20+
// }
21+
22+
class RepaintBoundarySave extends StatelessWidget {
23+
final GlobalKey _globalKey = GlobalKey();
24+
25+
@override
26+
Widget build(BuildContext context) {
27+
return Stack(
28+
children: [
29+
RepaintBoundary(
30+
key: _globalKey,
31+
child: TempPlayBezier3Page(),
32+
),
33+
Positioned(right: -10, child: _buildButton3(context))
34+
],
35+
);
36+
}
37+
38+
Widget _buildButton3(context) => MaterialButton(
39+
child: Icon(
40+
Icons.save_alt,
41+
size: 15,
42+
color: Colors.white,
43+
),
44+
color: Colors.green,
45+
shape: CircleBorder(
46+
side: BorderSide(width: 2.0, color: Color(0xFFFFDFDFDF)),
47+
),
48+
onPressed: () async {
49+
var bits = await _widget2Image(_globalKey);
50+
var dir = await getApplicationSupportDirectory();
51+
var file = File(dir.path + "/save_img.png");
52+
var f = await file.writeAsBytes(bits);
53+
Scaffold.of(context).showSnackBar(SnackBar(
54+
backgroundColor: Theme.of(context).primaryColor,
55+
content: Text('保存成功后! 路径为:${f.path}'),
56+
));
57+
});
58+
59+
Future<Uint8List> _widget2Image(GlobalKey key) async {
60+
RenderRepaintBoundary boundary = key.currentContext.findRenderObject();
61+
//获得 ui.image
62+
ui.Image img = await boundary.toImage();
63+
//获取图片字节
64+
var byteData = await img.toByteData(format: ui.ImageByteFormat.png);
65+
Uint8List bits = byteData.buffer.asUint8List();
66+
return bits;
67+
}
68+
}

lib/views/widgets/exp/render_object_unit.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export '../SingleChildRenderObjectWidget/Align/node1_base.dart';
2929
export '../SingleChildRenderObjectWidget/Align/node2_other.dart';
3030
export '../SingleChildRenderObjectWidget/CustomSingleChildLayout/node1_base.dart';
3131
export '../SingleChildRenderObjectWidget/CustomSingleChildLayout/node2_offset.dart';
32+
export '../SingleChildRenderObjectWidget/RepaintBoundary/node1_base.dart';
33+
export '../SingleChildRenderObjectWidget/RepaintBoundary/node2_save.dart';
3234

3335
export '../SingleChildRenderObjectWidget/ConstrainedBox/node1_base.dart';
3436
export '../SingleChildRenderObjectWidget/FractionalTranslation/node1_base.dart';

lib/views/widgets/widgets_map.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ class WidgetsMap {
149149
CustomVisibility(),
150150
ReplacementVisibility(),
151151
];
152+
case "RepaintBoundary":
153+
return [
154+
RepaintBoundaryDemo(),
155+
RepaintBoundarySave(),
156+
];
152157
case "Chip":
153158
return [
154159
CustomChip(),

macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import FlutterMacOS
66
import Foundation
77

8+
import path_provider_macos
89
import shared_preferences_macos
910
import sqflite
1011
import url_launcher_macos
1112

1213
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
14+
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
1315
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
1416
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
1517
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))

macos/Podfile.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ PODS:
33
- FMDB (2.7.5):
44
- FMDB/standard (= 2.7.5)
55
- FMDB/standard (2.7.5)
6+
- path_provider (0.0.1)
7+
- path_provider_macos (0.0.1):
8+
- FlutterMacOS
69
- shared_preferences (0.0.1)
710
- shared_preferences_macos (0.0.1):
811
- FlutterMacOS
@@ -15,6 +18,8 @@ PODS:
1518

1619
DEPENDENCIES:
1720
- FlutterMacOS (from `Flutter/ephemeral/.symlinks/flutter/darwin-x64`)
21+
- path_provider (from `Flutter/ephemeral/.symlinks/plugins/path_provider/macos`)
22+
- path_provider_macos (from `Flutter/ephemeral/.symlinks/plugins/path_provider_macos/macos`)
1823
- shared_preferences (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences/macos`)
1924
- shared_preferences_macos (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences_macos/macos`)
2025
- sqflite (from `Flutter/ephemeral/.symlinks/plugins/sqflite/macos`)
@@ -28,6 +33,10 @@ SPEC REPOS:
2833
EXTERNAL SOURCES:
2934
FlutterMacOS:
3035
:path: Flutter/ephemeral/.symlinks/flutter/darwin-x64
36+
path_provider:
37+
:path: Flutter/ephemeral/.symlinks/plugins/path_provider/macos
38+
path_provider_macos:
39+
:path: Flutter/ephemeral/.symlinks/plugins/path_provider_macos/macos
3140
shared_preferences:
3241
:path: Flutter/ephemeral/.symlinks/plugins/shared_preferences/macos
3342
shared_preferences_macos:
@@ -42,6 +51,8 @@ EXTERNAL SOURCES:
4251
SPEC CHECKSUMS:
4352
FlutterMacOS: 15bea8a44d2fa024068daa0140371c020b4b6ff9
4453
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
54+
path_provider: e0848572d1d38b9a7dd099e79cf83f5b7e2cde9f
55+
path_provider_macos: a0a3fd666cb7cd0448e936fb4abad4052961002b
4556
shared_preferences: 9fec34d1bd906196a4da48fcf6c3ad521cc00b8d
4657
shared_preferences_macos: 5e5c2839894accb56b7d23328905b757f2bafaf6
4758
sqflite: 6c1f07e1d4399d619ea619fea9171251dd24d058

0 commit comments

Comments
 (0)