buildSmartContainer function

Widget buildSmartContainer({
  1. bool visible = true,
  2. BoxConstraints? constraints,
  3. bool userClip = false,
  4. AlignmentGeometry? alignment,
  5. GestureTapCallback? onTap,
  6. bool isCircle = false,
  7. double borderWidth = 0,
  8. Color backgroundColor = Colors.transparent,
  9. double radius = 0,
  10. Widget? child,
  11. EdgeInsets? padding = EdgeInsets.zero,
  12. Color borderColor = Colors.transparent,
  13. EdgeInsets? margin,
  14. BorderRadius? borderRadius,
  15. double? height,
  16. double? width,
  17. HitTestBehavior? behavior,
  18. Offset? offset,
  19. Color? boxColor,
  20. double blurRadius = 0,
  21. double spreadRadius = 0,
  22. List<BoxShadow>? boxShadow,
  23. Widget replacement = const SizedBox.shrink(),
  24. bool maintainSize = false,
  25. bool maintainAnimation = false,
  26. bool maintainState = false,
})

胶囊控件

Implementation

Widget buildSmartContainer({
  bool visible = true,
  BoxConstraints? constraints,
  bool userClip = false,
  AlignmentGeometry? alignment,
  GestureTapCallback? onTap,
  bool isCircle = false,
  double borderWidth = 0,
  Color backgroundColor = Colors.transparent,
  double radius = 0,
  Widget? child,
  EdgeInsets? padding = EdgeInsets.zero,
  Color borderColor = Colors.transparent,
  EdgeInsets? margin,
  BorderRadius? borderRadius,
  double? height,
  double? width,
  HitTestBehavior? behavior,
  Offset? offset,
  Color? boxColor,
  double blurRadius = 0,
  double spreadRadius = 0,
  List<BoxShadow>? boxShadow,
  Widget replacement = const SizedBox.shrink(),
  bool maintainSize = false,
  bool maintainAnimation = false,
  bool maintainState = false
}){

  ///加判断防止过度渲染
  Widget realChild;
  BorderRadius mRadius = borderRadius ?? BorderRadius.all(Radius.circular(getRadius(radius)));

  if(userClip){
    realChild = Container(
      child: ClipRRect(
        borderRadius: mRadius,
        child: Container(
          constraints: constraints,
          width: width,
          height: height,
          margin: margin,
          padding: padding,
          color: backgroundColor,
          alignment: alignment,
          child: child??Container(),
        ),
      ),
    );

    if(borderWidth > 0 || ObjectUtil.isNotEmpty(boxShadow)){
      realChild = Container(
          decoration: BoxDecoration(
              borderRadius: mRadius,
              border: borderWidth == 0? null : Border.all(color: borderColor, width: borderWidth),
              boxShadow: boxShadow
          ),
        child: realChild
      );
    }
  }else{
    realChild = Container(
      constraints: constraints,
      width: width,
      height: height,
      margin: margin,
      padding: padding,
      alignment: alignment,
      decoration: BoxDecoration(
          shape: isCircle? BoxShape.circle : BoxShape.rectangle,
          color: backgroundColor,
          borderRadius: isCircle? null : mRadius,
          border: borderWidth == 0? null : Border.all(color: borderColor, width: borderWidth),
          boxShadow: boxShadow
      ),
      child: child??Container(),
    );
  }

  if(ObjectUtil.isNotEmpty(onTap)){
    realChild = GestureDetector(
        behavior: behavior,
        onTap: onTap,
        child: realChild
    );
  }

  if(!visible){
    realChild = Visibility(
      visible: visible,
      replacement: replacement,
      maintainSize: maintainSize,
      maintainState: maintainState,
      maintainAnimation: maintainAnimation,
      child: GestureDetector(
          behavior: behavior,
          onTap: onTap,
          child: realChild
      ),
    );
  }
  return realChild;
}