Skip to content

Instantly share code, notes, and snippets.

@chimon2000
Last active November 2, 2020 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chimon2000/edb85b52005901db927bd0fd96d7db21 to your computer and use it in GitHub Desktop.
Save chimon2000/edb85b52005901db927bd0fd96d7db21 to your computer and use it in GitHub Desktop.
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