gpt4 book ai didi

flutter - 为什么 ElevatedButton 会被拉伸(stretch)以适应屏幕的宽度?

转载 作者:行者123 更新时间:2023-12-05 04:47:23 24 4
gpt4 key购买 nike

这是屏幕部分的完整代码:

ListView(children: [
ClipRect(
child: Image.asset(
'images/icon.png',
scale: 2,
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
child: TextField(
controller: _word,
textAlign: TextAlign.center,
decoration: InputDecoration(
filled: true,
helperText: 'Enter a word',
),
),
),
Padding(
padding: const EdgeInsets.only(top: 20, left: 24, right: 24, bottom: 8),
child: ElevatedButton(
child: Text("Search!"),
onPressed: () async {
String word = _word.text;
_word.text = '';
Navigator.push(context, MaterialPageRoute(builder: (context) {
return word == '' ? RandomResults() : Results(word);
}));
}),
),
]),

但是结果是这样的: image

我曾尝试使用 MaterialButton 而不是 ElevatedButton 但它也被拖动以适应屏幕的宽度。

我想让按钮足够大以容纳其中的文本。

最佳答案

Center 包裹按钮(如果你想以不同的方式放置它,也可以用 Align 包裹按钮)

  @override
Widget build(BuildContext context) {
return ListView(
children: [
Center(
child: ElevatedButton(
onPressed: () {},
child: Text('Search!'),
),
),
],
);
}

关于flutter - 为什么 ElevatedButton 会被拉伸(stretch)以适应屏幕的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68510620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com