gpt4 book ai didi

flutter - 在Flutter中,如何使按钮和文本框高度相同?

转载 作者:IT王子 更新时间:2023-10-29 06:48:35 25 4
gpt4 key购买 nike

enter image description here

我知道 TextFieldTextStyle,它有一个 height 属性,它只是一个基于 fontSize ,但是我怎样才能使所有的小部件具有相同的高度(不考虑字体大小)?

此外,是否有以下等效方法(几乎在任何其他编程语言中):

btnLogin.height = txtPassword.height;

最佳答案

输出:(所有高度完全相同)

enter image description here


我认为最好的方法是先找出 TextField 的高度,然后将它用于您的 RaisedButton,这里是演示的完整示例代码相同的。

void main() => runApp(MaterialApp(home: HomePage()));

class HomePage extends StatefulWidget {
@override
State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
double _height = 56; // dummy height
GlobalKey _globalKey = GlobalKey();

@override
void initState() {
super.initState();
SchedulerBinding.instance.addPostFrameCallback((_) {
setState(() {
// height of the TextFormField is calculated here, and we call setState to assign this value to Button
_height = _globalKey.currentContext.size.height;
});
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
TextField(
key: _globalKey,
decoration: InputDecoration(hintText: "Email Adress"),
),
TextField(decoration: InputDecoration(hintText: "Password")),
SizedBox(height: 12),
SizedBox(
width: double.maxFinite,
height: _height, // this is the height of TextField
child: RaisedButton(
onPressed: () {},
child: Text("LOGIN TO MY ACCOUNT"),
),
),
],
),
),
);
}
}

关于flutter - 在Flutter中,如何使按钮和文本框高度相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57600637/

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