【问题标题】:How do I logout time-based in Flutter App?如何在 Flutter App 中基于时间注销?
【发布时间】:2021-05-19 06:32:56
【问题描述】:

如果用户在登录后超过 5 分钟对应用没有反应或处于非活动状态,我需要从应用中注销用户。该应用程序将转到主登录页面。

我尝试实施给定的解决方案here,但没有成功。请帮助我如何实现这一目标。

我的代码

    class AppRootState extends State<AppRoot> {
    
          Timer _rootTimer;
        
          @override
          void initState() {
            // TODO: implement initState
            super.initState();
            initializeTimer();
          }
        
          void initializeTimer() {
           
            const time = const Duration(minutes: 5);
            _rootTimer = Timer(time, () {
              logOutUser();
            });
          }
        
          void logOutUser() async {
           
             // Log out the user if they're logged in, then cancel the timer.
            // You'll have to make sure to cancel the timer if the user manually logs out
            //   and to call _initializeTimer once the user logs in
            
            _rootTimer?.cancel();
    
          }
        
          // You'll probably want to wrap this function in a debounce
        
        void _handleUserInteraction([_]) {
            if (_rootTimer != null && !_rootTimer.isActive) {
              // This means the user has been logged out
              return;
            }
        
            _rootTimer?.cancel();
            initializeTimer();
          }
        
          @override
      Widget build(BuildContext context) {
        return Listener(
          behavior: HitTestBehavior.translucent,
          onPointerDown: _handleUserInteraction,
          onPointerMove: _handleUserInteraction,
          onPointerUp: _handleUserInteraction,
          child: MaterialApp(
    )
    );
    }
    
        }

我尝试使用 Listener 和 GestureDetector,但它不起作用。用户甚至在积极使用该应用程序时也已注销。

【问题讨论】:

    标签: flutter dart timer


    【解决方案1】:

    迟到但可能对其他人有所帮助。上次我遇到同样的问题。当我将 Timer _rootTimer 声明为全局变量并且我的代码开始工作时,我感到很惊讶。

    我的代码是:

    Timer _rootTimer;
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) => AppRoot();
    }
    
    class AppRoot extends StatefulWidget {
      // This widget is the root of your application.
      @override
      AppRootState createState() => AppRootState();
    }
    
    class AppRootState extends State<AppRoot> {
    @override
      void initState() {
        // TODO: implement initState
        super.initState();
        initializeTimer();
      }
    void initializeTimer() {
        if (_rootTimer != null) _rootTimer.cancel();
        const time = const Duration(minutes:  5 );
        _rootTimer = Timer(time, () {
          logOutUser();
        });
      }
    void logOutUser() async {
     // Log out the user if they're logged in, then cancel the timer.
               
                
     _rootTimer?.cancel();
    }
    
    // You'll probably want to wrap this function in a debounce
      void _handleUserInteraction([_]) {
        if (_rootTimer != null && !_rootTimer.isActive) {
          // This means the user has been logged out
          return;
        }
        _rootTimer?.cancel();
    
        initializeTimer();
      }
      @override
      Widget build(BuildContext context) {
        return Listener(
          behavior: HitTestBehavior.translucent,
          onPointerDown: _handleUserInteraction,
          onPointerMove: _handleUserInteraction,
          onPointerUp: _handleUserInteraction,
          child:MaterialApp(
              title: 'example',
              debugShowCheckedModeBanner: false,
              theme: ThemeData(
                primarySwatch: Colors.blue,
                visualDensity: VisualDensity.adaptivePlatformDensity,
              ),
              home: HomePage()
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-07
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2011-07-01
      • 2021-05-07
      • 2018-11-30
      • 1970-01-01
      相关资源
      最近更新 更多