Skip to content

Commit

Permalink
✨ 添加random portrait 绘制
Browse files Browse the repository at this point in the history
  • Loading branch information
toly1994328 committed Oct 10, 2020
1 parent 239b8f7 commit b837084
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 68 deletions.
20 changes: 15 additions & 5 deletions lib/views/pages/gallery/art/circle_packing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ class Circle {
Color color;
}

class CirclePacking extends StatelessWidget {
class CirclePacking extends StatefulWidget {
@override
_CirclePackingState createState() => _CirclePackingState();
}

class _CirclePackingState extends State<CirclePacking> {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: CirclePackingPainter(),
return GestureDetector(
onTap: (){
setState(() {

});
},
child: CustomPaint(
painter: CirclePackingPainter(),
),
);
}
}
Expand Down Expand Up @@ -109,6 +121,4 @@ class CirclePackingPainter extends CustomPainter {
@override
bool shouldRepaint(CirclePackingPainter oldDelegate) => true;

@override
bool shouldRebuildSemantics(CirclePackingPainter oldDelegate) => false;
}
22 changes: 16 additions & 6 deletions lib/views/pages/gallery/art/cubic_disarray.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ import 'dart:math';

import 'package:flutter/widgets.dart';

class CubicDisarray extends StatelessWidget {
class CubicDisarray extends StatefulWidget {
@override
_CubicDisarrayState createState() => _CubicDisarrayState();
}

class _CubicDisarrayState extends State<CubicDisarray> {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: CubicDisarrayPainter(),
return GestureDetector(
onTap: (){
setState(() {

});
},
child: CustomPaint(
painter: CubicDisarrayPainter(),
),
);
}
}
Expand Down Expand Up @@ -61,8 +73,6 @@ class CubicDisarrayPainter extends CustomPainter {
}

@override
bool shouldRepaint(CubicDisarrayPainter oldDelegate) => false;
bool shouldRepaint(CubicDisarrayPainter oldDelegate) => true;

@override
bool shouldRebuildSemantics(CubicDisarrayPainter oldDelegate) => false;
}
23 changes: 16 additions & 7 deletions lib/views/pages/gallery/art/hypnotic_squares.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ import 'package:flutter/material.dart';

import '../colors.dart';

class HypnoticSquares extends StatelessWidget {
class HypnoticSquares extends StatefulWidget {
@override
_HypnoticSquaresState createState() => _HypnoticSquaresState();
}

class _HypnoticSquaresState extends State<HypnoticSquares> {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: HypnoticSquaresPainter(),
return GestureDetector(
onTap: (){
setState(() {

});
},
child: CustomPaint(
painter: HypnoticSquaresPainter(),
),
);
}
}
Expand Down Expand Up @@ -85,8 +97,5 @@ class HypnoticSquaresPainter extends CustomPainter {
}

@override
bool shouldRepaint(HypnoticSquaresPainter oldDelegate) => false;

@override
bool shouldRebuildSemantics(HypnoticSquaresPainter oldDelegate) => false;
bool shouldRepaint(HypnoticSquaresPainter oldDelegate) => true;
}
24 changes: 17 additions & 7 deletions lib/views/pages/gallery/art/joy_division.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ import 'dart:ui';

import 'package:flutter/material.dart';

class JoyDivision extends StatelessWidget {
class JoyDivision extends StatefulWidget {
@override
_JoyDivisionState createState() => _JoyDivisionState();
}

class _JoyDivisionState extends State<JoyDivision> {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: JoyDivisionPainter(),
return GestureDetector(
onTap: (){
setState(() {

});
},
child: CustomPaint(
painter: JoyDivisionPainter(),
),
);
}
}
Expand Down Expand Up @@ -55,8 +67,6 @@ class JoyDivisionPainter extends CustomPainter {
}

@override
bool shouldRepaint(JoyDivisionPainter oldDelegate) => false;

@override
bool shouldRebuildSemantics(JoyDivisionPainter oldDelegate) => false;
bool shouldRepaint(JoyDivisionPainter oldDelegate) => true;

}
15 changes: 10 additions & 5 deletions lib/views/pages/gallery/art/piet_mondrian.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ class PietMondrian extends StatefulWidget {
class _PietMondrianState extends State<PietMondrian> {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: PietMondrianPainnter(),
return GestureDetector(
onTap: (){
setState(() {

});
},
child: CustomPaint(
painter: PietMondrianPainnter(),
),
);
}
}
Expand Down Expand Up @@ -164,8 +171,6 @@ class PietMondrianPainnter extends CustomPainter {
}

@override
bool shouldRepaint(PietMondrianPainnter oldDelegate) => false;
bool shouldRepaint(PietMondrianPainnter oldDelegate) => true;

@override
bool shouldRebuildSemantics(PietMondrianPainnter oldDelegate) => false;
}
97 changes: 97 additions & 0 deletions lib/views/pages/gallery/art/random_portrait.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

