【问题标题】:How can I implement single appbar icon to function differently on different tabbar?如何实现单个 appbar 图标以在不同的标签栏上以不同的方式运行?
【发布时间】:2020-04-01 15:49:41
【问题描述】:

我是flutter的新手,我正在尝试实现类似whatsApp的界面,在appBar的whatsapp右上角有垂直的3个点,显示不同选项列表根据所选标签。我想要类似的东西。尝试寻找 appBar 的构造函数,但无法找到解决方案如何开始。先感谢您。只需要提示即可开始。

【问题讨论】:

    标签: flutter


    【解决方案1】:
    import 'package:flutter/material.dart';
    
    class Example extends StatefulWidget {
      static const routeName = 'example';
      @override
      _ExampleState createState() => _ExampleState();
    }
    
    class _ExampleState extends State<Example> with SingleTickerProviderStateMixin {
      void _getCurrentTab() {
        if (_tabController.index == 0) {
          //execute code for first tab
    
        } else if (_tabController.index == 1) {
          //execute code for second tab
    
        } else {
          //execute code for third tab
        }
      }
    
      TabController _tabController;
    
      final List<Tab> tabs = [
        Tab(
          child: Icon(
            Icons.motorcycle,
          ),
        ),
        Tab(
          child: Icon(
            Icons.local_airport,
          ),
        ),
        Tab(
          child: Icon(
            Icons.local_taxi,
          ),
        ),
      ];
    
      @override
      void initState() {
        super.initState();
        _tabController = new TabController(vsync: this, length: tabs.length);
      }
    
      @override
      void dispose() {
        _tabController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return DefaultTabController(
          length: 3,
          initialIndex: 0,
          child: Scaffold(
            appBar: AppBar(
              title: Text('Title Goes Here'),
              centerTitle: true,
              actions: <Widget>[
                IconButton(
                  icon: Icon(Icons.more_horiz),
                  onPressed: () => _getCurrentTab(),
                )
              ],
              bottom: TabBar(
                controller: _tabController,
                tabs: tabs,
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 我仍然对上面的代码感到困惑,我有一个带有单个图标的 appBar,我想要的是,如果选择了第一个选项卡,图标会显示一些带有 A,B 的下拉菜单。如果选择了第二个选项卡,它会显示 C,D。
    • 对不起,误会了,请稍等
    • 好的,试试看是否更接近你想要的
    • 这就是我想要的。几乎接近。我可以实现休息。谢谢:-)
    猜你喜欢
    • 2017-09-04
    • 2021-06-24
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    相关资源
    最近更新 更多