Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在颤振中使用底部导航项切换选项卡栏的可见性_Flutter_Tabbar - Fatal编程技术网

Flutter 如何在颤振中使用底部导航项切换选项卡栏的可见性

Flutter 如何在颤振中使用底部导航项切换选项卡栏的可见性,flutter,tabbar,Flutter,Tabbar,我的flatter应用程序中有一个bottomNavigationBar和一个AppBar。AppBar底部是一个由两项组成的选项卡栏。因此,我希望单击底部导航栏的某些项时,选项卡栏不可见。我试图使用布尔变量将可见性类分配给我的选项卡,但它不起作用。似乎我无法单独处理TabBar小部件 如何解决这个问题 class DashBoardPage extends StatefulWidget { @override _DashBoardPageState createState()

我的
flatter
应用程序中有一个
bottomNavigationBar
和一个
AppBar
。AppBar底部是一个由两项组成的
选项卡栏
。因此,我希望单击
底部导航栏
的某些项时,
选项卡栏
不可见。我试图使用
布尔变量将可见性类分配给我的
选项卡
,但它不起作用。似乎我无法单独处理
TabBar
小部件

如何解决这个问题

     class DashBoardPage extends StatefulWidget {
  @override
  _DashBoardPageState createState() => _DashBoardPageState();
}

class _DashBoardPageState extends State<DashBoardPage> {
  SharedPreferences sharedPreferences;
  bool showTabs = false;
  int tabsIndex = 0;
  int _currentIndex = 0;
  String _appBarText = "Welcome, User";

  Widget callPage(int currentIndex) {
    switch (currentIndex) {
      case 0:
        showTabs = true;
        _appBarText = "Welcome, User";
          return TabBarView(
           children:[
             new HomePage(),
             new SchedulePage()
           ]
          );
         break;
      case 1:
        showTabs = false;
        break;
      case 2:
        showTabs = false;
        break;
      default:
        return HomePage();
    }
  }

  @override
  void initState() {
    super.initState();
    checkLoginState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MAF Mentor',
      debugShowCheckedModeBanner: false,
      home: DefaultTabController(
        length: choices.length,
        child: Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xFFFFFFFF),
          title: Text(
            _appBarText,
            style: TextStyle(
              color: Color(0xFF1C2447),
              fontFamily: 'Muli',
              fontSize: 16.0,
            ),
          ),
          bottom:  showTabs? TabBar(
            isScrollable: true,
            tabs: choices.map<Widget>((Choice choice) {
              return Tab(
                text: choice.title,
                icon: Icon(choice.icon),
              );
            }).toList(),
            labelColor: Color(0xFF1C2447),
          ):null,
          actions: <Widget>[
            IconButton(
              icon: Icon(
                Icons.account_circle,
                color: Color(0xFF1C2447),
              ),
              onPressed: () {
                Navigator.of(context).pushNamed('/profile_page');
              },
            ),
            IconButton(
              icon: Icon(
                Icons.notifications,
                color: Color(0xFF1C2447),
              ),
              onPressed: () {
                // do something
              },
            ),
          ],
        ), //AppBar
        body: callPage(_currentIndex),
        bottomNavigationBar: BottomNavigationBar(
          showSelectedLabels: false,
          showUnselectedLabels: false,
          fixedColor: Color(0xFF1C2447),
          currentIndex: _currentIndex,
          onTap: (value) {
            _currentIndex = value;
            callPage(_currentIndex);
            setState(() {

            });
          },
          items: [
            BottomNavigationBarItem(
                icon: Icon(Icons.home), title: Text("Bar 1")),
            BottomNavigationBarItem(
                icon: Icon(Icons.people), title: Text("Bar 2")),
            BottomNavigationBarItem(
                icon: Icon(Icons.history), title: Text("Bar 3"))
          ],
        ),
      ),
      ),
    );
  }
