如何在Flutter Cupertino底部导航栏中导航到同级项

时间:2019-03-23 15:37:46

标签: flutter bottomnavigationview flutter-cupertino

我在Flutter中使用Cupertino设计,并在使用CupertinoTabBar。我想从不同的屏幕访问不同的标签项。

例如,我有2个项目:“主页”和“个人资料”。 CupertinoTabBar在主屏幕中定义。我想在HomeScreen中有一个按钮来访问ProfileScreen标签。在这种情况下如何导航?

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class MainScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MainScreenState();
  }
}

class _MainScreenState extends State<MainScreen> {

  @override
  Widget build(BuildContext context) {
    return Material(
      child: CupertinoTabScaffold(
        tabBar: CupertinoTabBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              title: Text("Home")
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.user),
              title: Text("My Appointments")
            )
          ],
        ),
        tabBuilder: (BuildContext context, int index) {
          switch (index) {
            case 0:
              return CupertinoTabView(
                builder: (BuildContext context) {
                  return HomeScreen();
                },
                defaultTitle: 'Home',
              );
              break;
            case 1:
              return CupertinoTabView(
                builder: (BuildContext context) => ProfileScreen(),
                defaultTitle: 'Profile',
              );
              break;
          }

          return null;
        },
      ),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return Container(
      child: CupertinoButton(
            child: Text("Check Profile"),
            onPressed: () {
              // Pop this page and Navigate to Profile page
            },
          )
    );
  }
}

class ProfileScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return Container(
      child: Text("Profile")
    );
  }
}

1 个答案:

答案 0 :(得分:0)

也许可以帮助您

int currentTabIndex = 0;

  onTapped(int index) {
    setState(() {
      currentTabIndex = index;
    });
  }


  List<Widget> tabs = [
    InicioPage(),
    OfertasPage(),
    FavoritosPage(),
    Login(),
  ];

 tabBuilder: (context, index) {
        switch (index) {
          case 0:
            return InicioPage();
            break;
          case 1:
            return OfertasPage();
            break;
          case 2:
            return FavoritosPage();
            break;
          case 3:
            return Login();
          default:
            return InicioPage();
            break;
        }
      }