2024年Flutter 项目实战 编辑框(TextField) 自定义 七,2024年最新程序员面试自我介绍
在Flutter中,自定义TextField
通常意味着创建一个继承自TextField
的新类,并重写其中的某些方法来改变其行为。以下是一个自定义TextField
的简单示例,它增加了一个自定义的装饰:
import 'package:flutter/material.dart';
class CustomTextField extends TextField {
CustomTextField({
Key? key,
TextEditingController? controller,
FocusNode? focusNode,
InputDecoration? decoration = const InputDecoration(),
TextInputType? keyboardType,
TextInputAction? textInputAction,
TextStyle? style,
StrutStyle? strutStyle,
TextDirection? textDirection,
TextAlign textAlign = TextAlign.start,
bool readOnly = false,
bool showCursor = true,
bool autofocus = false,
bool obscureText = false,
bool autocorrect = true,
int maxLines = 1,
int minLines = 1,
MaxLengthEnforcement? maxLengthEnforcement,
int? maxLength,
ValueChanged<String>? onChanged,
VoidCallback? onEditingComplete,
ValueChanged<String>? onSubmitted,
List<TextInputFormatter>? inputFormatters,
bool enabled = true,
double cursorWidth = 2.0,
Radius? cursorRadius,
Color? cursorColor,
Brightness? keyboardAppearance,
EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
bool enableInteractiveSelection = true,
InputCounterWidgetBuilder? buildCounter,
}) : super(
key: key,
controller: controller,
focusNode: focusNode,
decoration: decoration!.copyWith(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(color: Colors.blue),
),
filled: true,
fillColor: Colors.blue.shade100,
),
keyboardType: keyboardType,
textInputAction: textInputAction,
style: style,
strutStyle: strutStyle,
textDirection: textDirection,
textAlign: textAlign,
readOnly: readOnly,
showCursor: showCursor,
autofocus: autofocus,
obscureText: obscureText,
autocorrect: autocorrect,
maxLines: maxLines,
minLines: minLines,
maxLengthEnforcement: maxLengthEnforcement,
maxLength: maxLength,
onChanged: onChanged,
onEditingComplete: onEditingComplete,
onSubmitted: onSubmitted,
inputFormatters: inputFormatters,
enabled: enabled,
cursorWidth: cursorWidth,
cursorRadius: cursorRadius,
cursorColor: cur
评论已关闭