Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Updating metodo

Estou querendo atualizar um dado chamado nivel que tem autoincremento, todas vez que houver uma ação de click no botão UP. Porém não faço ideia de como no ElevatedButton pegar o id e o novo nivel para o metoda update. Veja o código abaixo.

Alguém pode me ajudar?

class Task extends StatefulWidget {
  final String nome;
  final String foto;
  final int dificultade;
  final int nivel;

  const Task(this.nome, this.foto, this.dificultade, this.nivel, {Key? key})
      : super(key: key);

  //int nivel = 0;
  @override
  State<Task> createState() => _TaskState();
}

class _TaskState extends State<Task> {
  bool assetOrNetwork() {
    if (widget.foto.contains('http')) {
      return false;
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Stack(
        children: [
          Container(
            decoration: BoxDecoration(
                color: ((widget.nivel / widget.dificultade) / 10) <= 1
                    ? Colors.blue
                    : ((widget.nivel / widget.dificultade) / 10) <= 2
                        ? Colors.yellow
                        : Colors.purple,
                borderRadius: BorderRadius.circular(4)),
            height: 140,
          ),
          Column(
            children: [
              Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(4)),
                height: 100,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(4),
                        color: Colors.black26,
                      ),
                      width: 72,
                      height: 100,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(4),
                        child: assetOrNetwork()
                            ? Image.asset(widget.foto, fit: BoxFit.cover)
                            : Image.network(
                                widget.foto,
                                fit: BoxFit.cover,
                              ),
                      ),
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                            width: 200,
                            child: Text(
                              widget.nome,
                              style: const TextStyle(
                                  fontSize: 24,
                                  overflow: TextOverflow.ellipsis),
                            )),
                        Difficulty(
                          difficultyLevel: widget.dificultade,
                        ),
                      ],
                    ),
                    SizedBox(
                      width: 52,
                      height: 52,
                      child: ElevatedButton(
                          onPressed: () {
                            setState(() {
                              widget.nivel;
                            });
                          },
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: const [
                              Icon(Icons.arrow_drop_up),
                              Text(
                                'UP',
                                style: TextStyle(fontSize: 12),
                              )
                            ],
                          )),
                    )
                  ],
                ),
              ),
              Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SizedBox(
                    width: 200,
                    child: LinearProgressIndicator(
                      color: Colors.white,
                      value: (widget.dificultade > 0)
                          ? (widget.nivel / widget.dificultade) / 10
                          : 1,
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(12.0),
                  child: Text('Nivel: ${widget.nivel}',
                      style:
                          const TextStyle(color: Colors.white, fontSize: 16)),
                )
              ]),
            ],
          )
        ],
      ),
    );
  }
}


class Tarefa {
  final int id;
  final String name;
  final int difficulty;
  final String image;
  final int nivel;

  Tarefa(this.id,this.name, this.difficulty, this.image,this.nivel);

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
      'difficulty': difficulty,
      'image': image,
      'nivel': nivel,
    };
  }



  @override
  String toString() {
    return 'Tarefa{id:$id, name: $name, difficulty: $difficulty, image: $image, nivel: $nivel}';
  }
}

Future<void> update(Tarefa tarefa) async {
  // Get a reference to the database.
  final db = await getDatabase();

  // Update the given Dog.
  await db.update(
    'tarefas',
    tarefa.toMap(),
    // Ensure that the Dog has a matching id.
    where: 'id = ?',
    // Pass the Dog's id as a whereArg to prevent SQL injection.
    whereArgs: [tarefa.id],
  );
}
1 resposta
solução!

Consegui ajustando o ElevateButton conforme abaixo. Updeting realizando como esperado.

ElevatedButton(
                          onPressed: () {
                            widget.nivel++;
                            final Tarefa newTarefa = Tarefa(
                                widget.id,
                                widget.nome,
                                widget.dificultade,
                                widget.foto,
                                widget.nivel);
                            update(newTarefa);

                            setState(() {
                              widget.nivel;
                            });
                           // print(widget.nivel);
                          }