mobile_skt/lib/widgets/form_item/sk_text_input.dart

124 lines
3.5 KiB
Dart
Raw Normal View History

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