我正在尝试使用 DefaultTabController
在一些小部件的中间。所以我的 TabBar
不能在 AppBar
并且必须关闭一些小部件。
所以我的问题是当我使用 TabBarView
时它崩溃了...
所以这里有一个 Flutter 示例,但没有找到如何在没有 Scaffold
的情况下做到这一点.
final List<Tab> myTabs = <Tab>[
Tab(text: 'LEFT'),
Tab(text: 'RIGHT')];
代码
DefaultTabController(
length: myTabs.length,
child: Scaffold(
appBar: TabBar(
tabs: myTabs,
),
body: TabBarView(
children: myTabs.map((Tab tab) {
final String label = tab.text.toLowerCase();
return Center(
child: Text(
'This is the $label tab',
style: const TextStyle(fontSize: 36),
),
);
}).toList(),
),
),
);
这是我应该做的 TabBar 的另一个示例 image
实码
class ProfileTabBarNavigation extends StatelessWidget {
final List<Tab> myTabs = <Tab>[
const Tab(text: kArtwork),
const Tab(text: kPastJobs)];
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
initialIndex: 0,
child: Padding(
padding: kPaddingTabBar,
child: Container(
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
color: kLightGrey,
borderRadius: BorderRadius.all(
Radius.circular(50),
),
),
child: Column(children: <Widget>[
TabBar(
tabs: myTabs,
unselectedLabelColor: Colors.black54,
labelColor: Colors.black,
unselectedLabelStyle: kBoldText,
labelStyle: kBoldText,
indicatorSize: TabBarIndicatorSize.tab,
indicator: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(50),
color: Colors.white,
),
),
TabBarView(
children: myTabs.map((Tab tab) {
final String label = tab.text.toLowerCase();
return Center(
child: Text(
'This is the $label tab',
style: const TextStyle(fontSize: 36),
),
);
}).toList(),
),
]),
),
),
);
}
}
最佳答案
关于flutter - 没有脚手架的 DefaultTabController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60362159/