DOG*_*hal 4 dart segmentedcontrol flutter
我正在尝试使用CupertinoSegmentedControl来自 flutter Cupertino 库的AppBarusing the bottom 属性来实现以下设计(高度 = 32)
所以我尝试了以下方法:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 2,
backgroundColor: Colors.white,
centerTitle: true,
title: Text(this.widget.title, style: TextStyle(color: Colors.black)),
bottom: PreferredSize(
child: Padding(
padding: const EdgeInsets.only(top: 8, bottom: 12),
child: Row(
children: <Widget>[
SizedBox(width: 24),
Expanded(
child: CupertinoSegmentedControl(
children: this.widget.tabs,
groupValue: this._selectedTab,
onValueChanged: (value) {
this.setState(() => this._selectedTab = value);
this._tabController.animateTo(value);
}
),
),
SizedBox(width: 24)
],
),
),
preferredSize: Size(double.infinity, 48)
)
),
body: new TabBarView(
controller: this._tabController,
children: this.widget.views,
));
}
Run Code Online (Sandbox Code Playgroud)
类似的东西是否与您想要的布局相似?(当然去掉绿色^_^)
Play around with the Container and PreferredSize heights to adjust the height to fit your needs.
Scaffold(
appBar: AppBar(
elevation: 2,
backgroundColor: Colors.white,
centerTitle: true,
title:
Text(this.widget.title, style: TextStyle(color: Colors.black)),
bottom: PreferredSize(
child: Row(
children: [
Expanded(
child: Container(
height: 48,
color: Colors.lightGreenAccent,
child: CupertinoSegmentedControl(
children: children,
groupValue: this._selectedTab,
onValueChanged: (value) {
this.setState(() => this._selectedTab = value);
}),
),
)
],
),
preferredSize: Size(double.infinity, 48))),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('hello')
]
)
)
);
Run Code Online (Sandbox Code Playgroud)
UPDATE:
As kazimad pointed out, if you want to increase the segmented control height and not only add padding to it insiede the app bar, you can add a Padding widget to your tabs, like that:
Scaffold(
appBar: AppBar(
elevation: 2,
backgroundColor: Colors.white,
centerTitle: true,
title:
Text(this.widget.title, style: TextStyle(color: Colors.black)),
bottom: PreferredSize(
child: Row(
children: [
Expanded(
child: Container(
height: 48,
color: Colors.lightGreenAccent,
child: CupertinoSegmentedControl(
children: children,
groupValue: this._selectedTab,
onValueChanged: (value) {
this.setState(() => this._selectedTab = value);
}),
),
)
],
),
preferredSize: Size(double.infinity, 48))),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('hello')
]
)
)
);
Run Code Online (Sandbox Code Playgroud)
只需将 Padding 小部件或 margin 属性添加到 Container 并将您的小部件包装在“tabs”集合中(在您的情况下是 this.widget.tabs)
就我而言
CupertinoSegmentedControl<int>(
children: segmentTextWidgets,
...
),
final Map<int, Widget> segmentTextWidgets = <int, Widget>{
0: Container(
margin: const EdgeInsets.symmetric(vertical: 16),
child: Text("Tab 1 title"),
),
1: Container(
margin: const EdgeInsets.symmetric(vertical: 16),
child: Text("Tab 2 title"),
),
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6047 次 |
| 最近记录: |