可以通过在NavigationView的页面中混合AutomaticKeepAliveClientMixin来保持页面状态。以下是示例代码:
import 'package:flutter/material.dart';
import 'package:fluent_ui/fluent_ui.dart';
class MyPage extends StatefulWidget {
const MyPage({Key? key}) : super(key: key);
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
super.build(context); // 必须调用此方法才能保持状态
return Scaffold(
body: NavigationView(
pane: NavigationPane(
selected: 4,
items: [
PaneItem(
icon: const Icon(FluentIcons.home_20_filled),
title: const Text('Home'),
),
...
],
),
content: Container(
padding: const EdgeInsets.all(16),
child: const Text('My Page Content'),
),
),
);
}
}
在这个示例中,MyPage被混合了AutomaticKeepAliveClientMixin,并重写了它的wantKeepAlive getter方法,返回true来指示页面的状态应该被保留。当你在NavigationView中导航到MyPage时,页面的状态将保持不变。为了使状态保持生效,你需要在State的build方法中调用super.build方法。
上一篇:AutomaticGMailGrading(GoogleScript)
下一篇:AutomaticKeepAliveClientMixin在BottomNavigationBar页面中不起作用。