Greetings to all. We'll look at how the Pushnamedandremoveuntil problem can be resolved using computer language in this post.
What is pushNamedAndRemoveUntil?
pushNamedAndRemoveUntil(context, "/login", (Route<dynamic> route) => false);
Where "/login" is the route you want to push on the route stack. This statement removes all the routes in the stack and makes the pushed one the root.
Simply, for example,
We'll create a login screen, after entering the email and password, the user can access the main UI. If we use normal methods (navigator method), the user can use the back button(android) and come back to the login screen. It's not good. If you use this method, the user has to log out to come back to the login screen. The back button is not working.
Example with coding
main. dart file here (Below)
import 'package:flutter/material.dart';
import 'screens/login_screen.dart';
import 'screens/register_screen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginView(),
routes: {
'/reg': (context) => const RegisterView(),
'/log': (context) => const LoginView(),
},
);
}
}
screens/login_screen. dart file coding here (Below)
import 'package:flutter/material.dart';
class LoginView extends StatelessWidget {
const LoginView({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(30),
child: Column(
children: [
Text(
'Login Screen',
style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold),
),
TextField(
decoration: InputDecoration(
labelText: 'Email',
labelStyle: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 18,
),
),
),
TextField(
decoration: InputDecoration(
labelText: 'Password',
labelStyle: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 18,
),
),
),
SizedBox(
height: 20,
),
Container(
child: ElevatedButton(
child: const Text('Login'),
onPressed: () {},
)),
TextButton(
onPressed: () {
//using method
// when went register screen you can use back button
Navigator.of(context).pushNamedAndRemoveUntil(
'/reg', (Route<dynamic> route) => false);
},
child: Text('Go to Register view'))
],
),
),
)),
);
}
}
screens/register_screen. dart file coding here (Below)
import 'package:flutter/material.dart';
class RegisterView extends StatelessWidget {
const RegisterView({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Register'),
),
body: TextButton(
onPressed: () {
//using method
Navigator.of(context)
.pushNamedAndRemoveUntil('/log', (Route<dynamic> route) => false);
},
child: Text('Go to Login view'),
),
);
}
}
Comments
Post a Comment