10

I want to show some Screensaver type of screen when the user is not interacting the app for 5 minutes. So is anyone know how to achieve this kind of functionality in flutter.

import 'dart:async';

import 'package:flutter/material.dart';

const timeout = const Duration(seconds: 10);
const ms = const Duration(milliseconds: 1);
Timer timer;

void main() =>
    runApp(MyApp());


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var home = MyHomePage(title: 'Flutter Demo Home Page');
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: home,
    );
  }


}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _goToSecondScreen() {
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(builder: (context) => SecondPage()),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(child: Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(),
      floatingActionButton: FloatingActionButton(
        onPressed: _goToSecondScreen,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    ), behavior:
    HitTestBehavior.translucent, onTapDown: (tapdown) {
      print("down");
      if (timer != null) {
        timer.cancel();
      }
      timer = startTimeout();
    },);
  }

  startTimeout([int milliseconds]) {
    var duration = milliseconds == null ? timeout : ms * milliseconds;
    return new Timer(duration, handleTimeout);
  }

  void handleTimeout() {
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => ScreenSaver()),
    );
  }

}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Container();
  }
}
class ScreenSaver extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new GestureDetector(child:Container(color: Colors.yellow,),
      onTap: (){
        Navigator.pop(context);
      },
    );
  }
}

This is the sample code which I am trying to achieve the functionality. When the screen in active in Second screen its not working and the GestureDetector stops listening.

Radhabinod
  • 116
  • 1
  • 4

1 Answers1

7

You can wrap your whole app in a GestureDetector with behavior: HitTestBehavior.translucent to receive touch events while allowing widgets in your app also receiving these touch events.

You might also want to listen to keyboard events External keyboard in flutter support

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks for answering. I will try listening on `onTapDown` – Radhabinod Mar 05 '19 at 19:51
  • 1
    I have wrapped the app in `GestureDetector` but the issue is when I launch an screen from the event it worked only when it is on Parent Widget. If it is on another screen the I can not launch another screen due to context. So how can I handle the `context` issues. – Radhabinod Mar 07 '19 at 12:32
  • Please create a **minimal** reproduction as a single file including imports and `main(){...}` and add it to your question so that I (and others) can investigate. – Günter Zöchbauer Mar 07 '19 at 12:37
  • 1
    I have added the sample code in the question please check – Radhabinod Mar 07 '19 at 12:47
  • 1
    Exactly my thoughts! I came to SO to see if there are other methods, happy to see your solution is similar to mine. – om-ha Apr 22 '20 at 01:11