class DashBoardPage扩展StatefulWidget{
@凌驾
_DashBoardPageState createState()=>DashBoardPageState();
}
类_DashBoardPageState扩展状态{
SharedReferences SharedReferences;
bool showtab=false;
int-tabsIndex=0;
int _currentIndex=0;
字符串_appBarText=“欢迎,用户”;
小部件调用页(int currentIndex){
开关(当前索引){
案例0:
showtab=true;
_appbartex=“欢迎,用户”;
返回选项卡视图(
儿童:[
新网页(),
新计划页()
]
);
打破
案例1:
showtab=false;
打破
案例2:
showtab=false;
打破
违约:
返回主页();
}
}
@凌驾
void initState(){
super.initState();
checkLoginState();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“MAF导师”,
debugShowCheckedModeBanner:false,
主页:DefaultTabController(
长度:选择。长度,
孩子:脚手架(
appBar:appBar(
背景颜色:颜色(0xFFFFFF),
标题:正文(
_appBarText,
样式:TextStyle(
颜色:颜色(0xFF1C2447),
fontFamily:“Muli”,
字体大小:16.0,
),
),
底部:显示选项卡?选项卡栏(
isScrollable:是的,
选项卡:选项.map((选项){
返回选项卡(
text:choice.title,
图标:图标(choice.icon),
);
}).toList(),
labelColor:颜色(0xFF1C2447),
):null,
行动:[
图标按钮(
图标:图标(
Icons.account_圈,
颜色:颜色(0xFF1C2447),
),
已按下:(){
Navigator.of(context.pushNamed('/profile_page');
},
),
图标按钮(
图标:图标(
图标、通知、,
颜色:颜色(0xFF1C2447),
),
已按下:(){
//做点什么
},
),
],
),//AppBar
正文:调用页(_currentIndex),
底部导航栏:底部导航栏(
showSelectedLabels:false,
显示未选择的标签:false,
固定颜色:颜色(0xFF1C2447),
currentIndex:_currentIndex,
onTap:(值){
_当前索引=值;
调用页(_currentIndex);
设置状态(){
});
},
项目:[
底部导航气压计(
图标:图标(Icons.home),标题:文本(“条1”),
底部导航气压计(
图标:图标(Icons.people),标题:文本(“条2”),
底部导航气压计(
图标:图标(图标。历史记录),标题:文本(“第3栏”))
],
),
),
),
);
}

底部
需要一个
首选大小边条
,所以你不能在那里使用
可见性
小部件。你可以使用一个布尔变量来实现这一点。你可以看到下面的全部代码。因为我不知道你的选择和选项卡,所以我会随机放置一些东西。但是,如果你想在用户点击时显示
选项卡
BottomNavigationBarItem
第一,只需将布尔变量更新为true,否则将其设置为false

class TabBarExample extends StatefulWidget {
  @override
  _TabBarExampleState createState() => _TabBarExampleState();
}

class _TabBarExampleState extends State<TabBarExample> {
  bool showTabs = false;
  int selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xFFFFFFFF),
          title: Text(
            '_appBarText',
            style: TextStyle(
              color: Color(0xFF1C2447),
              fontFamily: 'Muli',
              fontSize: 16.0,
            ),
          ),
          bottom: showTabs
              ? TabBar(
                  isScrollable: true,
                  tabs: <Widget>[
                    Tab(
                      text: 'Choice1',
                      icon: Icon(Icons.add_circle_outline),
                    ),
                    Tab(
                      text: 'Choice1',
                      icon: Icon(Icons.add_circle),
                    ),
                  ],
                  labelColor: Color(0xFF1C2447),
                )
              : null,
        ),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: selectedIndex,
          items: [
            BottomNavigationBarItem(
                icon: Icon(Icons.home), title: Text('first')),
            BottomNavigationBarItem(
                icon: Icon(Icons.favorite), title: Text('second')),
          ],
          onTap: (index) {
            if (index == 1) {
              setState(() => showTabs = true);
            } else {
              setState(() => showTabs = false);
            }
            setState(() => selectedIndex = index);
          },
        ),
      ),
    );
  }
}
class TabBarExample扩展StatefulWidget{
@凌驾
_TabBarExampleState createState()=>TabBarExampleState();
}
类\u tabbareExampleState扩展状态{
bool showtab=false;
int selectedIndex=0;
@凌驾
小部件构建(构建上下文){
返回DefaultTabController(
长度:2,
孩子:脚手架(
appBar:appBar(
背景颜色:颜色(0xFFFFFF),
标题:正文(
"appbartex",,
样式:TextStyle(
颜色:颜色(0xFF1C2447),
fontFamily:“Muli”,
字体大小:16.0,
),
),
底部:显示选项卡
?TabBar(
isScrollable:是的,
选项卡:[
标签(
文本:“选择1”,
图标:图标(图标。添加圆圈和轮廓),
),
标签(
文本:“选择1”,
图标:图标(图标。添加圆圈),
),
],
labelColor:颜色(0xFF1C2447),
)
:null,
),
底部导航栏:底部导航栏(
currentIndex:selectedIndex,
项目:[
底部导航气压计(
图标:图标(Icons.home),标题:文本('first'),
底部导航气压计(
图标:图标(Icons.favorite),标题:文本('second'),
],
onTap:(索引){
如果(索引==1){
设置状态(()=>showTabs=true);
}否则{
设置状态(()=>showTabs=false);
}
设置状态(()=>selectedIndex=索引);
},
),
),
);
}
}

底部
需要一个
首选大小边条
,因此您不能使用
可见性