skip to Main Content

I need to listen for a tap event on the status bar and call a custom function (call Scrollable.ensureVisible). However, there is no way I can find a way to do this.

As far as I understand, there are 2 options:

  1. Listen to this event from flutter, but I haven’t found any way to do this.
  2. Write native code that will listen to the status bar tap event and use MethodChannel to deliver the event to dart.

All my attempts to implement both options failed. Could you please tell me how it can be done?

2

Answers


  1. Use this package to achieve your goal https://pub.dev/packages/scrolls_to_top

    Login or Signup to reply.
  2. Keep in mind that Flutter has the ability to render every pixel of your screen. So, to override the default tap action on the status bar you have to add a GestureDetector where the status bar is normally positioned.

    This can be achieved with a Stack where the first child is your main widget tree (usually a Scaffold) and the second child is a Positioned with the GestureDetector:

    import 'package:flutter/material.dart';
    
    class StatusBarTapExample extends StatelessWidget {
      const StatusBarTapExample({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Stack(
          children: [
            Scaffold(
              appBar: AppBar(
                title: Text('Title'),
              ),
              body: Center(
                child: Text('Content'),
              ),
            ),
            // Status bar tap override
            Positioned(
              top: 0,
              left: 0,
              right: 0,
              height: MediaQuery.of(context).padding.top,
              child: GestureDetector(
                excludeFromSemantics: true,
                onTap: onStatusBarTap,
              ),
            ),
          ],
        );
      }
    
      void onStatusBarTap() {
        print('onStatusBarTap');
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search