报错 The context used to push or pop routes from the Navigator...
来源:7-9 动画Animation开发指南-Hero动画-1

罗小黑战记
2019-10-30
void main() => runApp(HeroAnimation());
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;
class PhotoHero extends StatelessWidget {
final String Photo;
final VoidCallback onTap;
final double width;
const PhotoHero({Key key, this.Photo, this.onTap, this.width})
: super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
width: width,
child: Hero(
tag: Photo,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onTap,
child: Image.network(
Photo,
fit: BoxFit.contain,
),
),
)),
);
}
}
class HeroAnimation extends StatelessWidget {
@override
Widget build(BuildContext context) {
timeDilation = 10;
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Hero Animation'),
),
body: Center(
child: PhotoHero(
Photo: 'http://pic13.nipic.com/20110405/5194273_154928947378_2.jpg',
width: 300,
onTap: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Hero Animation 2'),
),
body: Container(
color: Colors.limeAccent,
padding: EdgeInsets.all(16),
alignment: Alignment.topLeft,
child: PhotoHero(
Photo:
'http://pic13.nipic.com/20110405/5194273_154928947378_2.jpg',
width: 100,
onTap: () {
Navigator.of(context).pop();
}),
),
),
);
}));
},
),
),
),
);
}
}
I/flutter (20193): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (20193): The following assertion was thrown while handling a gesture:
I/flutter (20193): Navigator operation requested with a context that does not include a Navigator.
I/flutter (20193): The context used to push or pop routes from the Navigator must be that of a widget that is a
I/flutter (20193): descendant of a Navigator widget.
I/flutter (20193): When the exception was thrown, this was the stack:
I/flutter (20193): #0 Navigator.of. (package:flutter/src/widgets/navigator.dart:1461:9)
I/flutter (20193): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:1468:6)
I/flutter (20193): #2 HeroAnimation.build. (package:myflutter_app/Herotest.dart:46:25)
I/flutter (20193): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635:14)
I/flutter (20193): #4 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:711:32)
I/flutter (20193): #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
I/flutter (20193): #6 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
I/flutter (20193): #7 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
I/flutter (20193): #8 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
I/flutter (20193): #9 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
I/flutter (20193): #10 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
I/flutter (20193): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
I/flutter (20193): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
I/flutter (20193): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
I/flutter (20193): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
I/flutter (20193): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
I/flutter (20193): #19 _invoke1 (dart:ui/hooks.dart:250:10)
I/flutter (20193): #20 _dispatchPointerDataPacket (dart:ui/hooks.dart:159:5)
I/flutter (20193): (elided 3 frames from package dart:async)
I/flutter (20193): Handler: "onTap"
I/flutter (20193): Recognizer:
I/flutter (20193): TapGestureRecognizer#8e36b
I/flutter (20193): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/[MALI]Gralloc: [+]r_hnd(0x7f8f4a2ee0), client(50), share_fd(72)
D/GraphicBuffer(20193): register, handle(0x7f8f4a2ee0) (w:720 h:1184 s:720 f:0x1 u:0x000b00)
1回答
-
CrazyCodeBoy
2019-10-31
你将要跳转的页面抽出来单独放一个文件中再去跳转试一下
00
相似问题