我正在尝试在按下浏览器刷新按钮后恢复页面堆栈。使用onGenerateRoute
onMaterialApp
我可以恢复单个页面,即使使用SharedPrefences
api 和 Firestore 数据的参数,但是当我按下浏览器后退按钮时,它会转到主页。如何保存颤动页面堆栈或至少只保存堆栈中当前页面的分支?这是一个onGenerateRoute
实现示例:
if (settings.name == ProductEditScreen.routeName) {
final args = settings.arguments != null
? settings.arguments as ProductEditScreenArguments
: null;
return MaterialPageRoute(
settings: RouteSettings(name: ProductEditScreen.routeName),
builder: (context) {
return settings.arguments != null
? ProductEditScreen(
arguments: ProductEditScreenArguments(
args!.product,
args.store,
args.locations,
),
setCurrentPageData: () async {
SharedPreferences prefs =
await SharedPreferences.getInstance();
//prefs.setString("sessionToken", user!.sessionToken!);
prefs.setString("productId", args.product.id!);
prefs.setString("storeId", args.store.id!);
},
)
: FutureBuilder<CurrentPageData>(future: () async {
SharedPreferences prefs =
await SharedPreferences.getInstance();
prefs.getString("sessionToken");
String? storeId = prefs.getString("storeId");
String? productId = prefs.getString("productId");
List<Location> locations = await EntityControllers
.locationController
.readAll()
.then((locations) => locations.cast<Location>());
Store store = (await EntityControllers.storeController
.readOneByAttribute("id", storeId!)) as Store;
Product product =
await ProductController.productQueryController(
productId!);
return CurrentPageData(
// sessionToken: user!.sessionToken!,
screenName: ProductEditScreen.routeName,
data: {
"store": store.toJson(),
},
dataEntity: {"product": product},
dataList: locations.map((e) => e.toJson()).toList(),
);
}(), builder: (BuildContext context,
AsyncSnapshot<CurrentPageData> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
if (snapshot.hasData) {
Store store =
Store.fromJson(snapshot.data!.data!["store"]!);
List<Location> locations = snapshot.data!.dataList!
.map((location) => Location.fromJson(location))
.toList();
Product product =
snapshot.data!.dataEntity!["product"] as Product;
return ProductEditScreen(
arguments: ProductEditScreenArguments(
product,
store,
locations,
),
setCurrentPageData: () async {
SharedPreferences prefs =
await SharedPreferences.getInstance();
//prefs.setString("sessionToken", user!.sessionToken!);
print("screen session token:");
prefs.setString("storeId", store.id!);
prefs.setString("productId", product.id!);
},
);
} else {
if (snapshot.hasError) {
print(snapshot.error);
}
return DashboardScreen();
}
});
},
);
}
在ProductEditScreen
页面之前有ProductListScreen
页面。我需要将它们放入堆栈。