Skip to content

Instantly share code, notes, and snippets.

View chimon2000's full-sized avatar

Ryan chimon2000

View GitHub Profile
@chimon2000
chimon2000 / notes.viewmodel.dart
Created November 1, 2020 06:52
state_example: flutter command viewmodel
class NotesViewModel {
NotesState state = NotesState.initial();
Command<String, String> inputChangedCommand;
Command<String, NotesState> updateNotesCommand;
NotesViewModel() {
inputChangedCommand = Command.createSync((x) => x, '');
updateNotesCommand = Command.createSync((input) {
state = state.copyWith(notes: state.notes.concat(input));
@chimon2000
chimon2000 / notes.dart
Created November 1, 2020 06:55
state_example: mobx note store
part 'notes.g.dart';
class Notes = NotesBase with _$Notes;
abstract class NotesBase with Store {
@observable
String input = '';
@observable
ObservableList<String> notes = ObservableList();
@chimon2000
chimon2000 / list.extension.dart
Last active November 2, 2020 06:47
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]);
}
@chimon2000
chimon2000 / main.dart
Last active November 3, 2020 04:33
state_examples: riverpod main root
void main() {
// For widgets to be able to read providers, we need to wrap the entire
// application in a "ProviderScope" widget.
// This is where the state of our providers will be stored.
runApp(ProviderScope(child: App()));
}
@chimon2000
chimon2000 / main.dart
Last active November 3, 2020 04:36
state_example: binder root
void main() {
// For widgets to be able to read app state, we need to wrap the entire
// application in a "BinderScope" widget.
runApp(BinderScope(child: App()));
}
@chimon2000
chimon2000 / notes.controller.dart
Last active November 3, 2020 04:38
notes controller
// We create a "provider", which will store a reference to NotesController.
final notesProvider = StateNotifierProvider((ref) => NotesController());
class NotesController extends StateNotifier<NotesState> {
NotesController() : super(NotesState.initial());
void addNote() {
var notes = state.notes.concat(state.input);
state = state.copyWith(notes: notes, input: '');
}
@chimon2000
chimon2000 / main.dart
Last active November 3, 2020 04:40
state_example: bloc main
void main() {
// For widgets to be able to read providers, we need to wrap the entire
// application in a "BlocProvider" widget.
// This is where the state of our NotesCubit will be stored.
runApp(BlocProvider<NotesCubit>(create: (_) => NotesCubit(), child: App()));
}
class RiverpodPage extends StatefulWidget {
const RiverpodPage({Key key}) : super(key: key);
@override
_RiverpodPageState createState() => _RiverpodPageState();
}
class _RiverpodPageState extends State<RiverpodPage> {
TextEditingController _controller;
@chimon2000
chimon2000 / binder.page.dart
Last active November 3, 2020 04:45
state_example: binder page
class BinderPage extends StatefulWidget {
const BinderPage({Key key}) : super(key: key);
@override
_BinderPageState createState() => _BinderPageState();
}
class _BinderPageState extends State<BinderPage> {
TextEditingController _controller;
@chimon2000
chimon2000 / notes.viewlogic.dart
Last active November 3, 2020 05:29
state_example: notes viewlogic
// We create a "logic ref", which will store a reference to NotesViewLogic.
final notesViewLogicRef = LogicRef((scope) => NotesViewLogic(scope));
class NotesViewLogic with Logic {
const NotesViewLogic(this.scope);
@override
final Scope scope;
void addNote() {