/// create by 张风捷特烈 on 2020/10/10
/// contact me by email [email protected]
/// 说明:
class Position {
final int x;
final int y;

Position(this.x, this.y);

@override
String toString() {
return 'Position{x: $x, y: $y}';
}
}

class RandomPortrait extends StatefulWidget {
@override
_RandomPortraitState createState() => _RandomPortraitState();
}

class _RandomPortraitState extends State<RandomPortrait> {
List<Position> positions = [];
Random random = Random();
final int blockCount = 9;

@override
Widget build(BuildContext context) {
_initPosition();
return GestureDetector(
onTap: () {
setState(() {});
},
child: CustomPaint(painter: PaperPainter(positions)));
}

void _initPosition() async {
positions.clear();
int randomCount = 2 + random.nextInt(blockCount * blockCount ~/ 2 - 2);

var flag = blockCount ~/ 2 + 1;

for (int i = 0; i < randomCount; i++) {
int randomX = random.nextInt(flag);
int randomY = random.nextInt(blockCount);
var position = Position(randomX, randomY);
positions.add(position);
}
for (int i = 0; i < positions.length; i++) {
if (positions[i].x < blockCount ~/ 2) {
positions
.add(Position(2 * flag - (positions[i].x + 1) - 1, positions[i].y));
}
}
}
}

class PaperPainter extends CustomPainter {
Paint _paint;

final double strokeWidth = 0.5;
final Color color = Colors.blue;
final int blockCount = 9;

final List<Position> positions;

PaperPainter(this.positions) : _paint = Paint()..color = Colors.blue;

@override
void paint(Canvas canvas, Size size) {
canvas.clipRect(
Rect.fromPoints(Offset.zero, Offset(size.width, size.height)));

var pd = 20.0;

var dW = (size.width-pd*2) / (blockCount);
var dH = (size.height-pd*2) / (blockCount);
canvas.translate(pd, pd);
positions.forEach((element) {
_drawBlock(dW.floor(), dH.floor(), canvas, element);
});
}

void _drawBlock(int dW, int dH, Canvas canvas, Position position) {
canvas.drawRect(
Rect.fromLTWH(position.x * dW*1.0, position.y * dH*1.0, dW*1.0, dH*1.0), _paint);
}

@override
bool shouldRepaint(PaperPainter oldDelegate) => true;
}
20 changes: 16 additions & 4 deletions lib/views/pages/gallery/art/tiled_lines.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import 'dart:math';
import 'package:flutter/material.dart';

class TiledLines extends StatelessWidget {
class TiledLines extends StatefulWidget {
@override
_TiledLinesState createState() => _TiledLinesState();
}

class _TiledLinesState extends State<TiledLines> {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: TiledLinesPainter(20),
return GestureDetector(
onTap: (){
setState(() {

});
},
child: CustomPaint(
painter: TiledLinesPainter(20),
),
);
}
}
Expand Down Expand Up @@ -52,5 +64,5 @@ class TiledLinesPainter extends CustomPainter {
}
}

bool shouldRepaint(TiledLinesPainter oldDelegate) => false;
bool shouldRepaint(TiledLinesPainter oldDelegate) => true;
}
20 changes: 16 additions & 4 deletions lib/views/pages/gallery/art/triangular_mesh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@ class Point {
double y;
}

class TriangularMesh extends StatelessWidget {
class TriangularMesh extends StatefulWidget {
@override
_TriangularMeshState createState() => _TriangularMeshState();
}

class _TriangularMeshState extends State<TriangularMesh> {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: TriangularMeshPainter(),
return GestureDetector(
onTap: (){
setState(() {

});
},
child: CustomPaint(
painter: TriangularMeshPainter(),
),
);
}
}
Expand Down Expand Up @@ -82,5 +94,5 @@ class TriangularMeshPainter extends CustomPainter {
}

@override
bool shouldRepaint(TriangularMeshPainter oldDelegate) => false;
bool shouldRepaint(TriangularMeshPainter oldDelegate) => true;
}
22 changes: 16 additions & 6 deletions lib/views/pages/gallery/art/un_deux_trois.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ import 'dart:math';

import 'package:flutter/material.dart';

class UnDeuxTrois extends StatelessWidget {
class UnDeuxTrois extends StatefulWidget {
@override
_UnDeuxTroisState createState() => _UnDeuxTroisState();
}

class _UnDeuxTroisState extends State<UnDeuxTrois> {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: UnDeuxTroisPainter(),
return GestureDetector(
onTap: (){
setState(() {

});
},
child: CustomPaint(
painter: UnDeuxTroisPainter(),
),
);
}
}
Expand Down Expand Up @@ -59,8 +71,6 @@ class UnDeuxTroisPainter extends CustomPainter {
}

@override
bool shouldRepaint(UnDeuxTroisPainter oldDelegate) => false;
bool shouldRepaint(UnDeuxTroisPainter oldDelegate) => true;

@override
bool shouldRebuildSemantics(UnDeuxTroisPainter oldDelegate) => false;
}
Loading

0 comments on commit b837084

Please sign in to comment.