如何从任何屏幕导航回主屏幕。我尝试在下面画一些例子。
runApp() ->HomePage()
->FirstScreen()
->FirstSubScreen() // <- Back to HomePage
->SecondScreen()
->ThirdScreen()
我要实现的目标是,当我在FirstSubScreen
时,我可以导航回HomePage
。我尝试使用onPressed: () => Navigator.popUntil(context, ModalRoute.withName('/homepage')
,但是什么也没有发生,没有错误或其他任何东西。那么我该如何解决这个问题呢?我将包含一些代码。
main.dart
void main() => runApp(new MyApp());
String username = '';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginPage(),
routes: <String, WidgetBuilder>{
'/landingpage': (BuildContext context) => new MyApp(),
'/homepage': (BuildContext context) => new HomePage(
username: username,
),
},
);
}
}
在登录页面中,我成功登录后就是社交登录,它导航到HomePage()
在主页中,我正在使用具有3个标签的标签控制器,分别为FirstScreen
,SecondScreen
,ThirdScreen
。在FirstScreen
中,我有一个扫描按钮,成功扫描后,它将导航到FirstSubScreen
,下面是代码。
FirstScreen.dart
Future scan() async {
try {
String barcode = await BarcodeScanner.scan();
setState(() {
var parsedJson = jsonDecode(barcode);
var qrdetails = QRDetails.fromJson(parsedJson);
final data = QRDetails(
name: "${qrdetails.name}",
type: "${qrdetails.type}",
amount: "${qrdetails.amount}",
);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ScannedScreen(
barcodedata: data,
)));
});
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(
() /* {
this.barcode = 'The user did not grant the camera permission!';
} */
{
Fluttertoast.showToast(
msg: 'You need to grant the camera permission',
toastLength: Toast.LENGTH_SHORT);
});
} else {
setState(() => this.barcode = 'Unknown error: $e');
}
} on FormatException {
setState(
() /* => this.barcode =
'null (User returned using the "back"-button before scanning anything. Result)' */
{
Fluttertoast.showToast(
msg: 'Scan canceled', toastLength: Toast.LENGTH_SHORT);
});
} catch (e) {
setState(() => this.barcode = 'Unknown error: $e');
}
}
FirstSubScreen
Widget backtoMainButton() {
return Align(
alignment: FractionalOffset.bottomCenter,
child: Container(
height: 80.0,
child: SizedBox.expand(
child: FlatButton(
onPressed: () {
Navigator.of(context).popUntil(ModalRoute.withName('/homepage'));
},
child: Text('BACK TO HOME',
style: TextStyle(
fontSize: 16.0,
fontFamily: "Roboto Regular",
color: Colors.black)),
),
)));
}