请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Navigator operation requested with a context that does not include a Navigator

import ‘package:flutter/cupertino.dart’;
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);

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.0; //正常动画速度
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text(“你好”),
),
body: Center(
child: PhotoHero(
photo:
https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595506145397&di=d2329b0a785fac8fc7f620b4fa5801e6&imgtype=0&src=http%3A%2F%2Fpic2.zhimg.com%2Fv2-bc0c14278ec0e2c604f9307a5323815b_1200x500.jpg”,
width: 300.0,
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => MaterialApp(home: NewAnimation(),)));
},
),
),
),
);
}
}

class NewAnimation extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(“Hello”),
),
body: Container(
color: Colors.lightBlueAccent,
padding: EdgeInsets.all(16.0),
alignment: Alignment.topLeft,
child: PhotoHero(
photo:
https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595506145397&di=d2329b0a785fac8fc7f620b4fa5801e6&imgtype=0&src=http%3A%2F%2Fpic2.zhimg.com%2Fv2-bc0c14278ec0e2c604f9307a5323815b_1200x500.jpg”,
width: 300.0,
onTap: () {
Navigator.of(context).pop();
}),
),
);
}
}

正在回答 回答被采纳积分+3

1回答

CrazyCodeBoy 2020-07-27 09:12:10

这个问题的出现是因为使用navigator的地方在MaterialApp 和 WidgetApp之外导致的:
https://stackoverflow.com/questions/44004451/navigator-operation-requested-with-a-context-that-does-not-include-a-navigator

也可对照下这块的课程源码检查下你的代码看是否有出入的地方:https://git.imooc.com/coding-321/flutter_trip/src/master/doc/%E5%8A%A8%E7%94%BBAnimation%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97.md

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信