0

How can I pass a non constant value that is defined when the page is called as a default value to init a function.

So I came up with the following code that is supposed to be a switch between to pages of an app but I want to change the default page by defining the default value with mainPage instead of true

class RegisterOrLogin extends StatefulWidget {
  final bool mainPage;
  const RegisterOrLogin({Key? key,required this.mainPage}) : super(key: key);

  @override
  State<RegisterOrLogin> createState() => _RegisterOrLoginState();
}

class _RegisterOrLoginState extends State<RegisterOrLogin> {
  
  // default value
  bool showLoginPage = true;

  // switch between pages
  togglePages() {
    setState(() {
      showLoginPage = !showLoginPage;
    });
  }

  @override
  Widget build(BuildContext context) {

    if (showLoginPage) {
      return LoginPage(showRegisterPage: togglePages);
    }
    else {
      return RegisterPage(showLoginPage: togglePages);
    }
  }
}

I tried to just write bool showLoginPage = widget.mainPage; but it seems like it's not something you can do. i tried to initialize the value inside the function but it didn't work as well.

So I was wondering if anyone could provide me a solution to this problem and maybe explain me why I was wrong.

0 Answers0