我收到一個例外:FlutterError(使用不包含 Navigator 的背景關系請求的 Navigator 操作。用于從 Navigator 推送或彈出路由的背景關系必須是作為 Navigator 小部件后代的小部件的背景關系。)
這是第一頁的重要代碼(跳過了一些不相關的代碼):
import 'package:google_fonts/google_fonts.dart';
import 'register.dart';
void main() async {
...
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
...
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
textTheme: GoogleFonts.interTextTheme(
Theme.of(context).textTheme,
)),
home: Scaffold(
body: Container(
width: double.infinity,
...
Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondRoute()));
},
child: Text(
"Forgot Password?",
style: GoogleFonts.inter(
fontWeight: FontWeight.w600,
),
),
),
),
...
),
));
} }
這是第二頁:
import 'package:flutter/material.dart';
class SecondRoute extends Navigator {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
);
}
}
沒有語法錯誤,只有在代碼運行時拋出的例外。我已經嘗試過尋找解決方案,但另一種將材料應用程式放入 runApp() 方法的方法,據我所知不適用于我使用文本主題和使用背景關系的方式。
如果我需要提供更多代碼或背景關系,請告訴我。
任何幫助將不勝感激!
uj5u.com熱心網友回復:
嘗試改變:
class SecondRoute extends Navigator {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
);
}
}
到:
class SecondRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
);
}
}
編輯,還將腳手架從_MyAppState它自己的小部件中移動。
class FirstRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox(
width: double.infinity,
child: Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: () {
print('hello');
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
child: Text(
"Forgot Password?",
style: GoogleFonts.inter(
fontWeight: FontWeight.w600,
),
),
),
),
),
);
}
}
然后_MyAppState像這樣使用:
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
textTheme: GoogleFonts.interTextTheme(
Theme.of(context).textTheme,
)),
home: FirstRoute(),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/394153.html
