我想在扑朔迷离中走一条新路,但是我失败了, 我的VS代码给我这个:
在处理手势时引发了以下断言: I / flutter(32582):请求的导航器操作使用了不包含导航器的上下文。 I / flutter(32582):用于从导航器推送或弹出路由的上下文必须是作为 I / flutter(32582):导航器小部件的后代
import 'package:flutter/material.dart';
void main()=>runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Lake',
routes: {
'sss': (context)=>new NewRoute()
},
home: new Scaffold(
appBar: AppBar(
title: Text('Lake'),
),
body: Text('BBB'),
floatingActionButton: new FloatingActionButton(
child: Icon(Icons.import_contacts),
onPressed: (){
Navigator.pushNamed(context, 'sss');
},
),
),
);
}
}
class NewRoute extends StatelessWidget{
@override
Widget build(BuildContext context){
return new Scaffold(
appBar: AppBar(
title: Text('BBB'),
),
body: Center(
child: Text('wahaha'),
),
);
}
}
答案 0 :(得分:0)
请使用此代码
home: Builder(
builder: (context) => Scaffold(
appBar: AppBar(
title: Text('Lake'),
),
body: Text('BBB'),
floatingActionButton: new FloatingActionButton(
child: Icon(Icons.import_contacts),
onPressed: (){
Navigator.pushNamed(context, 'sss');
},
),
),)
Builder让您可以像直接https://docs.flutter.io/flutter/widgets/Builder-class.html
中所述从直接父级构建新上下文