簡體   English   中英

如何通過傳遞選定的索引在 flutter 底部導航欄上打開屏幕

[英]How to open a screen on flutter bottom navigation bar by passing selected index

我的底部導航欄中有三個屏幕。 一個屏幕包含到另一個屏幕的按鈕。 通常,當我單擊按鈕時,我希望它在維護腳手架的同時打開相應的屏幕。 這就是我進行的方式:我在底部導航中添加了一個變量,以便當我可以在路線導航器中傳遞一個索引以打開該特定屏幕但我遇到錯誤時

這是我的代碼


    import 'dart:ui';
    import 'package:anime_quiz/components/appbar.dart';
    import 'package:anime_quiz/components/quizDrawer.dart';
    import 'package:anime_quiz/screens/newsScreen/NewsScreen.dart';
    import 'package:anime_quiz/screens/homeScreen/homeScreen.dart';
    import 'package:anime_quiz/screens/quizScreens/quizScreen.dart';
    import 'package:anime_quiz/utilities/constants.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_svg/flutter_svg.dart';
    
    final _scaffoldKey = GlobalKey<ScaffoldState>();
    
    class LayOutScreen extends StatefulWidget {
      static const String id = "layOutScreen";
      final int selectedIndex;
      LayOutScreen({this.selectedIndex});
    
      @override
      _LayOutScreenState createState() => _LayOutScreenState();
    }
    
    class _LayOutScreenState extends State<LayOutScreen> {
      
      int _selectedIndex = 1;
      String _title;
    
      @override
      initState(){
        super.initState();
        _selectedIndex = widget.selectedIndex;
        _title = 'Home';
      }
    
      void _onItemTapped(int index) {
        setState(() {
          _selectedIndex = index;
          switch(index) {
            case 0: { _title = 'Anime Quiz'; }
            break;
            case 1: { _title = 'Home'; }
            break;
            case 2: { _title = 'Anime News'; }
            break;
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          key: _scaffoldKey,
          resizeToAvoidBottomInset: false,
          resizeToAvoidBottomPadding: false,
          appBar: PreferredSize(
            preferredSize: Size.fromHeight(kH50),
            child: AppBarAQ(
              title: _title,
              onPress: () => _scaffoldKey.currentState.openDrawer(),
              leadIcon: 'assets/icons/icons8-naruto-50.png',
              actionIcon: null,
            ),
          ),
          bottomNavigationBar: SizedBox(
    
            height: 52,
            child: Container(
            height: 40,
              child: BottomNavigationBar(
                backgroundColor: kWhiteColor,
                unselectedItemColor: kGreyColor,
                selectedIconTheme: IconThemeData(),
                // showUnselectedLabels: false,
                items: <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
                    icon: SvgPicture.asset(
                        'assets/icons/shuriken2.svg',
                        width: kH20),
                    label: 'Quiz',
                  ),
                  BottomNavigationBarItem(
                    icon: SvgPicture.asset(
                        'assets/icons/icons8-naruto-sign (2).svg',
                        width: kH20),
                    label: 'Home',
                  ),
                  BottomNavigationBarItem(
                    icon: SvgPicture.asset(
                        'assets/icons/scroll2.svg',
                        width: kH20),
                    label: 'News',
                  ),
                ],
                currentIndex: _selectedIndex,
                selectedItemColor: kPrimaryColor,
                onTap: _onItemTapped,
              ),
            ),
          ),
          drawer: QuizDrawer(),
    
          body: IndexedStack(
            index: _selectedIndex,
            children: [
              QuizScreen(),
              HomeScreen(),
              NewsScreen(),
            ],
          ),
        );
      }
    }

這是我如何使用它


 SimpleCard(
     title1: 'ANIME',
     title2: 'QUIZ',
     onTap: () {Navigator.push(
       context,
       MaterialPageRoute(
            builder: (context) =>
                LayOutScreen(selectedIndex: 0)
       )
    );},
  ),

我得到這個錯誤

[我得到了什么] [1]: https://i.stack.imgur.com/pnuzf.png

這是控制台

我的控制台

試試這樣

  1. 聲明一個最終的 int currentIndex;
  2. 為您的 class yourclass({ this.currentIndex = 0 }); 聲明一個構造函數; 0 是第一頁
  3. 像 yourclass(currentIndex: 0)) 一樣導航;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM