Cha*_*ata 31 dart material-design flutter
我怎样才能简单地设置AppBar
Flutter 的高度?
酒吧的标题应该垂直居中(在那里AppBar
).
Cin*_*inn 72
您可以使用PreferredSize:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
home: Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50.0), // here the desired height
child: AppBar(
// ...
)
),
body: // ...
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
Cop*_*oad 19
toolbarHeight
:不再需要使用PreferredSize
. toolbarHeight
与 一起使用flexibleSpace
。
AppBar(
toolbarHeight: 120, // Set this height
flexibleSpace: Container(
color: Colors.orange,
child: Column(
children: [
Text('1'),
Text('2'),
Text('3'),
Text('4'),
],
),
),
)
Run Code Online (Sandbox Code Playgroud)
foo*_*ist 14
您可以使用PreferredSize
和flexibleSpace
:
appBar: PreferredSize(
preferredSize: Size.fromHeight(100.0),
child: AppBar(
automaticallyImplyLeading: false, // hides leading widget
flexibleSpace: SomeWidget(),
)
),
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以保持elevation
of AppBar
的可见,并具有自定义的高度,这正是我想要的。不过,您必须在中设置间距SomeWidget
。
Pav*_*vel 12
除了@Cinn 的回答,你还可以定义一个这样的类
class MyAppBar extends AppBar with PreferredSizeWidget {
@override
get preferredSize => Size.fromHeight(50);
MyAppBar({Key key, Widget title}) : super(
key: key,
title: title,
// maybe other AppBar properties
);
}
Run Code Online (Sandbox Code Playgroud)
或者这样
class MyAppBar extends PreferredSize {
MyAppBar({Key key, Widget title}) : super(
key: key,
preferredSize: Size.fromHeight(50),
child: AppBar(
title: title,
// maybe other AppBar properties
),
);
}
Run Code Online (Sandbox Code Playgroud)
然后用它代替标准的
Oma*_*mar 12
您可以简单地使用toolbarHeight,如下所示:
appBar: AppBar(
toolbarHeight: 70.0, // add this line
centerTitle: true, // add this line
title: Text('your title'),
),
Run Code Online (Sandbox Code Playgroud)
但如果您有任何操作,上面的代码不能按您想要的方式工作,您可以使用此代码
AppBar(
centerTitle: true,
title: Padding(
padding: const EdgeInsets.all(16.0),
child: Stack(
alignment: Alignment.center,
children: [
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Chats', style: TextStyle(color:Colors.black),),
Icon(Icons.add, color: Colors.black,),
],
),
Align(
alignment: Alignment.centerRight,
child: Icon(Icons.add, color: Colors.black,),
),
],
),
),
)
Run Code Online (Sandbox Code Playgroud)
在撰写本文时,我尚未意识到PreferredSize
。Cinn的答案更好地实现了这一目标。
您可以创建一个具有自定义高度的自定义小部件:
import "package:flutter/material.dart";
class Page extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Column(children : <Widget>[new CustomAppBar("Custom App Bar"), new Container()],);
}
}
class CustomAppBar extends StatelessWidget {
final String title;
final double barHeight = 50.0; // change this for different heights
CustomAppBar(this.title);
@override
Widget build(BuildContext context) {
final double statusbarHeight = MediaQuery
.of(context)
.padding
.top;
return new Container(
padding: new EdgeInsets.only(top: statusbarHeight),
height: statusbarHeight + barHeight,
child: new Center(
child: new Text(
title,
style: new TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
toolbarHeight
在 AppBar 中使用属性例子 :
AppBar(
title: Text('Flutter is great'),
toolbarHeight: 100,
),
Run Code Online (Sandbox Code Playgroud)
您可以
flexibleSpace
在 appBar 中添加属性以获得更大的灵活性
输出:
示例:
appBar: PreferredSize(
preferredSize: Size(100, 80), //width and height
// The size the AppBar would prefer if there were no other constraints.
child: SafeArea(
child: Container(
height: 100,
color: Colors.red,
child: Center(child: Text('Fluter is great')),
),
),
),
Run Code Online (Sandbox Code Playgroud)
SafeArea
如果您没有安全区域,请不要忘记使用小部件
输出 :
Cinn 的回答很好,但它有一个问题。
该PreferredSize
控件将在屏幕顶部立即启动,而不考虑状态栏,所以它的一些高度将通过状态栏的高度阴影。这也说明了侧凹口。
解决方案:preferredSize
用一个包裹 the的孩子SafeArea
appBar: PreferredSize(
//Here is the preferred height.
preferredSize: Size.fromHeight(50.0),
child: SafeArea(
child: AppBar(
flexibleSpace: ...
),
),
),
Run Code Online (Sandbox Code Playgroud)
如果您不想使用 flexibleSpace 属性,则不需要所有这些,因为AppBar
will的其他属性会自动说明状态栏。
小智 5
只需使用工具栏高度...
AppBar(
title: Text("NASHIR'S APP"),
toolbarHeight: 100,
),
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30223 次 |
最近记录: |