0
respostas

[Dúvida] Resetando os níveis ao scrollar a tela.

O código todo está rodando certinho, porém ao scrollar a tela, os níveis setados voltam a 0. Já fiz o que recomendaram em uma duvida igual a minha, porém não funcionou. Meu código está assim:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.lightBlue,
          title: Text(
            'Lista de Tarefas',
            style: TextStyle(fontSize: 30),
          ),
        ),
        body: ListView(
          children: [
            Task('Aprender Flutter'),
            Task('Meditar'),
            Task('Andar de bike'),
            Task('Pescar'),
            Task('Ajudar Idosos'),
            Task('Ir para a igreja'),
            Task('Correr'),
            Task('Estudar React'),
            Task('Estudar Cybersecurity'),
            Task('Passear com as cachorras'),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
        ),
      ),
    );
  }
}

class Task extends StatefulWidget {
  final String nome;

  const Task(this.nome, {super.key});

  @override
  State<Task> createState() => _TaskState();
}

class _TaskState extends State<Task> {
  int nivel = 0;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Container(
        child: Stack(
          children: [
            Container(
              color: Colors.blue,
              width: 400,
              height: 140,
            ),
            Column(
              children: [
                Container(
                  color: Colors.white,
                  width: 380,
                  height: 100,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        color: Colors.black12,
                        width: 80,
                        height: 100,
                      ),
                      Container(
                          alignment: Alignment.center,
                          width: 180,
                          child: Text(widget.nome,
                              style: TextStyle(
                                fontSize: 20,
                                overflow: TextOverflow.ellipsis,
                              ))),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            nivel++;
                          });
                          print(nivel);
                        },
                        child: Icon(Icons.arrow_drop_up),
                      )
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        child: LinearProgressIndicator(
                          color: Colors.white,
                          value: nivel/10 ,
                        ),
                        width: 200,
                      ),
                      Text(
                        'Nivel $nivel',
                        style: TextStyle(color: Colors.white, fontSize: 16),
                      ),
                    ],
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}