Home > database >  How to show a string variable in flutter which changes when a button is pressed?
How to show a string variable in flutter which changes when a button is pressed?

Time:11-28

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(),
              ),
            ),
          )
        ],
      ),      
  }
}

CodePudding user response:

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(),
                ),
              )
            ),
          )
  • Related