Home > OS >  In flutter I need to hide button after 1 mins
In flutter I need to hide button after 1 mins

Time:07-27

I need to hide button after 1 mins but while reloading and again restarting application it will not show again once it hide it will not repeated

CodePudding user response:

Use shared_preferences (or any persist memory) to persist the state of the button over restarts.

See Store key-value data on disk for details.

CodePudding user response:

You need to use db to handle restarting application case.

You can use shared_preferences to store a flag about visibility.


class _GState extends State<G> {
  static const String _buttonVisibilityKey = "DoneWIthButtonX";

  bool showButton = false;
  @override
  void initState() {
    super.initState();

    _buttonActionChecker().then((value) async {
      // return false if button never showed up, activate the timer to hide
      print(value);
      if (!value) {
        showButton = true;
        setState(() {});
        Future.delayed(Duration(minutes: 1)).then((value) async {
          showButton = false;
          SharedPreferences.getInstance()
              .then((value) => value.setBool(_buttonVisibilityKey, true));
          setState(() {});
        });
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  /// is the button have been shown already return true
  Future<bool> _buttonActionChecker() async {
    return SharedPreferences.getInstance().then((value) {
      return value.getBool(_buttonVisibilityKey) ?? false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: showButton
            ? FloatingActionButton(
                onPressed: () {
                  setState(() {});
                },
              )
            : null,
);
  }
}
  • Related