【问题标题】:Is there a way to put tabs inside a drawer in Flutter?有没有办法将标签放在 Flutter 的抽屉中?
【发布时间】:2020-12-12 15:41:01
【问题描述】:

我目前是 Flutter 的新手,正在为我正在尝试实现的功能寻求帮助。目前我的抽屉里有太多信息,所以我试图在抽屉里放标签,这样人们就可以在抽屉里访问不同类型的信息。我拍了一张照片来展示我脑海中的想法。 Drawer with Tab Pictures shown here。到目前为止,我已经尝试过 DefaultTabController,但无法让它工作,所以如果有人有任何其他想法或不同的处理方式,我将不胜感激。

提前致谢!

【问题讨论】:

    标签: android ios flutter flutter-layout flutter-animation


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    你可以用Drawer 包裹DefaultTabController
    代码sn-p

    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),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 2022-08-15
      • 2014-07-13
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多