I've searched everywhere and can't find a simple solution. I'm new to coding and new to Flutter/Dart.
I suspect this is a very basic problem. I need to pass a callback via an intermediary widget to the final widget.
Overview of the issue:
RootPage > TabNavigator > SearchPage > ProfilePage(with logout button on ProfilePage). Does not work.
In RootPage the offending code is:
@override
Widget build(BuildContext context) {
switch (authStatus) {
case AuthStatus.notDetermined:
return _buildWaitingScreen();
case AuthStatus.notSignedIn:
return LoginPage(
onSignedIn: _signedIn,
);
case AuthStatus.signedIn:
return TabNavigation(
ProfilePage(onSignedOut: _signedOut,)
);
}
return null;
}
RootPage > SearchPage (logout button on this SearchPage). Does work with this code:
@override
Widget build(BuildContext context) {
switch (authStatus) {
case AuthStatus.notDetermined:
return _buildWaitingScreen();
case AuthStatus.notSignedIn:
return LoginPage(
onSignedIn: _signedIn,
);
case AuthStatus.signedIn:
return SearchPage(
onSignedOut: _signedOut,
);
}
return null;
}
Code snippet:
class ProfilePage extends StatelessWidget {
ProfilePage({this.onSignedOut});
final VoidCallback onSignedOut;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Manage Profile'),
actions: <Widget> [
new FlatButton(
child: new Text('Logout', style: new TextStyle(fontSize: 17.0, color: Colors.white)),
onPressed: () => _signOut(context)
.....
....
class _RootPageState extends State<RootPage> {
AuthStatus authStatus = AuthStatus.notDetermined;
@override
Widget build(BuildContext context) {
switch (authStatus) {
case AuthStatus.notDetermined:
return _buildWaitingScreen();
case AuthStatus.notSignedIn:
return LoginPage(
onSignedIn: _signedIn,
);
case AuthStatus.signedIn:
return TabNavigation(
ProfilePage(onSignedOut: _signedOut),
);
}
return null;
}
Homepage of my app is RootPage, which manages the user's login status. Once logged in, the RootPage returns TabNavigation, which is a utility that manages the bottomNavigationbar, and returns SearchPage by default (i.e. setState returns currentPage = SearchPage).
I now wish to have the logout button on ProfilePage.
I don't know how to get the onPressed callback function to pass it's output back to RootPage.
Difficult to explain without pasting in the full code, but I hope you get the concept.