flutter - floatingactionbutton 在不同的选项卡上显示/隐藏

在 flutter 中,我有 2 个显示不同 View 的标签页。我想让 float 操作按钮仅显示在一个 View 中并隐藏在其他选项卡中。 但是即使切换 View , float 操作按钮也会保持 float 。 谁可以帮我这个事?如果有任何代码或教程,将不胜感激。

最佳答案

演示图片:
tab1 show fab
tab2 hide fab

_selectedIndex == 0时你可以设置floatingActionButton为null,这样FAB就没有动画了,太酷了。并记住在 BottomNavigationBar onTap 方法中更改 _selectedIndex

这是带有一些注释的示例代码:

final _tabTitle = [
  'Title 1',
  'Title 2'
]; // BottomNavigationBarItem title

final _tabIcon = [
  Icon(Icons.timer_off),
  Icon(Icons.timeline),
]; // BottomNavigationBarItem icon

class _MyHomePageState extends State<MyHomePage> {

  int _selectedIndex = 0;
  String title = _tabTitle[0];

  // tap BottomNavigationBar will invoke this method
  _onItemTapped(int index) {
    setState(() {
      // change _selectedIndex, fab will show or hide
      _selectedIndex = index;      
      // change app bar title     
      title = _tabTitle[index];
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(title),
      ),
      body: Center(
        child: Text(_tabTitle[_selectedIndex]),
      ),
      // two tabs
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(title: Text(_tabTitle[0]), icon: _tabIcon[0]),
          BottomNavigationBarItem(title: Text(_tabTitle[1]), icon: _tabIcon[1])
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
      ),
      // key point, fab will show in Tab 0, and will hide in others.
      floatingActionButton: _selectedIndex == 0 ? FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.add),
      ) : null,
    );
  }
}

https://stackoverflow.com/questions/49339716/

相关文章:

flutter - 如何定期设置状态?

dart - 如何使TextField的背景为矩形框形状?

dart - 格式化持续时间,如 HH :mm:ss

dart - Flutter 中的透明 PageRoute 用于显示(半)透明页面

dart - Dart 中有 Goto 函数吗?

dart - Flutter Web 不显示 Material Design 图标

flutter - 如何在 flutter 的盒子周围创建虚线边框?

dictionary - 不断向 map 添加数据

image - 如何在 Dart/Flutter 中读取本地镜像文件的字节数?

dart - 用于 Flutter/Dart 的 Google Sheets API v4