dialog_input.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. const VDialogInput({
  15. super.key,
  16. this.title,
  17. this.description,
  18. this.placeholder,
  19. this.initialValue,
  20. });
  21. Future<String?> show<String>() => VAlertDialog.showDialog<String>(this);
  22. @override
  23. Widget build(BuildContext context) {
  24. final controller = TextEditingController(text: initialValue);
  25. return VAlertDialog(
  26. title: title,
  27. width: 440,
  28. contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
  29. content: _buildContent(context, controller),
  30. showCancel: true,
  31. onConfirm: () {
  32. Get.back(result: controller.text);
  33. },
  34. );
  35. }
  36. Widget _buildContent(
  37. BuildContext context,
  38. TextEditingController controller,
  39. ) {
  40. final children = <Widget>[];
  41. if (description != null) {
  42. children.add(
  43. Padding(
  44. padding: const EdgeInsets.symmetric(horizontal: 4),
  45. child: Text(
  46. description!,
  47. style: const TextStyle(color: Colors.black87, fontSize: 18),
  48. ),
  49. ),
  50. );
  51. children.add(const SizedBox(height: 8));
  52. } else {
  53. children.add(const SizedBox(height: 12));
  54. }
  55. children.add(_buildInputWidget(context, controller));
  56. return SingleChildScrollView(
  57. child: Column(
  58. mainAxisSize: MainAxisSize.min,
  59. crossAxisAlignment: CrossAxisAlignment.start,
  60. children: children,
  61. ),
  62. );
  63. }
  64. Widget _buildInputWidget(
  65. BuildContext context,
  66. TextEditingController controller,
  67. ) {
  68. const fontSize = 20.0;
  69. const height = 56.0;
  70. return SizedBox(
  71. height: height,
  72. child: TextField(
  73. controller: controller,
  74. readOnly: false,
  75. autofocus: true,
  76. style: const TextStyle(fontSize: fontSize),
  77. decoration: InputDecoration(
  78. border: const UnderlineInputBorder(
  79. borderRadius: BorderRadius.zero,
  80. borderSide: BorderSide(),
  81. ),
  82. enabledBorder: const UnderlineInputBorder(
  83. borderRadius: BorderRadius.zero,
  84. borderSide: BorderSide(
  85. color: Colors.black54,
  86. ),
  87. ),
  88. focusedBorder: UnderlineInputBorder(
  89. borderRadius: BorderRadius.zero,
  90. borderSide: BorderSide(
  91. color: Theme.of(context).primaryColor.withOpacity(.4),
  92. ),
  93. ),
  94. filled: true,
  95. fillColor: Colors.white,
  96. contentPadding: const EdgeInsets.symmetric(
  97. vertical: 8,
  98. horizontal: 8,
  99. ),
  100. hintStyle: const TextStyle(fontSize: fontSize),
  101. labelStyle: const TextStyle(fontSize: fontSize),
  102. hintText: placeholder,
  103. isCollapsed: true,
  104. ),
  105. onSubmitted: (value) {
  106. Get.back(result: value);
  107. },
  108. ),
  109. );
  110. }
  111. }