layout.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import 'package:flutter/material.dart';
  2. extension LayoutX<T extends Widget> on T {
  3. //
  4. // SizedBox wrapContent() {
  5. // return SizedBox.shrink(child: this);
  6. // }
  7. //
  8. // SizedBox matchParent() {
  9. // return SizedBox.expand(child: this);
  10. // }
  11. // 布局
  12. Padding paddingAll(double size) {
  13. return padding(left: size, right: size, top: size, bottom: size);
  14. }
  15. Padding get paddingZero => padding();
  16. Padding paddingSymmetric({double horizontal = 0, double vertical = 0}) {
  17. return padding(left: horizontal, right: horizontal, top: vertical, bottom: vertical);
  18. }
  19. Padding paddingLeft(double size) {
  20. return padding(left: size);
  21. }
  22. Padding paddingRight(double size) {
  23. return padding(right: size);
  24. }
  25. Padding paddingTop(double size) {
  26. return padding(top: size);
  27. }
  28. Padding paddingBottom(double size) {
  29. return padding(bottom: size);
  30. }
  31. Padding padding({double left = 0, double top = 0, double right = 0, double bottom = 0}) {
  32. return Padding(
  33. padding: EdgeInsets.only(left: left, top: top, right: right, bottom: bottom),
  34. child: this,
  35. );
  36. }
  37. //______margin_______
  38. Container marginAll(double size) {
  39. return margin(left: size, right: size, top: size, bottom: size);
  40. }
  41. Container get marginZero => margin();
  42. Container marginSymmetric({double horizontal = 0, double vertical = 0}) {
  43. return margin(left: horizontal, right: horizontal, top: vertical, bottom: vertical);
  44. }
  45. Container marginLeft(double size) {
  46. return margin(left: size);
  47. }
  48. Container marginRight(double size) {
  49. return margin(right: size);
  50. }
  51. Container marginTop(double size) {
  52. return margin(top: size);
  53. }
  54. Container marginBottom(double size) {
  55. return margin(bottom: size);
  56. }
  57. Container margin({double left = 0, double top = 0, double right = 0, double bottom = 0}) {
  58. return Container(
  59. margin: EdgeInsets.only(left: left, top: top, right: right, bottom: bottom),
  60. child: this,
  61. );
  62. }
  63. //transform 变换
  64. Transform translate(double dx, double dy) {
  65. return Transform.translate(
  66. offset: Offset(dx, dy),
  67. child: this,
  68. );
  69. }
  70. Transform rotate(double angle) {
  71. return Transform.rotate(
  72. angle: angle,
  73. child: this,
  74. );
  75. }
  76. Transform scale(double scale) {
  77. return Transform.scale(
  78. scale: scale,
  79. child: this,
  80. );
  81. }
  82. //Flex
  83. Flexible flexible({
  84. int flex = 1,
  85. FlexFit fit = FlexFit.loose,
  86. }) {
  87. return Flexible(flex: flex, fit: fit, child: this);
  88. }
  89. Expanded expanded() {
  90. return Expanded(child: this);
  91. }
  92. Positioned positioned(
  93. {double? left, double? right, double? top, double? bottom, double? width, double? height}) {
  94. return Positioned(
  95. child: this,
  96. left: left,
  97. right: right,
  98. top: top,
  99. bottom: bottom,
  100. width: width,
  101. height: height,
  102. );
  103. }
  104. //套入一个方形的盒子
  105. // SizedBox square(double size) {
  106. // return SizedBox.square(child: this, dimension: size);
  107. // }
  108. //尺寸相关
  109. SizedBox height(double? height) {
  110. return SizedBox(height: height, child: this);
  111. }
  112. SizedBox width(double? width) {
  113. return SizedBox(width: width, child: this);
  114. }
  115. ConstrainedBox maxWidth(double? size) {
  116. return constraints(maxWidth: size);
  117. }
  118. ConstrainedBox maxHeight(double? size) {
  119. return constraints(maxHeight: size);
  120. }
  121. ConstrainedBox minWidth(double? size) {
  122. return constraints(minWidth: size);
  123. }
  124. ConstrainedBox minHeight(double? size) {
  125. return constraints(minHeight: size);
  126. }
  127. SizedBox size({double? width, double? height}) {
  128. return SizedBox(width: width, height: height, child: this);
  129. }
  130. ///aspect ratio 纵横比
  131. AspectRatio aspectRatio(double aspectRatio) {
  132. return AspectRatio(
  133. child: this,
  134. aspectRatio: aspectRatio,
  135. );
  136. }
  137. ConstrainedBox constraints(
  138. {double? maxWidth, double? maxHeight, double? minWidth, double? minHeight}) {
  139. BoxConstraints constraints = BoxConstraints(
  140. maxWidth: maxWidth ?? double.infinity,
  141. maxHeight: maxHeight ?? double.infinity,
  142. minWidth: minWidth ?? 0,
  143. minHeight: minHeight ?? 0);
  144. if (this is ConstrainedBox) {
  145. var thisConstraints = (this as ConstrainedBox).constraints;
  146. constraints =
  147. maxWidth?.let((it) => thisConstraints.copyWith(maxWidth: maxWidth)) ?? thisConstraints;
  148. constraints =
  149. maxHeight?.let((it) => thisConstraints.copyWith(maxWidth: maxWidth)) ?? thisConstraints;
  150. constraints =
  151. minWidth?.let((it) => thisConstraints.copyWith(maxWidth: maxWidth)) ?? thisConstraints;
  152. constraints =
  153. minHeight?.let((it) => thisConstraints.copyWith(maxWidth: maxWidth)) ?? thisConstraints;
  154. }
  155. return ConstrainedBox(constraints: constraints, child: this);
  156. }
  157. }
  158. extension ScopeEx<T> on T {
  159. T also(Function(T it) function) {
  160. function(this);
  161. return this;
  162. }
  163. R let<R>(R Function(T it) function) {
  164. return function(this);
  165. }
  166. }