configure_layout.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import 'package:fis_i18n/i18n.dart';
  2. import 'package:fis_ui/index.dart';
  3. import 'package:flutter/material.dart';
  4. /// 配置项布局组件
  5. class FConfigureLayoutStyle extends InheritedWidget implements FWidget {
  6. const FConfigureLayoutStyle({
  7. key,
  8. required this.child,
  9. required this.width,
  10. }) : super(child: child);
  11. final double width;
  12. final FWidget child;
  13. static double? of(BuildContext context) {
  14. final style =
  15. context.dependOnInheritedWidgetOfExactType<FConfigureLayoutStyle>();
  16. return style?.width;
  17. }
  18. @override
  19. bool updateShouldNotify(covariant InheritedWidget oldWidget) {
  20. return this.width != oldWidget.child;
  21. }
  22. }
  23. class FConfigureLayout extends FStatelessWidget {
  24. static const C_FONT_SIZE = 16.0;
  25. static const C_PADDING_LEFT = 25.0;
  26. static const C_PADDING_BOTTOM = 5.0;
  27. static const C_MARGIN_HORIZONTAL_SIZE = 15.0;
  28. static const C_MARGIN_VERTICAL_SIZE = 10.0;
  29. /// TODO:[Gavin] i18n-UI-整合
  30. double C_FCONTAITNER_WIDTH = i18nBook.isCurrentChinese ? 80 : 160.0;
  31. /// 标题
  32. final String title;
  33. /// 组件
  34. final FWidget widget;
  35. /// 比例
  36. final double? proportion;
  37. /// 是否必填
  38. final bool? isRequired;
  39. /// 是否显示标题
  40. final bool? isShowTitle;
  41. FConfigureLayout({
  42. required this.title,
  43. required this.widget,
  44. this.proportion = 1,
  45. this.isRequired = false,
  46. this.isShowTitle = true,
  47. });
  48. @override
  49. FWidget build(BuildContext context) {
  50. final width = FConfigureLayoutStyle.of(context);
  51. return FContainer(
  52. width: proportion! * width!,
  53. child: FRow(
  54. mainAxisAlignment: MainAxisAlignment.spaceAround,
  55. crossAxisAlignment: CrossAxisAlignment.center,
  56. children: [
  57. FSizedBox(
  58. width: 20,
  59. ),
  60. isShowTitle!
  61. ? _buildTitle()
  62. : FSizedBox(
  63. width: C_PADDING_LEFT,
  64. ),
  65. widget,
  66. ],
  67. ),
  68. );
  69. }
  70. /// 构建标题
  71. FWidget _buildTitle() {
  72. final labelStyle = TextStyle(fontSize: C_FONT_SIZE);
  73. return FContainer(
  74. margin: EdgeInsets.symmetric(
  75. vertical: C_MARGIN_VERTICAL_SIZE,
  76. horizontal: C_MARGIN_HORIZONTAL_SIZE,
  77. ),
  78. width: C_FCONTAITNER_WIDTH,
  79. alignment: Alignment.centerLeft,
  80. child: FRow(
  81. mainAxisAlignment: MainAxisAlignment.center,
  82. children: [
  83. FExpanded(
  84. child: FText.rich(
  85. TextSpan(
  86. children: [
  87. TextSpan(
  88. text: title,
  89. style: labelStyle,
  90. ),
  91. if (isRequired!) ...[
  92. TextSpan(
  93. text: ' *',
  94. style: labelStyle.copyWith(color: Colors.red),
  95. )
  96. ] else ...[
  97. TextSpan(text: '')
  98. ]
  99. ],
  100. ),
  101. ),
  102. ),
  103. ],
  104. ),
  105. );
  106. }
  107. }