【发布时间】:2019-12-27 06:20:57
【问题描述】:
我知道TextField 有TextStyle,它有一个height 属性,它只是基于fontSize 的乘数,但是我怎样才能使所有小部件的高度相同(不管字体大小) ?
此外,是否有以下等效方法(在几乎任何其他编程语言中):
btnLogin.height = txtPassword.height;
【问题讨论】:
标签: flutter dart flutter-layout
我知道TextField 有TextStyle,它有一个height 属性,它只是基于fontSize 的乘数,但是我怎样才能使所有小部件的高度相同(不管字体大小) ?
此外,是否有以下等效方法(在几乎任何其他编程语言中):
btnLogin.height = txtPassword.height;
【问题讨论】:
标签: flutter dart flutter-layout
输出:(都具有完全相同的高度)
我认为最好的方法是首先找出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"),
),
),
],
),
),
);
}
}
【讨论】:
addPostFrameCallback、LayoutBuilder、IntrinsicHeight 等类型的小部件没有任何好处。
为简单起见,您可以使用以下代码:
Container(
height: 210 , // Your fixed height*3 here (70*3=210)
width : double.infinity,
padding: EdgeInsets.symmetric(horizontal: 8.0), //Add padding as per your convenience
child : Column(
children: <Widget>[
Expanded(TextField([...])),
Expanded(TextField([...])),
Expanded(RaisedButton([...])),
],
),
)
您可以随意在小部件之间插入 Divider 小部件或 SizedBox,以根据您的要求提供更简洁的外观。
提示:与您在问题中描述问题的方式相比,Flutter 的方法略有不同,因此我建议您在继续之前查看更多 Flutter 的编码相关视频/博客。
【讨论】:
400 和 TextField 到 70 的高度不是一个干净的方法,您只是在这里对所有内容进行硬编码.