简体   繁体   English

Flutter,如何为底部导航栏使用 switch case?

[英]Flutter, how to use switch case for bottomnavigationbar?

Im a beginner in flutter, and I will like to use switch case to route to another pages, I did try to do it but it doesn't work, can anyone tell me what did I done wrong???我是颤振的初学者,我想使用 switch case 路由到另一个页面,我确实尝试过但它不起作用,谁能告诉我我做错了什么???

Hope someone can solve my problem Hope someone can solve my problem Hope someone can solve my problem Hope someone can solve my problem Hope someone can solve my problem Hope someone can solve my problem希望有人能解决我的问题 希望有人能解决我的问题 希望有人能解决我的问题 希望有人能解决我的问题 希望有人能解决我的问题

class BottomNavigationEzyMember extends StatefulWidget {
  const BottomNavigationEzyMember({Key key}) : super(key: key);

  @override
  _BottomNavigationEzyMemberState createState() =>
      _BottomNavigationEzyMemberState();
}

class _BottomNavigationEzyMemberState extends State<BottomNavigationEzyMember> {
  int _selectedIndex = 0;

  var bottomTextStyle =
      GoogleFonts.inter(fontSize: 12, fontWeight: FontWeight.w500);

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

@override
  Widget build(BuildContext context) {
        (index) {
      switch (_selectedIndex) {
        case 0:
          Navigator.of(context).pushNamed('/homepage');
          break;
        case 1:
          Navigator.of(context).pushNamed('/cardpage');
          break;
      }
    };
    return Container(
      height: 64,
      decoration: BoxDecoration(
        color: mFillColor,
        boxShadow: [
          BoxShadow(
              color: Colors.grey.withOpacity(0.3),
              spreadRadius: 2,
              blurRadius: 15,
              offset: const Offset(0, 5))
        ],
        borderRadius: const BorderRadius.only(
          topLeft: Radius.circular(24),
          topRight: Radius.circular(24),
        ),
      ),
      child: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: _selectedIndex == 0
                ? SvgPicture.asset('assets/icons/home_colored.svg')
                : SvgPicture.asset('assets/icons/home.svg'),
            title: Text('Home', style: bottomTextStyle),
          ),
          BottomNavigationBarItem(
            icon: _selectedIndex == 1
                ? SvgPicture.asset('assets/icons/order_colored.svg')
                : SvgPicture.asset('assets/icons/order.svg'),
            title: Text(
              'My Card',
              style: bottomTextStyle,
            ),
          ),
          BottomNavigationBarItem(
            icon: _selectedIndex == 2
                ? SvgPicture.asset('assets/icons/watch_colored.svg')
                : SvgPicture.asset('assets/icons/watch.svg'),
            title: Text(
              'Watch List',
              style: bottomTextStyle,
            ),
          ),
          BottomNavigationBarItem(
            icon: _selectedIndex == 3
                ? SvgPicture.asset('assets/icons/account_colored.svg')
                : SvgPicture.asset('assets/icons/account.svg'),
            title: Text(
              'Account',
              style: bottomTextStyle,
            ),
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: mBlueColor,
        unselectedItemColor: mSubtitleColor,
        onTap: _onItemTapped,
        backgroundColor: Colors.transparent,
        type: BottomNavigationBarType.fixed,
        selectedFontSize: 12,
        showUnselectedLabels: true,
        elevation: 0,
      ),
    );
  }
}

Remove switch statement from under buildContext ;删除buildContext下的 switch 语句; you will use switch statement under _onItemTapped .您将在_onItemTapped下使用 switch 语句。

  void _onItemTapped(int index) {
    setState(() {
      print("working");      
      _selectedIndex = index;
      switch (_selectedIndex) {
        case 0:
          Navigator.of(context).pushNamed('/homepage');
          break;
        case 1:
          Navigator.of(context).pushNamed('/cardpage');
          break;
      }

    });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM