my_text_view.dart 3.7 KB

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