我有一个 ChangeNotifierProvider 类,它为它的 Consumer Widgets 提供了一个弹出窗口。名为“showPopup”的弹出功能包含一个凸起的按钮,当按下该按钮时,用户使用 Google 登录,获取他们的信息,将其发布到 mysql 数据库并将其保存到共享首选项文件中。
我正在使用提供程序类来访问更改通知程序,因为一旦使用登录的用户信息更新了共享首选项,我就需要立即通知其他页面。
登录后,我希望弹出窗口自动关闭。不幸的是,我尝试使用的代码不起作用 - “上下文”一词下方有一条红线。据推测,这是因为弹出窗口是从不同的页面调用的。
Navigator.pop(context);
我可以为“上下文”添加什么?或者有没有其他方法可以让这个弹出窗口在用户登录后立即自动关闭?
这是代码;
class SocialProvider with ChangeNotifier {
final SocialLogin socialLogin = SocialLogin();
static const GOOGLE_WEB_CLIENT_ID = "xxx.apps.googleusercontent.com";
SocialUser currentUser;
String currentname;
String currentavatar;
String currentemail;
bool currentlogged;
void socState() {
socialLogin.setConfig(SocialConfig(
googleWebClientId: GOOGLE_WEB_CLIENT_ID,
));
}
Future<void> logInGoogle() async {
try {
socState();
currentUser = await socialLogin.logInGoogle();
currentavatar = currentUser.pictureUrl;
currentname = currentUser.name;
currentemail = currentUser.email;
currentlogged = true;
postSocialData(currentUser.name, currentUser.email, currentavatar);
savePreferences(
currentname: currentUser.name,
currentemail: currentUser.email,
currentlogged: true,
);
Navigator.pop(context);
} catch (e) {
print(e);
}
}
savePreferences({String currentname, String currentavatar, String currentemail, bool currentlogged}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('currentname', currentname);
prefs.setString('currentavatar', currentavatar);
prefs.setString('currentemail', currentemail);
prefs.setBool('currentlogged', currentlogged);
notifyListeners();
}
loadPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String currentname = prefs.getString('currentname');
String currentavatar = prefs.getString('currentavatar');
String currentemail = prefs.getString('currentemail');
bool currentlogged = prefs.getBool('currentlogged');
notifyListeners();
}
showPopup(BuildContext context, Widget widget, String title,
{BuildContext popupContext}) {
Navigator.push(
context,
PopupLayout(
top: 30,
left: 30,
right: 30,
bottom: 50,
child: PopupContent(
content: Scaffold(
resizeToAvoidBottomPadding: false,
body: widget,
),
),
),
);
}
Widget popupBody() {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
textColor: Colors.white,
color: Colors.indigoAccent,
onPressed: logInGoogle,
child: Text('Log in with Google'),
)
],
),
),
],
),
),
);
}
}
这是从有状态小部件的构建中调用弹出窗口的地方;
child: new FlatButton(
onPressed: () {
socialProvider.loadPreferences();
if (socialProvider.currentlogged != true) {
socialProvider.showPopup(context, socialProvider.popupBody(), 'Cancel Login');
} else {
switch (_currentStatus) {
case RecordingStatus.Initialized:
{
_start();
break;
}
case RecordingStatus.Recording:
{
_pause();
break;
}
case RecordingStatus.Paused:
{
_resume();
break;
}
case RecordingStatus.Stopped:
{
_init();
break;
}
default:
break;
}
}//switch end
},
child: Padding(
padding: const EdgeInsets.all(5.0),
child: _buildText(_currentStatus),
),
color: Color(0xffb79eb5).withOpacity(.75),
),