123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:get/get.dart';
- import 'alert_dialog.dart';
- /// 小弹窗输入
- class VDialogInput extends StatelessWidget {
- /// 标题
- final String? title;
- /// 描述
- final String? description;
- /// 输入占位符
- final String? placeholder;
- /// 初始值
- final String? initialValue;
- /// 是否为富文本框
- final int? maxLines;
- /// 当前输入框的高度
- final double? inputHeight;
- ///校验文本是否合法,不合法无法提交
- final bool Function(String)? onConfirmVerification;
- ///是否能点击蒙层取消弹窗
- final bool showCancel;
- final List<TextInputFormatter>? inputFormatters;
- final TextInputType? keyboardType;
- const VDialogInput({
- super.key,
- this.title,
- this.description,
- this.placeholder,
- this.initialValue,
- this.maxLines = 1,
- this.inputHeight = 56,
- this.onConfirmVerification,
- this.inputFormatters,
- this.showCancel = false,
- this.keyboardType,
- });
- Future<String?> show<String>() => VAlertDialog.showDialog<String>(this);
- @override
- Widget build(BuildContext context) {
- final controller = TextEditingController(text: initialValue);
- return VAlertDialog(
- title: title,
- width: 440,
- contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
- content: _buildContent(context, controller),
- showCancel: showCancel,
- onConfirm: () {
- if (onConfirmVerification != null) {
- FocusScope.of(context).nextFocus();
- bool isValid = onConfirmVerification!.call(controller.text);
- if (isValid) {
- Get.back(result: controller.text);
- }
- } else {
- Get.back(result: controller.text);
- }
- },
- );
- }
- Widget _buildContent(
- BuildContext context,
- TextEditingController controller,
- ) {
- final children = <Widget>[];
- if (description != null) {
- children.add(
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 4),
- child: Text(
- description!,
- style: const TextStyle(color: Colors.black87, fontSize: 18),
- ),
- ),
- );
- children.add(const SizedBox(height: 8));
- } else {
- children.add(const SizedBox(height: 12));
- }
- children.add(_buildInputWidget(context, controller));
- return SingleChildScrollView(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: children,
- ),
- );
- }
- Widget _buildInputWidget(
- BuildContext context,
- TextEditingController controller,
- ) {
- const fontSize = 20.0;
- return SizedBox(
- height: inputHeight,
- child: TextField(
- controller: controller,
- readOnly: false,
- autofocus: true,
- maxLines: maxLines,
- keyboardType: keyboardType,
- inputFormatters: inputFormatters,
- style: const TextStyle(fontSize: fontSize),
- decoration: InputDecoration(
- border: const UnderlineInputBorder(
- borderRadius: BorderRadius.zero,
- borderSide: BorderSide(),
- ),
- enabledBorder: const UnderlineInputBorder(
- borderRadius: BorderRadius.zero,
- borderSide: BorderSide(
- color: Colors.black54,
- ),
- ),
- focusedBorder: UnderlineInputBorder(
- borderRadius: BorderRadius.zero,
- borderSide: BorderSide(
- color: Theme.of(context).primaryColor.withOpacity(.4),
- ),
- ),
- filled: true,
- fillColor: Colors.white,
- contentPadding: const EdgeInsets.symmetric(
- vertical: 8,
- horizontal: 8,
- ),
- hintStyle: const TextStyle(fontSize: fontSize),
- labelStyle: const TextStyle(fontSize: fontSize),
- hintText: placeholder,
- isCollapsed: true,
- ),
- onSubmitted: (value) {
- if (onConfirmVerification != null) {
- bool isValid = onConfirmVerification!.call(value);
- if (isValid) {
- Get.back(result: value);
- }
- } else {
- Get.back(result: value);
- }
- },
- ),
- );
- }
- }
|