skip to Main Content
`import 'package:flutter/material.dart';

class SkillEntry extends StatefulWidget {
   const SkillEntry({super.key, required this.skll});

   final String skll;

  @override
  State<SkillEntry> createState() => _SkillEntryState();
}

class _SkillEntryState extends State<SkillEntry> {


  @override
  Widget build(BuildContext context) {

     int skillvalue = 0;

    return Container(
        height: 40,
        width: 200,
        child: Row(children: [
          const Flexible(flex: 20, child:
          TextField(decoration:(
              InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: ('Name and Skill'))),)),
          ElevatedButton(onPressed: PlusOne(skillvalue), child: Icon(Icons.add_circle)),
          ElevatedButton(onPressed: MinusOne(skillvalue), child: Icon(Icons.exposure_minus_1)),
          Flexible(flex: 7, child:
           Text('$skillvalue',)),
        ]));
  }
  PlusOne(skillvalue) {
    setState(() {
      skillvalue = skillvalue + 1;
      return skillvalue;
      });
  }

  MinusOne(skillvalue) {
    setState(() {
      skillvalue = skillvalue - 1;
      return skillvalue;
    });
  }
}

As it stands currently the buttons seemingly do nothing. I suspect that I’m getting the skillvalue variable into the function PlusOne but not properly returning it to the main program, or I might not be using setstate correctly. How can I solve this?

2

Answers


  1. The problem is that your skillValue is declared within your build method:

     @override
     Widget build(BuildContext context) {
     int skillvalue = 0;
    

    And the build method re-runs every setState. So skillValue will be reset to the value you set it every time you update the UI.

    To solve the problem, move the skillValue in the State class:

    class _SkillEntryState extends State<SkillEntry> {
    
        int skillvalue = 0;
        ...
    

    instead of the build method

    Login or Signup to reply.
  2. first of all, skillValue is declared in build method, the value will always be zero.

    secondly, the way you use ElevatedButton is wrong, you can not use a void method return value as a parameter to onPressed, which means the onPressed always be null.

    here is my demo code.

    class SkillEntry extends StatefulWidget {
      const SkillEntry({super.key, required this.skll});
    
      final String skll;
    
      @override
      State<SkillEntry> createState() => _SkillEntryState();
    }
    
    class _SkillEntryState extends State<SkillEntry> {
    
      int skillvalue = 0;
      @override
      Widget build(BuildContext context) {
    
    
    
        return Container(
            height: 40,
            width: 200,
            child: Row(children: [
              const Flexible(flex: 20, child:
              TextField(decoration:(
                  InputDecoration(
                      border: OutlineInputBorder(),
                      hintText: ('Name and Skill'))),)),
              ElevatedButton(onPressed: PlusOne, child: Icon(Icons.add_circle)),
              ElevatedButton(onPressed: MinusOne, child: Icon(Icons.exposure_minus_1)),
              Flexible(flex: 7, child:
              Text('$skillvalue',)),
            ]));
      }
      PlusOne() {
        setState(() {
          skillvalue = skillvalue + 1;
        });
      }
    
      MinusOne() {
        setState(() {
          skillvalue = skillvalue - 1;
        });
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search