|  | 
|  | 1 | +/// | 
|  | 2 | +/// Created by NieBin on 2019年6月15日 | 
|  | 3 | +/// Github: https://github.com/nb312 | 
|  | 4 | +/// Email: niebin312@gmail.com | 
|  | 5 | +/// | 
|  | 6 | +import "package:flutter/material.dart"; | 
|  | 7 | +import 'package:flutter_widgets/const/_const.dart'; | 
|  | 8 | +import 'dart:math'; | 
|  | 9 | + | 
|  | 10 | +class PaintingPage extends StatefulWidget { | 
|  | 11 | + @override | 
|  | 12 | + _OpacityState createState() => _OpacityState(); | 
|  | 13 | +} | 
|  | 14 | + | 
|  | 15 | +class _OpacityState extends State<PaintingPage> { | 
|  | 16 | + Widget _opacity() => Container( | 
|  | 17 | + constraints: BoxConstraints.expand(height: 100), | 
|  | 18 | + child: Opacity( | 
|  | 19 | + opacity: 0.7, | 
|  | 20 | + child: Text( | 
|  | 21 | + "Opacity", | 
|  | 22 | + style: TextStyle(color: TEXT_BLACK), | 
|  | 23 | + ), | 
|  | 24 | + ), | 
|  | 25 | + color: RED_LIGHT, | 
|  | 26 | + ); | 
|  | 27 | + | 
|  | 28 | + Widget _transform() => Container( | 
|  | 29 | + constraints: BoxConstraints.expand(height: 100), | 
|  | 30 | + child: Transform.rotate( | 
|  | 31 | + angle: pi / 2, | 
|  | 32 | + child: Center( | 
|  | 33 | + child: Text("Transform"), | 
|  | 34 | + ), | 
|  | 35 | + ), | 
|  | 36 | + color: BLUE_LIGHT, | 
|  | 37 | + ); | 
|  | 38 | + | 
|  | 39 | + Widget _decorated() => Container( | 
|  | 40 | + constraints: BoxConstraints.expand(height: 100), | 
|  | 41 | + child: DecoratedBox( | 
|  | 42 | + position: DecorationPosition.foreground, | 
|  | 43 | + decoration: ShapeDecoration( | 
|  | 44 | + shape: StadiumBorder( | 
|  | 45 | + side: BorderSide(color: TEXT_BLACK, width: 10), | 
|  | 46 | + ), | 
|  | 47 | + ), | 
|  | 48 | + child: Container( | 
|  | 49 | + color: PURPLE, | 
|  | 50 | + child: Center( | 
|  | 51 | + child: Text("DecoratedBox"), | 
|  | 52 | + ), | 
|  | 53 | + ))); | 
|  | 54 | + | 
|  | 55 | + Widget _fraction() => Container( | 
|  | 56 | + constraints: BoxConstraints.expand(height: 100), | 
|  | 57 | + child: FractionalTranslation( | 
|  | 58 | + translation: Offset(0.3, 0.1), | 
|  | 59 | + child: Container( | 
|  | 60 | + color: BLUE_LIGHT, | 
|  | 61 | + ), | 
|  | 62 | + ), | 
|  | 63 | + ); | 
|  | 64 | + | 
|  | 65 | + Widget _rotation() => Container( | 
|  | 66 | + constraints: BoxConstraints.expand(height: 100), | 
|  | 67 | + child: RotatedBox( | 
|  | 68 | + quarterTurns: 3, | 
|  | 69 | + child: Container( | 
|  | 70 | + color: GREEN, | 
|  | 71 | + child: Center( | 
|  | 72 | + child: Text("RotatedBox"), | 
|  | 73 | + ), | 
|  | 74 | + ), | 
|  | 75 | + ), | 
|  | 76 | + ); | 
|  | 77 | + | 
|  | 78 | + Widget _clipOval() => Container( | 
|  | 79 | + constraints: BoxConstraints.expand(height: 100), | 
|  | 80 | + child: ClipOval( | 
|  | 81 | + clipper: MyClipper(), | 
|  | 82 | + child: Container( | 
|  | 83 | + color: RED_LIGHT, | 
|  | 84 | + ), | 
|  | 85 | + ), | 
|  | 86 | + ); | 
|  | 87 | + | 
|  | 88 | + Widget _clipPath() => Container( | 
|  | 89 | + constraints: BoxConstraints.expand(height: 100), | 
|  | 90 | + child: ClipPath( | 
|  | 91 | + clipper: MPathClipper(), | 
|  | 92 | + child: Container(color: PURPLE), | 
|  | 93 | + ), | 
|  | 94 | + ); | 
|  | 95 | + | 
|  | 96 | + Widget _clipRect() => Container( | 
|  | 97 | + constraints: BoxConstraints.expand(height: 100), | 
|  | 98 | + child: ClipRect( | 
|  | 99 | + clipper: MyClipper(), | 
|  | 100 | + child: Container( | 
|  | 101 | + color: BLUE_DEEP, | 
|  | 102 | + ), | 
|  | 103 | + ), | 
|  | 104 | + ); | 
|  | 105 | + | 
|  | 106 | + Widget _custom() => Container( | 
|  | 107 | + constraints: BoxConstraints.expand(height: 200), | 
|  | 108 | + child: CustomPaint( | 
|  | 109 | + painter: MCustomPainter(), | 
|  | 110 | + child: Center( | 
|  | 111 | + child: Text("CustomPaint"), | 
|  | 112 | + ), | 
|  | 113 | + ), | 
|  | 114 | + ); | 
|  | 115 | + | 
|  | 116 | + @override | 
|  | 117 | + Widget build(BuildContext context) { | 
|  | 118 | + return Scaffold( | 
|  | 119 | + appBar: AppBar( | 
|  | 120 | + title: Text("Hello world"), | 
|  | 121 | + ), | 
|  | 122 | + body: SingleChildScrollView( | 
|  | 123 | + child: Column( | 
|  | 124 | + children: <Widget>[ | 
|  | 125 | + _opacity(), | 
|  | 126 | + _transform(), | 
|  | 127 | + _decorated(), | 
|  | 128 | + _fraction(), | 
|  | 129 | + _rotation(), | 
|  | 130 | + _clipOval(), | 
|  | 131 | + _clipPath(), | 
|  | 132 | + _clipRect(), | 
|  | 133 | + _custom(), | 
|  | 134 | + ], | 
|  | 135 | + ), | 
|  | 136 | + ), | 
|  | 137 | + ); | 
|  | 138 | + } | 
|  | 139 | +} | 
|  | 140 | + | 
|  | 141 | +class MCustomPainter extends CustomPainter { | 
|  | 142 | + @override | 
|  | 143 | + void paint(Canvas canvas, Size size) { | 
|  | 144 | + canvas.drawCircle( | 
|  | 145 | + Offset(size.width / 2, size.height / 2), | 
|  | 146 | + 100, | 
|  | 147 | + Paint() | 
|  | 148 | + ..color = RED_LIGHT | 
|  | 149 | + ..isAntiAlias = true | 
|  | 150 | + ..strokeWidth = 10); | 
|  | 151 | + } | 
|  | 152 | + | 
|  | 153 | + @override | 
|  | 154 | + bool shouldRepaint(CustomPainter oldDelegate) { | 
|  | 155 | + return true; | 
|  | 156 | + } | 
|  | 157 | +} | 
|  | 158 | + | 
|  | 159 | +class MPathClipper extends CustomClipper<Path> { | 
|  | 160 | + @override | 
|  | 161 | + Path getClip(Size size) { | 
|  | 162 | + return Path() | 
|  | 163 | + ..moveTo(100.0, 50.0) | 
|  | 164 | + ..addArc( | 
|  | 165 | + Rect.fromCenter( | 
|  | 166 | + center: Offset(100.0, 50.0), height: 50.0, width: 50.0), | 
|  | 167 | + 0, | 
|  | 168 | + pi / 2); | 
|  | 169 | + } | 
|  | 170 | + | 
|  | 171 | + @override | 
|  | 172 | + bool shouldReclip(CustomClipper<Path> oldClipper) { | 
|  | 173 | + return true; | 
|  | 174 | + } | 
|  | 175 | +} | 
|  | 176 | + | 
|  | 177 | +class MyClipper extends CustomClipper<Rect> { | 
|  | 178 | + @override | 
|  | 179 | + Rect getClip(Size size) { | 
|  | 180 | + return Rect.fromCenter( | 
|  | 181 | + center: Offset(50.0, 50.0), height: 100.0, width: 50.0); | 
|  | 182 | + } | 
|  | 183 | + | 
|  | 184 | + @override | 
|  | 185 | + bool shouldReclip(CustomClipper<Rect> oldClipper) { | 
|  | 186 | + return true; | 
|  | 187 | + } | 
|  | 188 | +} | 
0 commit comments