Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You need deep copy and immutable object #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 56 additions & 37 deletions change_notifier_provider/lib/home/todo_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,66 @@ class TodoListView extends StatelessWidget {
itemBuilder: (context, index) {
final todo = todos[index];

return Dismissible(
key: ArchSampleKeys.todoItem(todo.id),
onDismissed: (_) => onRemove(context, todo),
child: ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) {
return DetailsScreen(
id: todo?.id,
onRemove: () {
Navigator.pop(context);
onRemove(context, todo);
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Dismissible(
key: ArchSampleKeys.todoItem(todo.id),
onDismissed: (_) => onRemove(context, todo),
child: ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) {
return DetailsScreen(
id: todo?.id,
onRemove: () {
Navigator.pop(context);
onRemove(context, todo);
},
);
},
);
),
);
},
leading: Checkbox(
key: ArchSampleKeys.todoItemCheckbox(todo.id),
value: todo.complete,
onChanged: (complete) {
Provider.of<TodoListModel>(context, listen: false)
.updateTodo(todo.copy(complete: complete));
},
),
);
},
leading: Checkbox(
key: ArchSampleKeys.todoItemCheckbox(todo.id),
value: todo.complete,
onChanged: (complete) {
Provider.of<TodoListModel>(context, listen: false)
.updateTodo(todo.copy(complete: complete));
},
),
title: Text(
todo.task,
key: ArchSampleKeys.todoItemTask(todo.id),
style: Theme.of(context).textTheme.title,
),
subtitle: Text(
todo.note,
key: ArchSampleKeys.todoItemNote(todo.id),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.subhead,
title: Text(
todo.task,
key: ArchSampleKeys.todoItemTask(todo.id),
style: Theme.of(context).textTheme.title,
),
subtitle: Text(
todo.note,
key: ArchSampleKeys.todoItemNote(todo.id),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.subhead,
),
),
),
),
for(final person in todo.assignInfo.person)
ListTile(
title: Text('Person $person'),
trailing: IconButton(
icon: Icon(Icons.delete_forever),
onPressed: () {
Provider.of<TodoListModel>(context, listen: false)
.updateTodo(todo.copy(assignInfo: todo.assignInfo.copy(person: todo.assignInfo.person..remove(person))));
/// CORRECT WAY
// Provider.of<TodoListModel>(context, listen: false)
// .updateTodo(todo.copy(assignInfo: todo.assignInfo.copy(person: List.of(todo.assignInfo.person)..remove(person))));
},
),
)
],
);
},
);
Expand Down
29 changes: 24 additions & 5 deletions change_notifier_provider/lib/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,30 @@ class Todo {
final String id;
final String note;
final String task;
final AssignInfo assignInfo;

Todo(this.task, {this.complete = false, this.note = '', String id})
: id = id ?? Uuid().generateV4();
Todo(this.task, {this.complete = false, this.note = '', String id, AssignInfo assignInfo})
: id = id ?? Uuid().generateV4(),
assignInfo = assignInfo ?? AssignInfo(Person.values);

@override
int get hashCode =>
complete.hashCode ^ task.hashCode ^ note.hashCode ^ id.hashCode;
complete.hashCode ^ task.hashCode ^ note.hashCode ^ assignInfo.hashCode ^ id.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Todo &&
runtimeType == other.runtimeType &&
complete == other.complete &&
assignInfo == other.assignInfo &&
task == other.task &&
note == other.note &&
id == other.id;

@override
String toString() {
return 'Todo{complete: $complete, task: $task, note: $note, id: $id}';
return 'Todo{complete: $complete, task: $task, note: $note, assignInfo: $assignInfo, id: $id}';
}

TodoEntity toEntity() {
Expand All @@ -50,12 +53,28 @@ class Todo {
);
}

Todo copy({String task, bool complete, String note, String id}) {
Todo copy({String task, bool complete, String note, String id, AssignInfo assignInfo}) {
return Todo(
task ?? this.task,
complete: complete ?? this.complete,
note: note ?? this.note,
assignInfo: assignInfo?.copy() ?? this.assignInfo?.copy(),
id: id ?? this.id,
);
}
}

class AssignInfo {
final List<Person> person;

AssignInfo(this.person);

AssignInfo copy({List<Person> person}) {
final newPerson = person ?? this.person;
return AssignInfo(newPerson != null ? List.of(newPerson) : null);
}
}

enum Person {
sasha, masha, dasha
}