navigator.push 在 flutter 中,没有错误,但无法移动到另一个页面

navigator.push in flutter, no errors but it can not move to another page

 class _MyHomePageState extends State<MyHomePage> 
    @override
     Widget build(BuildContext context) {
     return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text("Main page"),
          backgroundColor: Colors.black,
        ),
        body: Center(
          child: Column(
            children: [
              Text(
                'this is main page',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 50,
                ),
              ),
              RaisedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) {
                      return TestPage();
                    }),
                  );
                },
                child: Text('go to test page'),
              ),
           
            ],
          ),
        ),
       ),
     );
    }
   }

Navigator不报错,但是不跳到别的页面,只是在console里给了很多

======== Exception caught by gesture 
The following assertion was thrown while handling a gesture:
Navigator operation requested with a context that does not include a Navigator.

The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
When the exception was thrown, this was the stack: 
#0      Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:2741:9)
#1      Navigator.of (package:flutter/src/widgets/navigator.dart:2748:6)
#2      Navigator.push (package:flutter/src/widgets/navigator.dart:2137:22)
#3      _MyHomePageState.build.<anonymous closure> (package:navigator_push_route/main.dart:43:29)
#4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:193:24)
#6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:608:11)
#7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
#8      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:230:7)
#9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:558:9)
#10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:94:12)
#11     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:139:9)
#12     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8)
#13     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:137:18)
#14     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:123:7)
#15     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:440:19)
#16     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:420:22)
#17     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:278:11)
#18     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
#19     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
#20     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
#21     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
#25     _invoke1 (dart:ui/hooks.dart:185:10)
#26     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:293:7)
#27     _dispatchPointerDataPacket (dart:ui/hooks.dart:98:31)
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer: TapGestureRecognizer#b9e3b
  debugOwner: GestureDetector
  state: possible
  won arena
  finalPosition: Offset(194.0, 253.0)
  finalLocalPosition: Offset(66.0, 24.0)
  button: 1
  sent tap down

在父窗口小部件上分开 MaterialApp

void main() {
  runApp(
    const MaterialApp(    
      home: MyHomePage(),
    ),
  );
}

或者为上下文创建一个小部件,

void main() {
  runApp(
    const MyApp(),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return  MaterialApp(
      home: MyHomePage(),
    );
  }
}

您使用了错误的上下文。

将您的 RaisedButton 移动到具有新上下文的另一个页面或像这样用构建器包装它:

 Builder(
             builder: (context) {
               return RaisedButton(
                 onPressed: () {
                   Navigator.push(
                     context,
                     MaterialPageRoute(builder: (context) {
                       return TestPage();
                     }),
                   );
                 },
                 child: Text('go to test page'),
               );
             }
           ),

这应该有效