| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- import 'package:flutter/material.dart';
- extension LayoutX<T extends Widget> on T {
- //
- // SizedBox wrapContent() {
- // return SizedBox.shrink(child: this);
- // }
- //
- // SizedBox matchParent() {
- // return SizedBox.expand(child: this);
- // }
- // 布局
- Padding paddingAll(double size) {
- return padding(left: size, right: size, top: size, bottom: size);
- }
- Padding get paddingZero => padding();
- Padding paddingSymmetric({double horizontal = 0, double vertical = 0}) {
- return padding(left: horizontal, right: horizontal, top: vertical, bottom: vertical);
- }
- Padding paddingLeft(double size) {
- return padding(left: size);
- }
- Padding paddingRight(double size) {
- return padding(right: size);
- }
- Padding paddingTop(double size) {
- return padding(top: size);
- }
- Padding paddingBottom(double size) {
- return padding(bottom: size);
- }
- Padding padding({double left = 0, double top = 0, double right = 0, double bottom = 0}) {
- return Padding(
- padding: EdgeInsets.only(left: left, top: top, right: right, bottom: bottom),
- child: this,
- );
- }
- //______margin_______
- Container marginAll(double size) {
- return margin(left: size, right: size, top: size, bottom: size);
- }
- Container get marginZero => margin();
- Container marginSymmetric({double horizontal = 0, double vertical = 0}) {
- return margin(left: horizontal, right: horizontal, top: vertical, bottom: vertical);
- }
- Container marginLeft(double size) {
- return margin(left: size);
- }
- Container marginRight(double size) {
- return margin(right: size);
- }
- Container marginTop(double size) {
- return margin(top: size);
- }
- Container marginBottom(double size) {
- return margin(bottom: size);
- }
- Container margin({double left = 0, double top = 0, double right = 0, double bottom = 0}) {
- return Container(
- margin: EdgeInsets.only(left: left, top: top, right: right, bottom: bottom),
- child: this,
- );
- }
- //transform 变换
- Transform translate(double dx, double dy) {
- return Transform.translate(
- offset: Offset(dx, dy),
- child: this,
- );
- }
- Transform rotate(double angle) {
- return Transform.rotate(
- angle: angle,
- child: this,
- );
- }
- Transform scale(double scale) {
- return Transform.scale(
- scale: scale,
- child: this,
- );
- }
- //Flex
- Flexible flexible({
- int flex = 1,
- FlexFit fit = FlexFit.loose,
- }) {
- return Flexible(flex: flex, fit: fit, child: this);
- }
- Expanded expanded() {
- return Expanded(child: this);
- }
- Positioned positioned(
- {double? left, double? right, double? top, double? bottom, double? width, double? height}) {
- return Positioned(
- child: this,
- left: left,
- right: right,
- top: top,
- bottom: bottom,
- width: width,
- height: height,
- );
- }
- //套入一个方形的盒子
- // SizedBox square(double size) {
- // return SizedBox.square(child: this, dimension: size);
- // }
- //尺寸相关
- SizedBox height(double? height) {
- return SizedBox(height: height, child: this);
- }
- SizedBox width(double? width) {
- return SizedBox(width: width, child: this);
- }
- ConstrainedBox maxWidth(double? size) {
- return constraints(maxWidth: size);
- }
- ConstrainedBox maxHeight(double? size) {
- return constraints(maxHeight: size);
- }
- ConstrainedBox minWidth(double? size) {
- return constraints(minWidth: size);
- }
- ConstrainedBox minHeight(double? size) {
- return constraints(minHeight: size);
- }
- SizedBox size({double? width, double? height}) {
- return SizedBox(width: width, height: height, child: this);
- }
-
- ///aspect ratio 纵横比
- AspectRatio aspectRatio(double aspectRatio) {
- return AspectRatio(
- child: this,
- aspectRatio: aspectRatio,
- );
- }
- ConstrainedBox constraints(
- {double? maxWidth, double? maxHeight, double? minWidth, double? minHeight}) {
- BoxConstraints constraints = BoxConstraints(
- maxWidth: maxWidth ?? double.infinity,
- maxHeight: maxHeight ?? double.infinity,
- minWidth: minWidth ?? 0,
- minHeight: minHeight ?? 0);
- if (this is ConstrainedBox) {
- var thisConstraints = (this as ConstrainedBox).constraints;
- constraints =
- maxWidth?.let((it) => thisConstraints.copyWith(maxWidth: maxWidth)) ?? thisConstraints;
- constraints =
- maxHeight?.let((it) => thisConstraints.copyWith(maxWidth: maxWidth)) ?? thisConstraints;
- constraints =
- minWidth?.let((it) => thisConstraints.copyWith(maxWidth: maxWidth)) ?? thisConstraints;
- constraints =
- minHeight?.let((it) => thisConstraints.copyWith(maxWidth: maxWidth)) ?? thisConstraints;
- }
- return ConstrainedBox(constraints: constraints, child: this);
- }
- }
- extension ScopeEx<T> on T {
- T also(Function(T it) function) {
- function(this);
- return this;
- }
- R let<R>(R Function(T it) function) {
- return function(this);
- }
- }
|