flutter-stats-fl icon indicating copy to clipboard operation
flutter-stats-fl copied to clipboard

Control whether FPS is enabled dynamically

Open litang0908 opened this issue 1 year ago • 1 comments

Make StatsFlState public and add a new method updateEnable() to control isEnabled.

for example:

final statsflKey = GlobalKey<StatsFlState>();

Widget app = StatsFl(
    key: statsflKey,
    isEnabled: true,
    showText: true,
    align: Alignment.centerLeft,
    maxFps: 120,
    child: Center(
      child: ElevatedButton(
        onPressed: () {
          if (statsflKey.currentState?.isEnabled == true) {
            statsflKey.currentState?.updateEnable(false);
          } else {
            statsflKey.currentState?.updateEnable(true);
          }
        },
        child: Text("Toggle"),
      ),
    ),
  );

litang0908 avatar Apr 18 '24 08:04 litang0908

Can't you just do:

Widget app = StatsFl(
    key: statsflKey,
    isEnabled: _showStats,
   ...
);

and

onPressed: () {
  setState((){
    _showStats = !_showStats;
  });},

Maybe we need to add a didUpdateWidget hook to start/stop the timer when isEnabled changes?

esDotDev avatar Apr 18 '24 20:04 esDotDev

Thanks for repling, this PR will be closed.

litang0908 avatar May 03 '24 04:05 litang0908