所有答案都适用于固定大小的文本字段,但在我的情况下,我使用的是一个动态大小的容器,它是我的文本字段的父级
AnimatedContainer(
width: (!widget.scrollController.hasClients ||
widget.scrollController.positions.length > 1)
? MediaQuery.of(context).size.width.toDouble()
: max(
MediaQuery.of(context).size.width -
widget.scrollController.offset.roundToDouble(),
(MediaQuery.of(context).size.width - 80.w).toDouble()),
duration: const Duration(milliseconds: 150),
constraints: BoxConstraints(maxHeight: 40.h),
margin: EdgeInsets.zero,
child: TextField(
controller: controller,
expands: true,
minLines: null,
maxLines: null,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
filled: true,
fillColor: MyAppColors.primaryColor1,
border: UnderlineInputBorder(
borderRadius:
BorderRadius.circular(15.w)),
prefixIcon: Icon(
Icons.search,
size: 20.w,
color: Theme.of(context).accentColor,
),
hintText: 'Songs, albums or artists',
contentPadding: EdgeInsets.zero,
hintStyle:
Theme.of(context).textTheme.subtitle2!.copyWith(
color: Colors.white.withOpacity(0.8),
fontSize: 18.sp,
)),
),
);
对我有用的解决方案是我将 expand 参数设置为 true 并设置这些参数:
minLines: null,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.center
必须将 min 和 max 行设置为 null 才能展开。
这是我找到的唯一解决方案,因为我的提示文本获得了额外的填充,即使我将内容填充设置为零我的提示文本不在其他屏幕尺寸中居中。