Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I change the background color of elevated button permanently in flutter? #35

Open
JiangWeixian opened this issue Nov 27, 2021 · 0 comments

Comments

@JiangWeixian
Copy link

I have made a list from a string and then made button from that list. Now, I want that when the button is pressed it will change it's color. Currently I have done the following but it only change the button color for a moment. Can it be permanently changed?

List<Widget> _buildButtonsWithNames() {
    for (int i = 0; i < wordlist.length; i++) {
      buttonsList.add(
        new ElevatedButton(
          onPressed: () => {
           
          },
          child: Text(wordlist[i]),
          style: ButtonStyle(
              overlayColor: getColor(Colors.white, Colors.teal),
              backgroundColor: getColor(Colors.blue, Colors.red)),
        ),
      );
    }
    return buttonsList;
  }

  MaterialStateProperty<Color> getColor(Color color, Color colorPressed) {
    final getColor = (Set<MaterialState> states) {
      if (states.contains(MaterialState.pressed)) {
        return colorPressed;
      } else {
        return color;
      }
    };
    return MaterialStateProperty.resolveWith(getColor);
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant