title: How to execute a function after a period of inactivity in Flutter
body: <p>I need to create an app that navigates a user to after a period of inactivity.</p>
  
  <p>I tried wrapping my app in GestureDetector and run a timer after a specified period as shown below but it is not working:</p>
  
  <pre><code>class _MyAppState extends State&lt;MyApp&gt; {
    Timer _timer;
  
    @override
    void initState() {
      super.initState();
  
      _initializeTimer();
    }
  
    void _initializeTimer() {
      _timer = Timer.periodic(const Duration(seconds: 20), (_) =&gt; _logOutUser);
    }
  
    void _logOutUser() {
  Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) =&gt; WelcomePage()));
      _timer.cancel();
    }
  
    void _handleUserInteraction([_]) {
      if (!_timer.isActive) {
        return;
      }
  
      _timer.cancel();
      _initializeTimer();
    }
  
    @override
    Widget build(BuildContext context) {
      return GestureDetector(
        onTap: _handleUserInteraction,
        onPanDown: _handleUserInteraction,
        child: MaterialApp(
          title: 'Hello',
          home: WelcomePage(),
          routes: &lt;String, WidgetBuilder&gt;{
            '/welcome': (BuildContext context) =&gt; new WelcomePage(),
            '/login': (BuildContext context) =&gt; new LoginPage(),
          },
          debugShowCheckedModeBanner: false,
        ),
      );
    }
  }
  </code></pre>
  
  <p>My question is how best should I implement running a function after a period of inactivity within my flutter app?</p>