Home > Software design >  How to move from one Widget to another Widget when the user Login?
How to move from one Widget to another Widget when the user Login?

Time:10-03

I'm trying to create a LogIn App using Flutter. The problem that I have is that I don't know how to move or call another class once the user logs in. For example: A user logs in and the following page must show the counter template that flutter provides. Can you give me any advice or feedback? Here is my code:

//Here is my buttonLogIn from my LoginPage Class. Also, here is the method which I was trying to use to move to the Counter Class.
Widget _buttonLogin(){
    return StreamBuilder(
        builder: (BuildContext context,AsyncSnapshot snapshot){
          return ElevatedButton(
              onPressed: (){
                var _email = _emailController.text;
                var _password = _passwordController.text;
                setState(() {
                  if(_email==_logInEmail && _password==_logInPassword){
                    //_message = "email:$_email\npassword:$_password";
                    _moveToHomePage();
                  }else{
                    _message = "Wrong Email/Password";
                  }
                });
              },
            style: ElevatedButton.styleFrom(
              primary: Colors.orange,
              onSurface: Colors.deepOrangeAccent,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
              elevation: 10.0,
            ),
              child: Container(
                padding: const EdgeInsets.symmetric(horizontal: 80.0,vertical: 15.0),
                child: const Text("Sign In"),
              ),
          );
        }
    );
  }

  Widget _moveToHomePage(){
    return const MyCounterPage(title: 'Counter App');
  }

//Here is MyCounterPage. The following code is just the template that flutter provides for the counter app
class MyCounterPage extends StatefulWidget {
  const MyCounterPage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyCounterPage> createState() => _MyCounterPageState();
}

class _MyCounterPageState extends State<MyCounterPage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter  ;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

CodePudding user response:

you can you user Navigator.pushAndRemoveUntil to prevent user return form the previous page

Navigator.pushAndRemoveUntil(context, newRoute, (route) => false)
  • Related