How to clone object which is passed from parent widget in Flutter/Dart

Viewed 462

I am Trying to build a simple ToDo-App. When I update my copied ToDo, the passed object updates. When I update the original the copied also change. I want a possibility to "discard changes". But always my passed object updates. I need a solution to clone that object, and only change the passed object, when I press "save".

My Code:

class _ToDayToDo extends State<ToDayToDo> {
...
//I call
    Future<void> _editToDos(ToDos selectedToDo) async {
      await showDialog(
          context: context,
          builder: (_) {
            return ShowActualToDo(selectedToDo);
          }).then((value) => setState(() {}));
    }
...
}

In ActualToDo:

class ShowActualToDo extends StatefulWidget {

  final ToDos editingToDo;

  ShowActualToDo(this.editingToDo);
  @override
  _ShowActualToDoState createState() => _ShowActualToDoState();
}

class _ShowActualToDoState extends State<ShowActualToDo> {
  ToDos editingToDo;

  @override
  void initState() {
    super.initState();

    editingToDo = widget.editingToDo;
    unsavedToDo = editingToDo.copyWith();

  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      content: Container(
        child: _selectedPage(),
      ),
    );
  }

  Widget _openEditSelectedToDo(ToDos selectedToDo) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8.0),
      child: ListView(
        children: [
          SizedBox(height: 20.0),
          Center(child: HeaderWidget("Bevorzugte Person", Colors.grey[800])),
          ToDoPersonsButtonWidget(selectedToDo, _changePerson),
          SizedBox(height: 20.0),
          Center(child: HeaderWidget("ToDo Steps", Colors.grey[800])),
          EditSelectedToDoWidget(selectedToDo),
        ],
      ),
    );
  }

...
}

My ToDo-Object Class:

class ToDo{
  String name;
  String img;
  String info;
  Map<String, bool> persons;
  List<Steps> steps;

  ToDo({
    this.name,
    this.img,
    this.info,
    this.persons,
    this.steps,
  });

  ToDo copyWith({
    String name,
  String img,
  String info,
  Map<String, bool> persons,
  List<Steps> steps,
  }) {
    return ToDo(
      name: name ?? this.name,
      img: img ?? this.img,
      info: info ?? this.info,
      persons: persons ?? this.persons,
      steps: steps ?? this.steps,
    );
  }
}
0 Answers
Related