首页 > 解决方案 > 如何检测用户是否在颤动中打开页面

问题描述

我正在制作一个聊天应用程序并想在其中添加一个“已看到”选项,为此我想知道用户何时打开的页面我对此一无所知这些是我的路线

class Routes {
  final routes = <String, WidgetBuilder>{
  '/auth': (BuildContext context) => AuthScreen(),
  '/home': (BuildContext context) => HomeScreen(),
  '/profile/edit': (BuildContext context) => EditUserScreen(),
  '/': (BuildContext context) => SplashScreen(),
  '/phonelog': (BuildContext context) => MobileAuthScreen(),
  '/Chat': (BuildContext context) => Chat(),
}; 

标签: flutterdart

解决方案


这是一个例子:

您可以在路线创建期间添加模型,或者如果您使用任何类型的服务定位器或继承的小部件,则可以提供它。

不仅仅是向该模型添加一个您已经看到路线的标志。

class Routes {
  final routes;
  final RouteModel routeModel;

  Routes()
      : routeModel = RouteModel(),
        routes = <String, WidgetBuilder>{
          '/auth': (BuildContext context) => AuthScreen(routeModel),
          '/home': (BuildContext context) => HomeScreen(routeModel),
          '/profile/edit': (BuildContext context) => EditUserScreen(routeModel),
          '/': (BuildContext context) => SplashScreen(routeModel),
          '/phonelog': (BuildContext context) => MobileAuthScreen(routeModel),
          '/Chat': (BuildContext context) => Chat(routeModel),
        };
}

class RouteModel {
  Map<String,bool> seen = {};
}

稍后在您的路线中,您希望以某种方式添加您看到的标志,并基于该标志构建小部件。

void aboutToNavigateOut(){
  routeModel.seen['/home'] = true;
}

Widget build(context){
  return Text(routeModel.seen['/home']?"Seen":"UnSeen");
}

推荐阅读