我正在尝试这里提供的标准导航应用程序https://flutter.dev/docs/cookbook/navigation/named-routes。即使我打开浏览器访问http://localhost:8080/#/second,它也会构建页面1,然后将其替换为页面2,甚至会向用户显示一个后退按钮。我真的想把用户直接带到第二个页面,而不是“传递go”。有什么想法吗?
下面是添加了一些打印内容的代码
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Named Routes Demo',
routes: {
'/': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
));
}
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print ("Screen 1 building");
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: RaisedButton(
child: Text('Launch second screen'),
onPressed: () {
// Navigate to the second screen using a named route.
Navigator.pushNamed(context, '/second');
},
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print ("Screen 2 building");
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('This is screen 2, go to 1!'),
),
),
);
}
}
发布于 2021-04-09 08:49:44
您可以在第14行<base href="/">
的<FlutterProject>/web/index.html
中设置默认路由路径。
它默认设置为"/“
https://stackoverflow.com/questions/62800142
复制相似问题