How to check if a widget/page is rendered?

The code bellow allows to check if the page is visible for a user:

import 'package:flutter/material.dart';

class TestWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('TestWidget: ${ModalRoute.of(context).isCurrent}');
    return Container();
  }
}

And this sample allows to check when a route is changed. But you can't get current page name.


As an addition to Kostya Vyrodov's answer and in response to his comment 'But you can't get current page name.', the following code snippet will print the current page name.

print("context.widget.toStringShort: ${context.widget.toStringShort()}");

Typically, this is best used in the page's initState to save the page name to a global variable for use elsewhere in the app. For example, I use it with Firebase Cloud Messaging in the _firebaseMessaging.configure() method to determine specific navigation requirements. If someone finds this useful, please up-vote.

Tags:

Flutter