【发布时间】:2021-05-27 17:33:06
【问题描述】:
我会链接创建一个TextField,它会缩小到里面的文本。
为此,我使用了this answer,它表示使用IntrinsicWidth 小部件。
效果很好,但是如果我指定hintText,即使用户输入的文字很短,TextField 的宽度也不会小于hintText 的宽度。
这是一个例子:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Container(
alignment: Alignment.center,
height: 50,
width: 300,
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.all(10),
color: Colors.grey[100],
child: IntrinsicWidth(
child: TextField(
decoration: InputDecoration(
hintText: 'My very very long hint', // Remove it and the text field can be as small as the text inside
),
),
),
),
),
),
),
);
}
}
文本域的宽度就是里面文本的宽度,就是我想要的。 ✔️
没有文字的时候,文字字段的宽度就是提示的宽度,这就是我想要的。 ✔️
字段的宽度就是里面文字的宽度,就是我想要的。 ✔️
如您所见,字段宽度是提示的宽度,而不是里面的文本(这不是我想要的 ❌)。
我怎样才能强制它成为里面实际文本的宽度?
【问题讨论】: