class - 从另一个类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),
),
),
],
),
),
),
);
}
}
我真的被这个问题困住了。任何帮助将不胜感激。谢谢!
解决方案
推荐阅读
- oauth-2.0 - 在获取 EWS.AccessAsUser.All 的访问令牌后获取用户名(至少是用户的电子邮件地址)
- python - 模拟一个从 ... import 中幸存的包
- haskell - Biff 是应用程序吗?
- html - Ruby如何同时循环两个对象
- javascript - 循环内的 Onchange
- python - PDF 到 Pandas 数据框
- arrays - SWIFT Xcode11.5 - 数组附加在 ViewController 中不起作用
- regex - 如何根据最大数字的位数为所有数字添加前缀?
- lua - Lua 尝试调用一个 nil 方法
- mfc - MFC:向可调整大小的 CDialogEx 添加状态栏?