state_examples: common classes
// A simple helper function to allow us to immutably add to lists. | |
extension ImmutableList<T> on List<T> { | |
List<T> concat(T item) => List<T>.from(<T>[...this, item]); | |
} |
// A simple widget for displaying individual notes. | |
class Note extends StatelessWidget { | |
final String text; | |
const Note({Key key, this.text}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Padding( | |
padding: const EdgeInsets.symmetric(vertical: 8.0), | |
child: Text('Note: $text'), | |
); | |
} | |
} |
// You can add equatable as a dependency to your pubspec.yaml | |
// https://pub.dev/packages/equatable/install | |
class NotesState extends Equatable { | |
final List<String> notes; | |
final String input; | |
NotesState( | |
this.notes, | |
this.input, | |
); | |
@override | |
List<Object> get props => [notes, input]; | |
@override | |
bool get stringify => true; | |
NotesState copyWith({ | |
List<String> notes, | |
String input, | |
}) { | |
return NotesState( | |
notes ?? this.notes, | |
input ?? this.input, | |
); | |
} | |
NotesState.initial() | |
: notes = [], | |
input = ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment