xxxxxxxxxx
import 'package:flutter/material.dart';
// Method to navigate to the previous screen
void goBack(BuildContext context) {
Navigator.of(context).pop();
}
// Example usage:
class MyScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Screen'),
),
body: Center(
child: RaisedButton(
onPressed: () {
goBack(context); // Navigates back to the previous screen
},
child: Text('Go Back'),
),
),
);
}
}
xxxxxxxxxx
import 'package:flutter/material.dart';
class MyScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("My Screen"),
),
body: Center(
child: RaisedButton(
child: Text("Go back"),
onPressed: () {
Navigator.pop(context); // Navigates back to the previous screen
},
),
),
);
}
}
xxxxxxxxxx
Future<void> _goToPage2() async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Page2(),
),
);
print("Page2 is poped");
}