DevRedStone / Chiclet

A Flutter package containing highly customizable island-style buttons known as Chiclet.
MIT License
9 stars 1 forks source link

isPressed argument is missleading #4

Open pro100svitlo opened 1 week ago

pro100svitlo commented 1 week ago

Expected behavior:

Actual behavior:

In other words, the naming might be changed to isInitialPressed or something similar. This would more accurately represent the real functionality.

However, the expected behavior would be very nice to have as well, since the button can be used as a kind of checkbox (selected and unselected states).


Code to reproduce the issue:

class Page extends StatefulWidget {
  const Page({super.key});

  @override
  State<Page> createState() => PageState();
}

class PageState extends State<Page> {
  var isFirstButtonPressed = false;

  @override
  Widget build(BuildContext context) {
    final firstButton = ChicletAnimatedButton(
      child: const Text('1'),
      onPressed: () {},
    );

    final secondButton = ChicletAnimatedButton(
      child: const Text('2'),
      onPressed: () {
        setState(() {
          isFirstButtonPressed = !isFirstButtonPressed;
        });
      },
    );

    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        firstButton,
        secondButton,
      ],
    );
  }
}
DevRedStone commented 1 week ago

Thank you for taking the time to report this issue and provide a detailed description! I understand the concern, and your explanation of the behavior is clear. You're correct that the current implementation might feel more like an "initial state" rather than a dynamically controlled one, which could limit its usability for scenarios like a toggleable checkbox.

The following solution resolves it:

final firstButton = ChicletAnimatedButton(
  key: Key(isFirstButtonPressed.toString()),
  isPressed: isFirstButtonPressed,
  child: const Text('1'),
  onPressed: () {},
);

By assigning a Key tied to the isFirstButtonPressed state, we force Flutter to rebuild the ChicletAnimatedButton whenever the state changes. This ensures the button updates dynamically to reflect the isPressed state.

I’ve updated the example code to incorporate this solution. However, please note that this approach serves as a hotfix for the situation. A more robust solution could involve revisiting the widget’s state management or improving how the ChicletAnimatedButton handles its internal and external states. Let me know if you'd like to discuss this further!

pro100svitlo commented 1 week ago

Hi,

thank you for the fast response 😉

Yes, you are absolutely right regarding the key usage (this is the way I fixed it for now). But I also agree that this is a duct taping 😄

Anyway, I appreciate the package and your time 🙏