Solved: How to show a string variable in flutter which changes when a button is pressed?

Question

Asked by masfiq r on November 27, 2021 (source).

I want show a string vaiable which changes it's value when a button is pressed. Inside the body there is variable name pressedText. It is a static variable. When a button is pressed I want to show it's updated value, but can not show anything right now. Can I show it's updated value?

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Second Route"),
      ),
      body: Column(
        children: <Widget>[
          Text(
            pressedText,
            style: TextStyle(
              fontSize: 30.0,
            ),
          ),
          Expanded(
            child: Center(
              child: Wrap(
                children: _buildButtonsWithNames(),
              ),
            ),
          )
        ],
      ),      
  }
}

Answer

Question answered by Jahidul I (source).

you can change with using setState and bind your Wrap widget with InkWll and call setState in the onTap function or you can pass a function to your _buildButtonsWithNames.

Expanded(
            child: Center(
              child: InkWell(
                onTap:(){
                   setState(() {
                    pressedText = "change Text"
                  });
                 },
                child: Wrap(
                children: _buildButtonsWithNames(),
                ),
              )
            ),
          )

Video Answers on YouTube

DART FLUTTER FLUTTER-LAYOUT
SHARE: