Skip to content

Commit 54f3939

Browse files
committed
comply the canvas
1 parent c5a0644 commit 54f3939

10 files changed

+161
-84
lines changed

doc/canvas_change.png

76.1 KB
Loading

doc/circle_note.png

24.6 KB
Loading

doc/flutter_note.png

56.3 KB
Loading

doc/logo.png

50.6 KB
Loading

doc/result_200_400.png

56.1 KB
Loading

doc/result_280_320.png

56.1 KB
Loading

doc/result_full_screen.png

59.8 KB
Loading

lib/OpenPainter.dart

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
///
2+
/// Created by NieBin on 2019/1/1
3+
/// Github: https://github.com/nb312
4+
5+
///
6+
7+
import "package:flutter/material.dart";
8+
import 'SizeUtil.dart';
9+
import 'dart:math';
10+
11+
const BLUE_NORMAL = Color(0xff54c5f8);
12+
const GREEN_NORMAL = Color(0xff6bde54);
13+
const BLUE_DARK2 = Color(0xff01579b);
14+
const BLUE_DARK1 = Color(0xff29b6f6);
15+
16+
class OpenPainter extends CustomPainter {
17+
void _drawFourShape(Canvas canvas,
18+
{Offset left_top,
19+
Offset right_top,
20+
Offset right_bottom,
21+
Offset left_bottom,
22+
Size size,
23+
paint}) {
24+
left_top = _convertLogicSize(left_top, size);
25+
right_top = _convertLogicSize(right_top, size);
26+
right_bottom = _convertLogicSize(right_bottom, size);
27+
left_bottom = _convertLogicSize(left_bottom, size);
28+
var path1 = Path()
29+
..moveTo(left_top.dx, left_top.dy)
30+
..lineTo(right_top.dx, right_top.dy)
31+
..lineTo(right_bottom.dx, right_bottom.dy)
32+
..lineTo(left_bottom.dx, left_bottom.dy);
33+
canvas.drawPath(path1, paint);
34+
}
35+
36+
Offset _convertLogicSize(Offset off, Size size) {
37+
return Offset(SizeUtil.getAxisX(off.dx), SizeUtil.getAxisY(off.dy));
38+
}
39+
40+
@override
41+
void paint(Canvas canvas, Size size) {
42+
//580*648
43+
if (size.width > 1.0 && size.height > 1.0) {
44+
print(">1.9");
45+
SizeUtil.size = size;
46+
}
47+
var paint = Paint()
48+
..style = PaintingStyle.fill
49+
..color = BLUE_NORMAL
50+
..isAntiAlias = true;
51+
_drawFourShape(canvas,
52+
left_top: Offset(291, 178),
53+
right_top: Offset(580, 458),
54+
right_bottom: Offset(580, 628),
55+
left_bottom: Offset(203, 267),
56+
size: size,
57+
paint: paint);
58+
_drawFourShape(canvas,
59+
left_top: Offset(156, 314),
60+
right_top: Offset(312, 468),
61+
right_bottom: Offset(312, 645),
62+
left_bottom: Offset(70, 402),
63+
size: size,
64+
paint: paint);
65+
paint.color = BLUE_DARK2;
66+
_drawFourShape(canvas,
67+
left_top: Offset(156, 314),
68+
right_top: Offset(245, 402),
69+
right_bottom: Offset(4, 643),
70+
left_bottom: Offset(4, 467),
71+
size: size,
72+
paint: paint);
73+
paint.color = BLUE_DARK1;
74+
_drawFourShape(canvas,
75+
left_top: Offset(156, 314),
76+
right_top: Offset(245, 402),
77+
right_bottom: Offset(157, 490),
78+
left_bottom: Offset(70, 402),
79+
size: size,
80+
paint: paint);
81+
var circleCenter = Offset(SizeUtil.getAxisX(294), SizeUtil.getAxisY(175));
82+
paint.color = BLUE_NORMAL;
83+
canvas.drawCircle(circleCenter, SizeUtil.getAxisBoth(174), paint);
84+
paint.color = GREEN_NORMAL;
85+
canvas.drawCircle(circleCenter, SizeUtil.getAxisBoth(124), paint);
86+
paint.color = Colors.white;
87+
canvas.drawCircle(circleCenter, SizeUtil.getAxisBoth(80), paint);
88+
canvas.save();
89+
canvas.restore();
90+
91+
// _drawFourShape(canvas, left_top: Offset(291, 178));
92+
// canvas.drawPath(path1, paint);
93+
}
94+
95+
@override
96+
bool shouldRepaint(CustomPainter oldDelegate) => false;
97+
}

lib/SizeUtil.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
///
2+
/// Created by NieBin on 2018/12/26
3+
/// Github: https://github.com/nb312
4+
5+
///
6+
import 'package:flutter/material.dart';
7+
import 'dart:math';
8+
9+
class SizeUtil {
10+
static const _DESIGN_WIDTH = 580;
11+
static const _DESIGN_HEIGHT = 648;
12+
13+
//logic size in device
14+
static Size _logicSize;
15+
16+
//device pixel radio.
17+
18+
static get width {
19+
return _logicSize.width;
20+
}
21+
22+
static get height {
23+
return _logicSize.height;
24+
}
25+
26+
static set size(size) {
27+
_logicSize = size;
28+
}
29+
30+
//@param w is the design w;
31+
static double getAxisX(double w) {
32+
return (w * width) / _DESIGN_WIDTH;
33+
}
34+
35+
// the y direction
36+
static double getAxisY(double h) {
37+
return (h * height) / _DESIGN_HEIGHT;
38+
}
39+
40+
// diagonal direction value with design size s.
41+
static double getAxisBoth(double s) {
42+
return s *
43+
sqrt((width * width + height * height) /
44+
(_DESIGN_WIDTH * _DESIGN_WIDTH + _DESIGN_HEIGHT * _DESIGN_HEIGHT));
45+
}
46+
}

lib/main.dart

Lines changed: 18 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,45 @@
11
import 'package:flutter/material.dart';
2+
import 'OpenPainter.dart';
3+
import 'package:flutter_canvas/SizeUtil.dart';
24

35
void main() => runApp(MyApp());
46

57
class MyApp extends StatelessWidget {
6-
// This widget is the root of your application.
78
@override
89
Widget build(BuildContext context) {
910
return MaterialApp(
10-
title: 'Flutter Demo',
11+
title: 'Flutter Canvas',
1112
theme: ThemeData(
12-
// This is the theme of your application.
13-
//
14-
// Try running your application with "flutter run". You'll see the
15-
// application has a blue toolbar. Then, without quitting the app, try
16-
// changing the primarySwatch below to Colors.green and then invoke
17-
// "hot reload" (press "r" in the console where you ran "flutter run",
18-
// or simply save your changes to "hot reload" in a Flutter IDE).
19-
// Notice that the counter didn't reset back to zero; the application
20-
// is not restarted.
2113
primarySwatch: Colors.blue,
2214
),
23-
home: MyHomePage(title: 'Flutter Demo Home Page'),
15+
home: HomePage(),
2416
);
2517
}
2618
}
2719

28-
class MyHomePage extends StatefulWidget {
29-
MyHomePage({Key key, this.title}) : super(key: key);
30-
31-
// This widget is the home page of your application. It is stateful, meaning
32-
// that it has a State object (defined below) that contains fields that affect
33-
// how it looks.
34-
35-
// This class is the configuration for the state. It holds the values (in this
36-
// case the title) provided by the parent (in this case the App widget) and
37-
// used by the build method of the State. Fields in a Widget subclass are
38-
// always marked "final".
39-
40-
final String title;
41-
20+
class HomePage extends StatefulWidget {
4221
@override
43-
_MyHomePageState createState() => _MyHomePageState();
22+
_HomeState createState() => _HomeState();
4423
}
4524

46-
class _MyHomePageState extends State<MyHomePage> {
47-
int _counter = 0;
48-
49-
void _incrementCounter() {
50-
setState(() {
51-
// This call to setState tells the Flutter framework that something has
52-
// changed in this State, which causes it to rerun the build method below
53-
// so that the display can reflect the updated values. If we changed
54-
// _counter without calling setState(), then the build method would not be
55-
// called again, and so nothing would appear to happen.
56-
_counter++;
57-
});
58-
}
59-
25+
class _HomeState extends State<HomePage> {
6026
@override
6127
Widget build(BuildContext context) {
62-
// This method is rerun every time setState is called, for instance as done
63-
// by the _incrementCounter method above.
64-
//
65-
// The Flutter framework has been optimized to make rerunning build methods
66-
// fast, so that you can just rebuild anything that needs updating rather
67-
// than having to individually change instances of widgets.
28+
SizeUtil.size = MediaQuery.of(context).size;
6829
return Scaffold(
6930
appBar: AppBar(
70-
// Here we take the value from the MyHomePage object that was created by
71-
// the App.build method, and use it to set our appbar title.
72-
title: Text(widget.title),
31+
title: Text("First Canvas"),
7332
),
74-
body: Center(
75-
// Center is a layout widget. It takes a single child and positions it
76-
// in the middle of the parent.
77-
child: Column(
78-
// Column is also layout widget. It takes a list of children and
79-
// arranges them vertically. By default, it sizes itself to fit its
80-
// children horizontally, and tries to be as tall as its parent.
81-
//
82-
// Invoke "debug painting" (press "p" in the console, choose the
83-
// "Toggle Debug Paint" action from the Flutter Inspector in Android
84-
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
85-
// to see the wireframe for each widget.
86-
//
87-
// Column has various properties to control how it sizes itself and
88-
// how it positions its children. Here we use mainAxisAlignment to
89-
// center the children vertically; the main axis here is the vertical
90-
// axis because Columns are vertical (the cross axis would be
91-
// horizontal).
92-
mainAxisAlignment: MainAxisAlignment.center,
93-
children: <Widget>[
94-
Text(
95-
'You have pushed the button this many times:',
96-
),
97-
Text(
98-
'$_counter',
99-
style: Theme.of(context).textTheme.display1,
100-
),
101-
],
33+
body: Container(
34+
child: Center(
35+
child: Container(
36+
width: 280,
37+
height: 320.0,
38+
child: CustomPaint(
39+
painter: OpenPainter(),
40+
),
10241
),
103-
),
104-
floatingActionButton: FloatingActionButton(
105-
onPressed: _incrementCounter,
106-
tooltip: 'Increment',
107-
child: Icon(Icons.add),
108-
), // This trailing comma makes auto-formatting nicer for build methods.
42+
)),
10943
);
11044
}
11145
}

0 commit comments

Comments
 (0)