Skip to content

Instantly share code, notes, and snippets.

@zmtzawqlp
Created August 13, 2021 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmtzawqlp/3cef21c0d9673a6b7b5458d182b4a649 to your computer and use it in GitHub Desktop.
Save zmtzawqlp/3cef21c0d9673a6b7b5458d182b4a649 to your computer and use it in GitHub Desktop.
文本扩展
/*
* @Author: zmtzawqlp
* @Date: 2020-01-07 15:26:54
* @Last Modified by: zmtzawqlp
* @Last Modified time: 2020-04-07 15:38:18
*/
import 'package:flutter/widgets.dart';
/// Widget相关扩展
extension TextE on Text {
/// 文本上对齐
Text alignUp({
@required double fontSize,
@required double lineHeight,
Color color,
}) {
return data == null
? Text.rich(
textSpan,
textAlign: textAlign,
textDirection: textDirection,
textHeightBehavior: textHeightBehavior,
textScaleFactor: textScaleFactor,
textWidthBasis: textWidthBasis,
locale: locale,
softWrap: softWrap,
overflow: overflow,
maxLines: maxLines,
semanticsLabel: semanticsLabel,
style: style?.copyWith(
fontSize: fontSize,
height: 1,
inherit: style?.inherit,
color: color ?? style?.color,
backgroundColor: style?.backgroundColor,
fontFamily: style?.fontFamily,
fontFamilyFallback: style?.fontFamilyFallback,
fontWeight: style?.fontWeight,
fontStyle: style?.fontStyle,
letterSpacing: style?.letterSpacing,
wordSpacing: style?.wordSpacing,
textBaseline: style?.textBaseline,
locale: style?.locale,
foreground: style?.foreground,
background: style?.background,
shadows: style?.shadows,
fontFeatures: style?.fontFeatures,
decoration: style?.decoration,
decorationColor: style?.decorationColor,
decorationStyle: style?.decorationStyle,
decorationThickness: style?.decorationThickness,
debugLabel: style?.debugLabel,
) ??
TextStyle(
fontSize: fontSize,
height: 1,
color: color,
),
strutStyle: StrutStyle(
height: lineHeight / fontSize,
),
)
: Text(
data,
textAlign: textAlign,
textDirection: textDirection,
textHeightBehavior: textHeightBehavior,
textScaleFactor: textScaleFactor,
textWidthBasis: textWidthBasis,
locale: locale,
softWrap: softWrap,
overflow: overflow,
maxLines: maxLines,
semanticsLabel: semanticsLabel,
style: style?.copyWith(
fontSize: fontSize,
height: 1,
inherit: style?.inherit,
color: color ?? style?.color,
backgroundColor: style?.backgroundColor,
fontFamily: style?.fontFamily,
fontFamilyFallback: style?.fontFamilyFallback,
fontWeight: style?.fontWeight,
fontStyle: style?.fontStyle,
letterSpacing: style?.letterSpacing,
wordSpacing: style?.wordSpacing,
textBaseline: style?.textBaseline,
locale: style?.locale,
foreground: style?.foreground,
background: style?.background,
shadows: style?.shadows,
fontFeatures: style?.fontFeatures,
decoration: style?.decoration,
decorationColor: style?.decorationColor,
decorationStyle: style?.decorationStyle,
decorationThickness: style?.decorationThickness,
debugLabel: style?.debugLabel,
) ??
TextStyle(
fontSize: fontSize,
height: 1,
color: color,
),
strutStyle: StrutStyle(
height: lineHeight / fontSize,
),
);
}
/// 文本下对齐
Text alignDown({
@required double fontSize,
@required double lineHeight,
Color color,
}) {
final double height = 1.0 - (lineHeight - fontSize) / 2.0 / lineHeight;
return data == null
? Text.rich(
textSpan,
textAlign: textAlign,
textDirection: textDirection,
textHeightBehavior: textHeightBehavior,
textScaleFactor: textScaleFactor,
textWidthBasis: textWidthBasis,
locale: locale,
softWrap: softWrap,
overflow: overflow,
maxLines: maxLines,
semanticsLabel: semanticsLabel,
style: style?.copyWith(
fontSize: fontSize,
height: height,
inherit: style?.inherit,
color: color ?? style?.color,
backgroundColor: style?.backgroundColor,
fontFamily: style?.fontFamily,
fontFamilyFallback: style?.fontFamilyFallback,
fontWeight: style?.fontWeight,
fontStyle: style?.fontStyle,
letterSpacing: style?.letterSpacing,
wordSpacing: style?.wordSpacing,
textBaseline: style?.textBaseline,
locale: style?.locale,
foreground: style?.foreground,
background: style?.background,
shadows: style?.shadows,
fontFeatures: style?.fontFeatures,
decoration: style?.decoration,
decorationColor: style?.decorationColor,
decorationStyle: style?.decorationStyle,
decorationThickness: style?.decorationThickness,
debugLabel: style?.debugLabel,
) ??
TextStyle(
fontSize: fontSize,
height: height,
color: color,
),
)
: Text(
data,
textAlign: textAlign,
textDirection: textDirection,
textHeightBehavior: textHeightBehavior,
textScaleFactor: textScaleFactor,
textWidthBasis: textWidthBasis,
locale: locale,
softWrap: softWrap,
overflow: overflow,
maxLines: maxLines,
semanticsLabel: semanticsLabel,
style: style?.copyWith(
fontSize: fontSize,
height: height,
inherit: style?.inherit,
color: color ?? style?.color,
backgroundColor: style?.backgroundColor,
fontFamily: style?.fontFamily,
fontFamilyFallback: style?.fontFamilyFallback,
fontWeight: style?.fontWeight,
fontStyle: style?.fontStyle,
letterSpacing: style?.letterSpacing,
wordSpacing: style?.wordSpacing,
textBaseline: style?.textBaseline,
locale: style?.locale,
foreground: style?.foreground,
background: style?.background,
shadows: style?.shadows,
fontFeatures: style?.fontFeatures,
decoration: style?.decoration,
decorationColor: style?.decorationColor,
decorationStyle: style?.decorationStyle,
decorationThickness: style?.decorationThickness,
debugLabel: style?.debugLabel,
) ??
TextStyle(
fontSize: fontSize,
height: height,
color: color,
),
);
}
/// 设置行高
Text withLineHeight({
@required double fontSize,
@required double lineHeight,
Color color,
}) {
return data == null
? Text.rich(
textSpan,
textAlign: textAlign,
textDirection: textDirection,
textHeightBehavior: textHeightBehavior,
textScaleFactor: textScaleFactor,
textWidthBasis: textWidthBasis,
locale: locale,
softWrap: softWrap,
overflow: overflow,
maxLines: maxLines,
semanticsLabel: semanticsLabel,
style: style?.copyWith(
fontSize: fontSize,
height: lineHeight / fontSize,
inherit: style?.inherit,
color: color ?? style?.color,
backgroundColor: style?.backgroundColor,
fontFamily: style?.fontFamily,
fontFamilyFallback: style?.fontFamilyFallback,
fontWeight: style?.fontWeight,
fontStyle: style?.fontStyle,
letterSpacing: style?.letterSpacing,
wordSpacing: style?.wordSpacing,
textBaseline: style?.textBaseline,
locale: style?.locale,
foreground: style?.foreground,
background: style?.background,
shadows: style?.shadows,
fontFeatures: style?.fontFeatures,
decoration: style?.decoration,
decorationColor: style?.decorationColor,
decorationStyle: style?.decorationStyle,
decorationThickness: style?.decorationThickness,
debugLabel: style?.debugLabel,
) ??
TextStyle(
fontSize: fontSize,
height: lineHeight / fontSize,
color: color,
),
)
: Text(
data,
textAlign: textAlign,
textDirection: textDirection,
textHeightBehavior: textHeightBehavior,
textScaleFactor: textScaleFactor,
textWidthBasis: textWidthBasis,
locale: locale,
softWrap: softWrap,
overflow: overflow,
maxLines: maxLines,
semanticsLabel: semanticsLabel,
style: style?.copyWith(
fontSize: fontSize,
height: lineHeight / fontSize,
inherit: style?.inherit,
color: color ?? style?.color,
backgroundColor: style?.backgroundColor,
fontFamily: style?.fontFamily,
fontFamilyFallback: style?.fontFamilyFallback,
fontWeight: style?.fontWeight,
fontStyle: style?.fontStyle,
letterSpacing: style?.letterSpacing,
wordSpacing: style?.wordSpacing,
textBaseline: style?.textBaseline,
locale: style?.locale,
foreground: style?.foreground,
background: style?.background,
shadows: style?.shadows,
fontFeatures: style?.fontFeatures,
decoration: style?.decoration,
decorationColor: style?.decorationColor,
decorationStyle: style?.decorationStyle,
decorationThickness: style?.decorationThickness,
debugLabel: style?.debugLabel,
) ??
TextStyle(
fontSize: fontSize,
height: lineHeight / fontSize,
color: color,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment