|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +/// LinearGradient 线性渐变封装 |
| 4 | +/// 一共有12种组合方式 |
| 5 | +/// 用法:在有gradient属性的地方使用:例如: |
| 6 | +/// 首先要导包,import 'FractionalOffsetUtil.dart'; |
| 7 | +/// 然后再去使用 |
| 8 | +/// gradient: setFractionalOffsets(Type.leftBottomToRightTop, null), |
| 9 | +/// 再例如: |
| 10 | +/// gradient: setFractionalOffsets(Type.leftToRight, Direction.firstDirection), |
| 11 | +
|
| 12 | +LinearGradient setFractionalOffsets( |
| 13 | + @required Type type, @required Direction direction) { |
| 14 | + LinearGradient linearGradient; |
| 15 | + |
| 16 | + if (type == Type.leftToRight) { |
| 17 | + linearGradient = |
| 18 | + DirectionStyle2(direction, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0); |
| 19 | + } else if (type == Type.rightToLeft) { |
| 20 | + linearGradient = |
| 21 | + DirectionStyle2(direction, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0); |
| 22 | + } else if (type == Type.topToBottom) { |
| 23 | + linearGradient = |
| 24 | + DirectionStyle2(direction, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0); |
| 25 | + } else if (type == Type.bottomToTop) { |
| 26 | + linearGradient = |
| 27 | + DirectionStyle2(direction, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0); |
| 28 | + } else if (type == Type.leftTopToRightBottom) { |
| 29 | + linearGradient = DirectionStyle(0.0, 0.0, 1.0, 1.0); |
| 30 | + } else if (type == Type.rightBottomToLeftTop) { |
| 31 | + linearGradient = DirectionStyle(1.0, 1.0, 0.0, 0.0); |
| 32 | + } else if (type == Type.rightTopToLeftBottom) { |
| 33 | + linearGradient = DirectionStyle(0.0, 1.0, 1.0, 0.0); |
| 34 | + } else if (type == Type.leftBottomToRightTop) { |
| 35 | + linearGradient = DirectionStyle(1.0, 0.0, 0.0, 1.0); |
| 36 | + } |
| 37 | + return linearGradient; |
| 38 | +} |
| 39 | + |
| 40 | +LinearGradient DirectionStyle( |
| 41 | + double startX, double startY, double endX, double endY) { |
| 42 | + var linearGradient = new LinearGradient( |
| 43 | + //线性渐变 |
| 44 | + begin: FractionalOffset(startX, startY), |
| 45 | + end: FractionalOffset(endX, endY), |
| 46 | + colors: <Color>[Colors.deepOrange, Colors.deepPurple], |
| 47 | + ); |
| 48 | + return linearGradient; |
| 49 | +} |
| 50 | + |
| 51 | +LinearGradient DirectionStyle2( |
| 52 | + @required Direction direction, |
| 53 | + double startX, |
| 54 | + double startY, |
| 55 | + double endX, |
| 56 | + double endY, |
| 57 | + double startX2, |
| 58 | + double startY2, |
| 59 | + double endX2, |
| 60 | + double endY2) { |
| 61 | + direction == Direction.firstDirection ? startX = startX : startX2; |
| 62 | + direction == Direction.firstDirection ? startY = startY : startY2; |
| 63 | + direction == Direction.firstDirection ? endX = endX : endX2; |
| 64 | + direction == Direction.firstDirection ? endY = endY : endY2; |
| 65 | + |
| 66 | + var linearGradient = new LinearGradient( |
| 67 | + //线性渐变 |
| 68 | + begin: FractionalOffset(startX, startY), |
| 69 | + end: FractionalOffset(endX, endY), |
| 70 | + colors: <Color>[Colors.deepOrange, Colors.deepPurple], |
| 71 | + ); |
| 72 | + return linearGradient; |
| 73 | +} |
| 74 | + |
| 75 | +// 因为从上到下,或者从左到右 都有两种可能。 |
| 76 | +// 从上到下 包括: 左上 -> 左下 右上 -> 右下 |
| 77 | +// 从左到右 包括: 左上 -> 右上 左下 -> 右下 |
| 78 | +enum Direction { |
| 79 | + firstDirection,//如果是从上到下: 左上 -> 左下,如果是从左到右:左上 -> 右上 |
| 80 | + lastPointDirection,// 如果是从上到下: 右上 -> 右下,如果是从左到右:左下 -> 右下 |
| 81 | +} |
| 82 | + |
| 83 | +enum Type { |
| 84 | + //8个方向 |
| 85 | + leftToRight, //→ |
| 86 | + rightToLeft, //← |
| 87 | + topToBottom, //↓ |
| 88 | + bottomToTop, //↑ |
| 89 | + leftTopToRightBottom, //↘ |
| 90 | + rightBottomToLeftTop, //↖ |
| 91 | + rightTopToLeftBottom, //↙ |
| 92 | + leftBottomToRightTop, //↗ |
| 93 | +} |
0 commit comments