-1

我正在开发一个具有超过 5 个屏幕的颤振应用程序,我想处理 android 的后退按钮,例如,当我登录应用程序时,它会显示仪表板屏幕,所以当我移动到配置文件然后移动到历史屏幕,当我点击历史屏幕上的后退按钮时,它应该导航到个人资料屏幕,因为我在历史屏幕之前访问的最后一个屏幕是个人资料,但它显示了登录屏幕的第一个屏幕。

我找到了解决方案,当我单击后退按钮时它会关闭应用程序。

更新:

我的屏幕从抽屉和底部导航导航,只有登录屏幕,我使用登录按钮并调用仪表板屏幕 onpressed 功能,除此之外,任何屏幕上都没有导航到其他屏幕的按钮。这是抽屉和底部导航的代码。

这是我在登录按钮上使用的代码行

 Navigator.push(
              context, new MaterialPageRoute(builder: (context) => Employee()));

抽屉和底部导航代码: 代码:

class Employee extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: new ThemeData(
        primaryColor: Colors.blue
      ),
      home: EmployeeNavigation(),
    );
  }
}
  int _selectedTab = 0;
  final _pageOptions = [
    EmployeeDashboard(),
    location(),
    Profile()
  ];

  String getname="";
  String getemail="";
  String getdesignation="";
  String getaccesstoken="";   
  String getdate;
  String getTime;

// ignore: must_be_immutable
class EmployeeNavigation extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return EmployeeNavigationState();
  }
}
class EmployeeNavigationState extends State<EmployeeNavigation> {
  
  var email;
  var designation;
  var date;
  bool valuefirst = false;
  String backtext="";

  @override
  Widget build(BuildContext context) {
   //i used this too but it doesn't work.
    return WillPopScope(
      onWillPop: () async {
        if (_selectedTab == 0) {
          return true;
        }
        setState(() {
          _selectedTab = 0;
        });
        return false;
      },
      
    child:Scaffold(    
    drawer:Emp_DrawerCode(),
    body:  _pageOptions[_selectedTab], 
    bottomNavigationBar: BottomNavigationBar(
     backgroundColor: Colors.blue[50],
    type: BottomNavigationBarType.fixed,
    currentIndex: _selectedTab,
    onTap: (value) {
      print(value);
      setState(() {
        _selectedTab = value;
      });
    },
    items: [
      BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
      BottomNavigationBarItem(icon: Icon(Icons.location_on), label: "Location"),
      BottomNavigationBarItem(
        icon: Icon(Icons.person),
        label: "Profile",
      ),
    ],
  )));
  
}
}



class Emp_DrawerCode extends StatefulWidget {
  @override
  _Emp_DrawerCodeState createState() => _Emp_DrawerCodeState();
}

class _Emp_DrawerCodeState extends State<Emp_DrawerCode> {

  SharedPreferences myPrefs;
  name() async{
     myPrefs=await SharedPreferences.getInstance();
    setState(() {
          getname=myPrefs.getString('name');
          getemail=myPrefs.getString('email');
          getdesignation=myPrefs.getString('designation');
   
        });
    
  }
  void initState(){
    name();
  }

