dialog_input.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'alert_dialog.dart';
  4. /// 小弹窗输入
  5. class VDialogInput extends StatelessWidget {
  6. /// 标题
  7. final String? title;
  8. /// 描述
  9. final String? description;
  10. /// 输入占位符
  11. final String? placeholder;
  12. /// 初始值
  13. final String? initialValue;
  14. /// 是否为富文本框
  15. final int? maxLines;
  16. /// 当前输入框的高度
  17. final double? inputHeight;
  18. const VDialogInput({
  19. super.key,
  20. this.title,
  21. this.description,
  22. this.placeholder,
  23. this.initialValue,
  24. this.maxLines = 1,
  25. this.inputHeight = 56,
  26. });
  27. Future<String?> show<String>() => VAlertDialog.showDialog<String>(this);
  28. @override
  29. Widget build(BuildContext context) {
  30. final controller = TextEditingController(text: initialValue);
  31. return VAlertDialog(
  32. title: title,
  33. width: 440,
  34. contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
  35. content: _buildContent(context, controller),
  36. showCancel: true,
  37. onConfirm: () {
  38. Get.back(result: controller.text);
  39. },
  40. );
  41. }
  42. Widget _buildContent(
  43. BuildContext context,
  44. TextEditingController controller,
  45. ) {
  46. final children = <Widget>[];
  47. if (description != null) {
  48. children.add(
  49. Padding(
  50. padding: const EdgeInsets.symmetric(horizontal: 4),
  51. child: Text(
  52. description!,
  53. style: const TextStyle(color: Colors.black87, fontSize: 18),
  54. ),
  55. ),
  56. );
  57. children.add(const SizedBox(height: 8));
  58. } else {
  59. children.add(const SizedBox(height: 12));
  60. }
  61. children.add(_buildInputWidget(context, controller));
  62. return SingleChildScrollView(
  63. child: Column(
  64. mainAxisSize: MainAxisSize.min,
  65. crossAxisAlignment: CrossAxisAlignment.start,
  66. children: children,
  67. ),
  68. );
  69. }
  70. Widget _buildInputWidget(
  71. BuildContext context,
  72. TextEditingController controller,
  73. ) {
  74. const fontSize = 20.0;
  75. return SizedBox(
  76. height: inputHeight,
  77. child: TextField(
  78. controller: controller,
  79. readOnly: false,
  80. autofocus: true,
  81. maxLines: maxLines,
  82. style: const TextStyle(fontSize: fontSize),
  83. decoration: InputDecoration(
  84. border: const UnderlineInputBorder(
  85. borderRadius: BorderRadius.zero,
  86. borderSide: BorderSide(),
  87. ),
  88. enabledBorder: const UnderlineInputBorder(
  89. borderRadius: BorderRadius.zero,
  90. borderSide: BorderSide(
  91. color: Colors.black54,
  92. ),
  93. ),
  94. focusedBorder: UnderlineInputBorder(
  95. borderRadius: BorderRadius.zero,
  96. borderSide: BorderSide(
  97. color: Theme.of(context).primaryColor.withOpacity(.4),
  98. ),
  99. ),
  100. filled: true,
  101. fillColor: Colors.white,
  102. contentPadding: const EdgeInsets.symmetric(
  103. vertical: 8,
  104. horizontal: 8,
  105. ),
  106. hintStyle: const TextStyle(fontSize: fontSize),
  107. labelStyle: const TextStyle(fontSize: fontSize),
  108. hintText: placeholder,
  109. isCollapsed: true,
  110. ),
  111. onSubmitted: (value) {
  112. Get.back(result: value);
  113. },
  114. ),
  115. );
  116. }
  117. }