Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

android - How to automatically hide the status bar after scrolling it down?

I want to automatically hide the status bar after 3 seconds of scrolling it down.

currently, I'm doing this.

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    Timer.periodic(const Duration(seconds: 3), (timer) {
      SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen();
    );
  }
}

I want the timer to begin as soon as the user scrolls the the status bar.

Is there any better way to do that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may face existing issue on SystemChrome.

When setting System UI Overlays to bottom or top only, the status bar/bottom will show persistently after clicking.

I provide a workaround solution that detect status bar appear and react to it by using WidgetsBindingObserver

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {

  var count = 0;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  ...

  @override void didChangeMetrics() {
    count++;
    Future.delayed(const Duration(seconds: 3), () {
      if(count == 1) {
        SystemChrome.restoreSystemUIOverlays();
      }
      count --;
    });
  }
  ...

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...