Flutter 输入组件模板

narakuyang
2023-02-07 / 0 评论 / 30 阅读 / 正在检测是否收录...

class comInput extends StatelessWidget {
  final TextEditingController controller;
  final List<TextInputFormatter>? inputFormatters;
  final String hintText;
  final Widget? prewidget;
  final Widget? suffwidget;
  final TextInputType? keyType;
  final bool? readOnly;
  final bool? obscureText;
  const comInput(
      {super.key,
      required this.controller,
      this.inputFormatters,
      required this.hintText,
      this.prewidget,
      this.suffwidget,
      this.keyType,
      this.readOnly = false,
      this.obscureText = false});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 18, right: 18),
      child: TextFormField(
          controller: controller,
          onEditingComplete: () {
            FocusScope.of(context).unfocus();
          },
          keyboardType: keyType != null ? keyType : TextInputType.number,
          textInputAction: TextInputAction.done,
          readOnly: readOnly!,
          obscureText: obscureText!,
          obscuringCharacter: '⁙',
          inputFormatters: inputFormatters != null
              ? inputFormatters
              : [
                  LengthLimitingTextInputFormatter(50),
                  FilteringTextInputFormatter.allow(RegExp("[0-9.]"))
                ],
          cursorColor: colorController.buttom.value.withOpacity(0.5),
          decoration: InputDecoration(
            contentPadding: EdgeInsets.all(12.0),
            focusedErrorBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(12),
                borderSide: BorderSide(color: Colors.red, width: 2)),
            focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(12),
                borderSide:
                    BorderSide(color: colorController.buttom.value, width: 2)),
            hintText: hintText,
            hintStyle: GoogleFonts.notoSans(
                color: getIsDark(context) == true
                    ? appColor.white.withOpacity(0.8)
                    : appColor.black.withOpacity(0.3)),
            prefixIcon: prewidget != null
                ? Padding(
                    padding: const EdgeInsets.only(left: 6, right: 6),
                    child: prewidget,
                  )
                : SizedBox(),
            suffixIcon: suffwidget != null
                ? Padding(
                    padding: const EdgeInsets.only(left: 6, right: 6),
                    child: suffwidget,
                  )
                : SizedBox(),
            suffixIconConstraints: BoxConstraints(
              maxHeight: 30,
              maxWidth: 40,
            ),
            prefixIconConstraints: BoxConstraints(
              maxHeight: 30,
              maxWidth: 40,
            ),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(12),
            ),
          )),
    );
  }
}
0

评论

博主关闭了所有页面的评论