  @override
  Widget build(BuildContext context) {
    return new Drawer(
      child: new ListView(
          padding: const EdgeInsets.all(0.0),
          children: <Widget>[
            
            new UserAccountsDrawerHeader(
                accountName: new Text(getname),
                accountEmail: new Text(getemail),
                currentAccountPicture: new CircleAvatar(
                  backgroundColor:
                  Theme.of(context).platform == TargetPlatform.android
                      ? Colors.white
                      : Colors.blue,
              child: Text(
                getname[0][0],
                style: TextStyle(fontSize: 40.0),
              ),
            ),
          ),
          new ListTile(
              title: new Text('Home'),
              leading: Icon(Icons.dashboard,color:Colors.grey),
              onTap: (){
                Navigator.pop(context);
                  Navigator.push(context, new MaterialPageRoute(
                    builder: (context)=> EmployeeNavigation()
                    )
                  );
              },
            ),
            new ListTile(
              title: new Text('Request for leave'),
              leading: Icon(Icons.request_page,color:Colors.grey),
              onTap: (){
                Navigator.pop(context);
                  Navigator.push(context, new MaterialPageRoute(
                    builder: (context)=>RequestForLeave()
                    )
                  );
              },
            ),
            new ExpansionTile(
              title: new Text('History'),
              children: <Widget>[
                ListTile(
                   title:new Text("My Attendance"),
                  leading: Icon(Icons.assessment_outlined ,color:Colors.grey),
                  onTap: (){
                Navigator.pop(context);
                  Navigator.push(context, new MaterialPageRoute(
                    builder: (context)=>new MyAttendance()
                    )
                  );
              },
                 
                ),
                ListTile(
                   title:new Text("Leaves"),
                  leading: Icon(Icons.assessment_outlined,color:Colors.grey ),
                  onTap: (){
                Navigator.pop(context);
                  Navigator.push(context, new MaterialPageRoute(
                    builder: (context)=>new LeaveHistory()
                    )
                  );
              },
                 
                ),
              ],
              leading: Icon(Icons.history,),
            
            ),
           
            new ListTile(
              title: new Text('Log out'),
              leading: Icon(Icons.logout,color:Colors.grey),
              onTap: (){
                myPrefs.setBool('login', true);
                Navigator.pop(context);
                  Navigator.push(context, new MaterialPageRoute(
                    builder: (context)=>
                    
                    Login()
                    )
                  );
              },
            ),
            
            ],
            
        ),
        

      
    );
  }
}

请帮助如何做到这一点。

4

3 回答 3

0

您需要先进一步澄清您的问题。

正在使用页面视图/您是在谈论所有都是单独可导航屏幕或某些不同场景的场景?

当所有屏幕都是单独导航的屏幕时,我将其视为第二种情况。

在这种情况下,每次用户导航到下一个屏幕时,您必须使用Navigator.pushNamed() / Navigator.push(),因为现在我认为您正在使用Navigator.pushReplacement()这可能会导致此问题。

Navigator只不过是一个知道内存中屏幕堆栈的类,它为我们提供的功能也是如此。简单的推送意味着推过最后推送的屏幕,而推送替换替换最后推送的屏幕,最终阻止您导航到最后推送的屏幕。就像它对堆栈数据结构的工作方式一样。

于 2021-08-10T21:51:27.103 回答
0

我正在使用 navigator.push 方法,它可以按照您的意愿行事。

appBar: AppBar(
  leading: IconButton(
    icon: Icon(Icons.arrow_back, color: Colors.blue),
    onPressed: () => Navigator.of(context).pop(),
  ), 
  title: Text("Sample"),
  centerTitle: true,
),

我希望它能解决你的问题。

于 2021-08-10T11:47:17.197 回答
0

首先用 WillPopScope 包裹你的脚手架

    return WillPopScope(
      onWillPop: _onBackPressed,
      child : Scaffold());

然后您可以调用处理后按的函数。

// Back Button Android Behaviour
      Future<bool> _onBackPressed() async {
        final shouldPop = await showDialog(
            context: context,
            builder: (context) => AlertDialog(
                  title: Text(
                    "Are you sure you want to leave this page?",
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 25.0,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  actions: <Widget>[
                    SizedBox(width: 16),
                    InkWell(
                      onTap: () {
                        Navigator.pushAndRemoveUntil(
                          context,
                          MaterialPageRoute(
                            builder: (BuildContext context) => HomeScreen(), // Destination
                          ),
                          (route) => false,
                        );
                      },
                      child: Container(
                        padding: EdgeInsets.all(8.0),
                        child: Text(
                          "LEAVE",
                          style: TextStyle(
                            color: Colors.red,
                            fontSize: 20.0,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ),
                    SizedBox(width: 8.0),
                    InkWell(
                      onTap: () => Navigator.of(context).pop(false),
                      child: Container(
                        padding: EdgeInsets.all(8.0),
                        child: Text(
                          "DO NOT LEAVE",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 20.0,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ),
                  ],
                ));
        return shouldPop ?? false;
      }
于 2021-08-10T22:11:38.517 回答