anirudhsharma392 / Slider-Button

A flutter package for creating a slider button widget.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reset button state

diego-lipinski-de-castro opened this issue · comments

How can I reset the button state? Sometimes the user push the button and it just stays there pushed, and I cant reset it back to its unpushed state

You ought to use a callback that sets another key.

Hope that the example below will help you.

import 'package:flutter/material.dart';
import 'package:slider_button/slider_button.dart';

class StateStore extends StatefulWidget {
  const StateStore({Key? key}) : super(key: key);

  @override
  StateStoreState createState() => StateStoreState();
}

class StateStoreState extends State<StateStore> {
  int _forcedRebuildNumber = 0;

  void forceRebuild() { // the callback
    setState(() => _forcedRebuildNumber += 1);
  }

  @override
  Widget build(BuildContext context) {
    // It is possible to add more widgets here (e.g. using a list).
    return ReadConfirmationSlider(key: Key(_forcedRebuildNumber.toString()));
  }
}

class ReadConfirmationSlider extends StatelessWidget {
  const ReadConfirmationSlider({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return SliderButton(
      action: () {
        ///Do something here
        Navigator.of(context).pop();
      },
      label: const Text(
        'Mark as read',
        style: TextStyle(
            color: Color(0xff4a4a4a),
            fontWeight: FontWeight.w500,
            fontSize: 17),
      ),
      icon: const Text(
        'x',
        style: TextStyle(
          color: Colors.white,
          fontWeight: FontWeight.w400,
          fontSize: 44,
        ),
      ),
    );
  }
}

@diego-lipinski-de-castro I think some exceptions are shooting up in action, which causes the button to get stuck.

Added a Future callback as an action to handle state, it's a breaking change though.