无法自定义Flutter Toast消息

问题描述 投票:0回答:1

fluttertoast:我正在使用 ^8.2.4 包。我用 BatteryLevelCubit 将其包装在我的 MaterialApp.router 小部件中。在其中我每分钟检查一次电池状态。
我想要的是无论用户在哪个屏幕上都能够显示此 toast 消息。但我不知道颤动图标从何而来,也不知道为什么我想要的改变没有发生。

我尝试过的:

void showLowBatteryAlert({required int batteryLevel}) {
    if (!isShowLowBatteryAlert) {
      if (batteryLevel == batteryLevel) {
        emit(LowBatteryLevelState(batteryLevel));
      }
    }
  }
class LowBatteryLevelState extends BatteryLevelState {
  LowBatteryLevelState(this.batteryLevel) {
    Fluttertoast.showToast(
      msg: 'Low Battery Level: $batteryLevel',
      backgroundColor: Colors.red,
      fontSize: 16.sp,
      textColor: Colors.white,
      gravity: ToastGravity.TOP,
    );
  }

  final int batteryLevel;
}

Here's the output

android ios flutter dart mobile
1个回答
0
投票

如果代码不起作用,请随时回复

这是修改后的代码:

void showLowBatteryAlert({required int batteryLevel}) {
  if (batteryLevel < lowBatteryThreshold) {
    emit(LowBatteryLevelState(batteryLevel));
  }
}

class LowBatteryLevelState extends BatteryLevelState {
  LowBatteryLevelState(this.batteryLevel) {
    Fluttertoast.showToast(
      msg: 'Low Battery Level: $batteryLevel',
      backgroundColor: Colors.red,
      fontSize: 16.sp,
      textColor: Colors.white,
      gravity: ToastGravity.TOP,
    );
  }

  final int batteryLevel;
}
© www.soinside.com 2019 - 2024. All rights reserved.