Flutter 在颤振中居中对齐主图标
我需要将提示文本和中间的前导图标对齐,如下所示: 当我添加一个主图标时,我得到的就是中心对齐装饰。我需要的图标,以在中心以及 编辑:当前代码Flutter 在颤振中居中对齐主图标,flutter,flutter-layout,Flutter,Flutter Layout,我需要将提示文本和中间的前导图标对齐,如下所示: 当我添加一个主图标时,我得到的就是中心对齐装饰。我需要的图标,以在中心以及 编辑:当前代码 TextField( textAlign: TextAlign.center, decoration: InputDecoration( hintText: 'Type something', prefixIcon: Icon(Icons.search) ) ), 删除文本对齐中心并尝试此操作
TextField(
textAlign: TextAlign.center,
decoration: InputDecoration(
hintText: 'Type something',
prefixIcon: Icon(Icons.search)
)
),
删除文本对齐中心并尝试此操作
TextField(
decoration: InputDecoration(
hintText: 'Type something',
prefixIcon: Padding(
padding: const EdgeInsets.only(left:100.0),
child: Icon(Icons.search,),
),
)
)
您的案例有一个小部件:
IntrinsicWidth
。此小部件用于将其子对象的大小调整为子对象的固有宽度
输出
完整代码:
class CenteredTextField extends StatefulWidget {
const CenteredTextField({Key key}) : super(key: key);
@override
_CenteredTextFieldState createState() => _CenteredTextFieldState();
}
class _CenteredTextFieldState extends State<CenteredTextField> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: 40.0,
margin: EdgeInsets.symmetric(horizontal: 20.0, vertical: 50.0),
decoration: BoxDecoration(
color: Colors.orange.withOpacity(0.4),
borderRadius: BorderRadius.circular(20.0),
border: Border.all(
color: Colors.orange,
width: 1.0,
),
),
child: Center(
child: IntrinsicWidth(
child: TextField(
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
prefixIcon: Icon(Icons.search),
hintText: 'Type verse address',
border: InputBorder.none,
),
),
),
),
),
);
}
}
类CenteredTextField扩展StatefulWidget{
const CenteredTextField({Key}):super(Key:Key);
@凌驾
_CenteredTextFieldState createState()=>\u CenteredTextFieldState();
}
类_CenteredTextFieldState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
身高:40.0,
边缘:边缘组。对称(水平:20.0,垂直:50.0),
装饰:盒子装饰(
颜色:颜色。橙色。不透明度(0.4),
边界半径:边界半径。圆形(20.0),
边界:边界(
颜色:颜色。橙色,
宽度:1.0,
),
),
儿童:中心(
子:内部宽度(
孩子:TextField(
textAlignVertical:textAlignVertical.center,
装饰:输入装饰(
前缀:图标(Icons.search),
hintText:'键入韵文地址',
边框:InputBorder.none,
),
),
),
),
),
);
}
}
您可以尝试以下代码:
Center(
child: Container(
height: 40.0,
child: Center(
child: IntrinsicWidth(
child: TextField(
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
prefixIcon: Icon(Icons.search),
hintText: 'type your hint here',
),
),
),
),
),
)
您需要共享现有代码。