variable won't change Text dynamically in flutter

前端 未结 1 1682
予麋鹿
予麋鹿 2021-01-15 04:27

I have my application defined, and I pass counter variable as a constructor like below:

class AppThreePlusThree extends StatelessWidget {
  @ove         


        
相关标签:
1条回答
  • 2021-01-15 05:02

    You have to make your StatelessWidget widget to StatefulWidget because that is how layout rebuild when state of counter variable changed(using setState()). Your code should look like below,

    class AppThreePlusThree extends StatefulWidget {
      _AppThreePlusThreeState createState() => _AppThreePlusThreeState ();
    }
    
    class _AppThreePlusThreeState extends State<AppThreePlusThree> {
      var counter = 265;
      var counter2 = 265;
    
      void changeVariableOnUI() {
        setState(() => counter2 = 22); 
      }
    
      @override
      Widget build(BuildContext context) {
        var game = new Game();
        // Inside the build method you cannot (no need) use setState but outside the build() to update a variable value on the UI have to use setState
        counter = 265; //I seriously doesnt have any idea how you are going to change or update your counter variable. something like this should work
        return new GameRedux(
          counter: counter,
          child: new MaterialApp(
            title: '3 + 3',
            theme: new ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: new Scaffold(
         ...
         ...
         Text('$counter'),
         Text('$counter2'),
         InkWell(
           child: Text("Tap here"),
           onTap: changeVariableOnUI,
         ),
         ...
    
    0 讨论(0)
提交回复
热议问题