2024-03-20 18:31:37 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
|
2024-04-10 11:04:27 +08:00
|
|
|
import 'package:sk_base_mobile/widgets/form_item/sk_form_item.dart';
|
|
|
|
import 'package:sk_base_mobile/widgets/form_item/sk_base_field.dart';
|
2024-03-20 18:31:37 +08:00
|
|
|
|
2024-04-09 16:32:18 +08:00
|
|
|
class SkTextInput extends SkBaseFieldWidget {
|
2024-03-20 18:31:37 +08:00
|
|
|
final TextEditingController textController;
|
2024-04-01 17:35:34 +08:00
|
|
|
final Function(FocusNode)? onTap;
|
2024-03-20 18:31:37 +08:00
|
|
|
final bool isRequired;
|
2024-04-09 16:32:18 +08:00
|
|
|
|
2024-03-20 18:31:37 +08:00
|
|
|
final String? hint;
|
|
|
|
final bool isTextArea;
|
2024-04-01 17:35:34 +08:00
|
|
|
final bool isDense;
|
2024-04-03 13:45:34 +08:00
|
|
|
final Function(String)? onTapOutside;
|
|
|
|
final Function(String)? onChanged;
|
|
|
|
final String? Function(String?)? validator;
|
2024-04-01 17:35:34 +08:00
|
|
|
final EdgeInsetsGeometry? contentPadding;
|
2024-04-09 16:32:18 +08:00
|
|
|
|
2024-04-07 17:32:46 +08:00
|
|
|
final ValueChanged<String>? onFieldSubmitted;
|
2024-04-08 13:50:23 +08:00
|
|
|
final Icon? prefix;
|
|
|
|
final Widget? suffixIcon;
|
|
|
|
final InputBorder? border;
|
|
|
|
final FloatingLabelBehavior? floatingLabelBehavior;
|
2024-04-09 08:31:17 +08:00
|
|
|
final TextInputType? keyboardType;
|
2024-04-09 16:32:18 +08:00
|
|
|
|
|
|
|
SkTextInput(
|
2024-04-03 13:45:34 +08:00
|
|
|
{super.key,
|
2024-04-09 16:32:18 +08:00
|
|
|
super.customLabel = false,
|
|
|
|
super.autoFocus = false,
|
|
|
|
super.labelText,
|
2024-04-10 11:04:27 +08:00
|
|
|
super.fillColor,
|
2024-04-03 13:45:34 +08:00
|
|
|
required this.textController,
|
|
|
|
this.onTap,
|
|
|
|
this.hint,
|
|
|
|
this.onFieldSubmitted,
|
|
|
|
this.isRequired = false,
|
|
|
|
this.onTapOutside,
|
2024-04-09 08:31:17 +08:00
|
|
|
this.keyboardType,
|
2024-04-08 13:50:23 +08:00
|
|
|
this.prefix,
|
|
|
|
this.suffixIcon,
|
2024-04-03 13:45:34 +08:00
|
|
|
this.onChanged,
|
2024-04-08 13:50:23 +08:00
|
|
|
this.border,
|
|
|
|
this.floatingLabelBehavior = FloatingLabelBehavior.always,
|
2024-04-03 13:45:34 +08:00
|
|
|
this.isTextArea = false,
|
|
|
|
this.contentPadding,
|
|
|
|
this.isDense = false,
|
|
|
|
this.validator});
|
2024-04-01 17:35:34 +08:00
|
|
|
|
2024-03-20 18:31:37 +08:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-04-09 16:32:18 +08:00
|
|
|
Widget field = TextFormField(
|
2024-04-01 17:35:34 +08:00
|
|
|
focusNode: focusNode,
|
2024-04-09 16:32:18 +08:00
|
|
|
controller: textController,
|
2024-04-03 13:45:34 +08:00
|
|
|
onChanged: (String value) {
|
2024-04-09 16:32:18 +08:00
|
|
|
if (onChanged != null) {
|
|
|
|
onChanged!(value);
|
2024-04-03 13:45:34 +08:00
|
|
|
}
|
|
|
|
},
|
2024-03-20 18:31:37 +08:00
|
|
|
onTapOutside: (event) {
|
2024-04-09 16:32:18 +08:00
|
|
|
if (onTapOutside != null) {
|
|
|
|
onTapOutside!(textController.text);
|
2024-04-03 13:45:34 +08:00
|
|
|
FocusScope.of(context).unfocus();
|
|
|
|
}
|
2024-03-20 18:31:37 +08:00
|
|
|
},
|
2024-04-09 16:32:18 +08:00
|
|
|
maxLines: isTextArea ? 2 : 1, // 添加这行代码
|
2024-04-01 17:35:34 +08:00
|
|
|
onTap: () {
|
2024-04-09 16:32:18 +08:00
|
|
|
if (onTap != null) {
|
|
|
|
onTap!(focusNode);
|
2024-04-01 17:35:34 +08:00
|
|
|
}
|
|
|
|
},
|
2024-04-09 16:32:18 +08:00
|
|
|
keyboardType: keyboardType,
|
|
|
|
onFieldSubmitted: onFieldSubmitted,
|
2024-04-03 13:45:34 +08:00
|
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
2024-04-09 16:32:18 +08:00
|
|
|
validator: validator,
|
2024-04-08 13:50:23 +08:00
|
|
|
|
2024-03-20 18:31:37 +08:00
|
|
|
decoration: InputDecoration(
|
2024-04-09 16:32:18 +08:00
|
|
|
prefixIcon: prefix,
|
|
|
|
suffixIcon: suffixIcon,
|
2024-04-10 11:04:27 +08:00
|
|
|
fillColor: fillColor,
|
|
|
|
filled: true,
|
2024-04-03 13:45:34 +08:00
|
|
|
errorStyle: const TextStyle(fontSize: 0, height: 0.01),
|
2024-04-09 16:32:18 +08:00
|
|
|
contentPadding: contentPadding,
|
|
|
|
isDense: isDense,
|
|
|
|
border: border,
|
|
|
|
floatingLabelBehavior: floatingLabelBehavior,
|
|
|
|
label: labelText != null && !customLabel
|
2024-04-08 13:50:23 +08:00
|
|
|
? Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2024-04-09 16:32:18 +08:00
|
|
|
if (isRequired)
|
2024-04-08 13:50:23 +08:00
|
|
|
Text(
|
|
|
|
"*",
|
|
|
|
style: TextStyle(
|
|
|
|
color: Colors.red,
|
|
|
|
fontSize: ScreenAdaper.height(30)),
|
|
|
|
),
|
|
|
|
Text(
|
2024-04-09 16:32:18 +08:00
|
|
|
labelText!,
|
2024-04-08 13:50:23 +08:00
|
|
|
style: TextStyle(fontSize: ScreenAdaper.height(30)),
|
|
|
|
),
|
|
|
|
])
|
|
|
|
: null,
|
2024-04-09 16:32:18 +08:00
|
|
|
hintText: hint ?? '请输入',
|
2024-03-20 18:31:37 +08:00
|
|
|
),
|
|
|
|
);
|
2024-04-09 16:32:18 +08:00
|
|
|
return SkFormItem(
|
|
|
|
controller: baseFieldController,
|
|
|
|
customLabel: customLabel,
|
|
|
|
labelText: labelText,
|
|
|
|
isRequired: isRequired,
|
|
|
|
child: field,
|
|
|
|
);
|
2024-03-20 18:31:37 +08:00
|
|
|
}
|
|
|
|
}
|
2024-04-09 16:32:18 +08:00
|
|
|
|
|
|
|
// class SkTextInputController extends GetxController {
|
|
|
|
// RxBool isFocus = false.obs;
|
|
|
|
// @override
|
|
|
|
// void onReady() {
|
|
|
|
// super.onReady();
|
|
|
|
// }
|
|
|
|
// }
|