flutter局部跳转的思路其实很简单,只需要在一个控件外创建一个根路由,仿照安卓的命名,我给这个路由起名叫Fragment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Fragment extends StatelessWidget {
final Widget child;
final String root;

const Fragment({required this.child, this.root = "/", super.key});

@override
Widget build(BuildContext context) {
return Navigator(
initialRoute: root,
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder builder;
builder = (context) => child;
return MaterialPageRoute(builder: builder);
},
);
}
}

这段代码来自这里

效果如Shape_Weather