首页 > 解决方案 > 从另一个类flutter访问私有方法

问题描述

我是 Flutter 的新手。我这里有 3 个类,它们是Login()MainMenu()这是已经登录后的屏幕,这MyDrawer()是我的应用程序的抽屉。我想要做的是我想signOut()Login(). 我该怎么做或者我应该怎么做才能重新设计我的代码。我已经尝试在下面访问它,它会收到异常 The method 'call' was called on null. 这是我完整代码中的代码片段:


class Login extends StatefulWidget {
  @override
  _LoginState createState() => _LoginState();
}

enum LoginStatus { notSignIn, signIn }

class _LoginState extends State<Login> {
  LoginStatus _loginStatus = LoginStatus.notSignIn;
  String email, password;
  final _key = new GlobalKey<FormState>();

  bool _secureText = true;

  signOut() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    setState(() {
      _loginStatus = LoginStatus.notSignIn;
    });
  }

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

  @override
  Widget build(BuildContext context) {
   
    switch (_loginStatus) {
      case LoginStatus.notSignIn:
        return Scaffold(
          
          backgroundColor: Colors.cyan,
          body: Center(
            child: ListView(
              shrinkWrap: true,
              padding: EdgeInsets.all(15.0),
              children: <Widget>[
                Center(
                  child: Container(
                    padding: const EdgeInsets.all(8.0),
                    color: Colors.cyan,
                    child: Form(
                      key: _key,
            
        break;

      case LoginStatus.signIn:
        return MainMenu();
       
        break;
    }
  }
}


class MainMenu extends StatefulWidget {

  

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

class _MainMenuState extends State<MainMenu> {


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
      )
   )
 }
 
class MyDrawer extends StatefulWidget {
    
  final Function onTap;
  final VoidCallback signOut;
 

    MyDrawer(
    {this.onTap,this.signOut
    });

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

    
 class _MyDrawerState extends State<MyDrawer> { 

  signOut() {
    setState(() {
      widget.signOut();
    });
  }

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

  @override
  Widget build(BuildContext context) {
    
    return SizedBox(
      width: MediaQuery
          .of(context)
          .size
          .width * 0.7,
      child: Drawer(
        child: Container(
          height:100,
          color: Colors.white,
          child: ListView(
            padding: EdgeInsets.all(0),
            children: <Widget>[
               ListTile(
                leading: Icon(Icons.exit_to_app,color: Colors.cyan, size: 30.0),
                 onTap: () {
                 signOut();
                  },
                title: Text("Logout",
                style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
              
                ),
              ),
         
            ],
          ),
        ),
      ),
    );
  }
}

我真的被这个问题困住了。任何帮助将不胜感激。谢谢!

标签: classflutterdart

解决方案


推荐阅读