my_text_view.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'package:flutter/material.dart';
  2. import 'package:ftrecruiter/comm/constants/font_constants.dart';
  3. // ignore: slash_for_doc_comments
  4. /**
  5. TextView + 容器的使用
  6. 内部使用各种容器包裹Text控件,用于一些复杂的UI效果
  7. 模板:
  8. TextView(
  9. "自定义textview自定义textview自定义textview自定义textview自定义textview",
  10. backgroundColor: Colors.red,
  11. textColor: Colors.white,
  12. padding: 10,
  13. cornerRadius: 10,
  14. borderColor: Colors.yellow,
  15. borderWidth: 1,
  16. marginTop: 5,
  17. singleLine: false,
  18. onClick: (){
  19. print("点击事件");
  20. }),
  21. */
  22. class MyTextView extends StatelessWidget {
  23. double? padding = 0;
  24. double? margin = 0;
  25. double? paddingLeft = 0;
  26. double? paddingRight = 0;
  27. double? paddingTop = 0;
  28. double? paddingBottom = 0;
  29. double? marginLeft = 0;
  30. double? marginRight = 0;
  31. double? marginTop = 0;
  32. double? marginBottom = 0;
  33. double? fontSize = 0;
  34. Color? textColor = Colors.black;
  35. Color? backgroundColor = Colors.transparent;
  36. AlignmentGeometry? alignment = Alignment.center;
  37. double? cornerRadius = 0;
  38. double? borderWidth = 0;
  39. Color? borderColor = Colors.transparent;
  40. String content = "";
  41. bool? singleLine = false;
  42. bool? isBold = false;
  43. VoidCallback? onClick;
  44. bool? isFontLight;
  45. bool? isFontRegular;
  46. bool? isFontMedium;
  47. bool? isFontBold;
  48. bool? isFonTimeNew;
  49. String? fontFamily;
  50. MyTextView(this.content,
  51. {this.textColor,
  52. this.backgroundColor,
  53. this.padding,
  54. this.paddingTop,
  55. this.paddingBottom,
  56. this.paddingRight,
  57. this.paddingLeft,
  58. this.cornerRadius,
  59. this.borderColor,
  60. this.borderWidth,
  61. this.marginBottom,
  62. this.marginLeft,
  63. this.marginRight,
  64. this.marginTop,
  65. this.margin,
  66. this.fontSize,
  67. this.singleLine,
  68. this.isBold,
  69. this.isFontLight,
  70. this.isFontRegular,
  71. this.isFontMedium,
  72. this.isFontBold,
  73. this.isFonTimeNew,
  74. this.onClick}) {
  75. if (padding != null) {
  76. if (padding != null && padding! > 0) {
  77. paddingLeft = padding;
  78. paddingRight = padding;
  79. paddingBottom = padding;
  80. paddingTop = padding;
  81. }
  82. }
  83. if (margin != null) {
  84. if (margin != null && margin! > 0) {
  85. marginLeft = margin;
  86. marginTop = margin;
  87. marginRight = margin;
  88. marginBottom = margin;
  89. }
  90. }
  91. onClick ??= () {};
  92. if (isFontLight != null && isFontLight!) {
  93. fontFamily = FontConstants.FONT_FAMILY_SC_LIGHT;
  94. } else if (isFontRegular != null && isFontRegular!) {
  95. fontFamily = FontConstants.FONT_FAMILY_SC_REGULAR;
  96. } else if (isFontMedium != null && isFontMedium!) {
  97. fontFamily = FontConstants.FONT_FAMILY_SC_MEDIUM;
  98. } else if (isFontBold != null && isFontBold!) {
  99. fontFamily = FontConstants.FONT_FAMILY_SC_BOLD;
  100. } else if (isFonTimeNew != null && isFonTimeNew!) {
  101. fontFamily = FontConstants.FONT_FAMILY_TIME_NEW;
  102. } else {
  103. fontFamily = null;
  104. }
  105. }
  106. @override
  107. Widget build(BuildContext context) {
  108. return Container(
  109. margin: EdgeInsets.fromLTRB(marginLeft ?? 0, marginTop ?? 0, marginRight ?? 0, marginBottom ?? 0),
  110. decoration: BoxDecoration(
  111. border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
  112. color: backgroundColor,
  113. borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
  114. ),
  115. padding: EdgeInsets.fromLTRB(paddingLeft ?? 0, paddingTop ?? 0, paddingRight ?? 0, paddingBottom ?? 0),
  116. child: GestureDetector(
  117. onTap: onClick,
  118. child: Text(
  119. content,
  120. style: TextStyle(
  121. color: textColor,
  122. fontSize: fontSize ?? 14,
  123. fontWeight: isBold ?? false ? FontWeight.bold : FontWeight.normal,
  124. overflow: singleLine ?? false ? TextOverflow.ellipsis : TextOverflow.clip,
  125. fontFamily: fontFamily,
  126. ),
  127. ),
  128. ));
  129. }
  130. }