有没有办法在 Flutter 中将选项卡放入抽屉内?
Is there a way to put tabs inside a drawer in Flutter?
我目前是 Flutter 的新手,正在为我正在尝试实现的功能寻求帮助。目前我的抽屉里有太多信息,所以我想在抽屉里放一些标签,这样人们就可以访问抽屉里的不同类型的信息。我用 Photoshop 处理了一张照片来展示我脑海中的想法。 Drawer with Tab Pictures shown here。到目前为止,我已经尝试了 DefaultTabController 但无法让它工作,所以如果有人有任何其他想法或不同的方法来处理它,我将非常感激。
提前致谢!
您可以复制粘贴 运行 下面的完整代码
您可以将 DefaultTabController
换成 Drawer
代码片段
return Scaffold(
...
drawer: Drawer(
child: DefaultTabController(
length: 3,
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
),
Container(
height: 50,
color: Colors.black12,
child: TabBar(
labelColor: Colors.deepOrange,
unselectedLabelColor: Colors.white,
tabs: [
Tab(text: "First"),
Tab(text: "Second"),
Tab(text: "Third"),
]),
),
Expanded(
child: Container(
color: Colors.green,
child: TabBarView(children: [
Container(
child: Text("First Body"),
),
Container(
child: Text("Second Body"),
),
Container(
child: Text("Third Body"),
),
]),
),
),
],
),
),
),
工作演示
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
drawer: Drawer(
child: DefaultTabController(
length: 3,
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
),
Container(
height: 50,
color: Colors.black12,
child: TabBar(
labelColor: Colors.deepOrange,
unselectedLabelColor: Colors.white,
tabs: [
Tab(text: "First"),
Tab(text: "Second"),
Tab(text: "Third"),
]),
),
Expanded(
child: Container(
color: Colors.green,
child: TabBarView(children: [
Container(
child: Text("First Body"),
),
Container(
child: Text("Second Body"),
),
Container(
child: Text("Third Body"),
),
]),
),
),
],
),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
我目前是 Flutter 的新手,正在为我正在尝试实现的功能寻求帮助。目前我的抽屉里有太多信息,所以我想在抽屉里放一些标签,这样人们就可以访问抽屉里的不同类型的信息。我用 Photoshop 处理了一张照片来展示我脑海中的想法。 Drawer with Tab Pictures shown here。到目前为止,我已经尝试了 DefaultTabController 但无法让它工作,所以如果有人有任何其他想法或不同的方法来处理它,我将非常感激。
提前致谢!
您可以复制粘贴 运行 下面的完整代码
您可以将 DefaultTabController
换成 Drawer
代码片段
return Scaffold(
...
drawer: Drawer(
child: DefaultTabController(
length: 3,
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
),
Container(
height: 50,
color: Colors.black12,
child: TabBar(
labelColor: Colors.deepOrange,
unselectedLabelColor: Colors.white,
tabs: [
Tab(text: "First"),
Tab(text: "Second"),
Tab(text: "Third"),
]),
),
Expanded(
child: Container(
color: Colors.green,
child: TabBarView(children: [
Container(
child: Text("First Body"),
),
Container(
child: Text("Second Body"),
),
Container(
child: Text("Third Body"),
),
]),
),
),
],
),
),
),
工作演示
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
drawer: Drawer(
child: DefaultTabController(
length: 3,
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
),
Container(
height: 50,
color: Colors.black12,
child: TabBar(
labelColor: Colors.deepOrange,
unselectedLabelColor: Colors.white,
tabs: [
Tab(text: "First"),
Tab(text: "Second"),
Tab(text: "Third"),
]),
),
Expanded(
child: Container(
color: Colors.green,
child: TabBarView(children: [
Container(
child: Text("First Body"),
),
Container(
child: Text("Second Body"),
),
Container(
child: Text("Third Body"),
),
]),
),
),
],
),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}