Skip to content

Instantly share code, notes, and snippets.

Avatar

Ryan chimon2000

View GitHub Profile
@chimon2000
chimon2000 / base_command.dart
Created Oct 27, 2020
Command Pattern in Dart (binder)
View base_command.dart
abstract class BaseCommand<T> {
BuildContext _context;
BaseCommand(this._context);
T locate<T>(LogicRef<T> ref) => _context.use(ref);
T read<T>(StateRef<T> ref) => _context.read(ref);
Future<T> run();
}
View notes.controller.dart
// 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 Nov 3, 2020
state_examples: riverpod main root
View main.dart
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 / list.extension.dart
Last active Nov 2, 2020
state_examples: common classes
View list.extension.dart
// 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]);
}
View riverpod.page.dart
class RiverpodPage extends StatefulWidget {
const RiverpodPage({Key key}) : super(key: key);
@override
_RiverpodPageState createState() => _RiverpodPageState();
}
class _RiverpodPageState extends State<RiverpodPage> {
TextEditingController _controller;
@chimon2000
chimon2000 / notes.cubit.dart
Created Nov 1, 2020
state_examples: notes cubit
View notes.cubit.dart
class NotesCubit extends Cubit<NotesState> {
NotesCubit(): super(NotesState.initial());
void addNote() {
emit(state.copyWith(notes: state.notes.concat(state.input), input: ''));
}
void updateInput(String input) => emit(state.copyWith(input: input));
}
@chimon2000
chimon2000 / bloc.page.dart
Last active Nov 3, 2020
state_examples bloc page
View bloc.page.dart
class BlocPage extends StatefulWidget {
const BlocPage({Key key}) : super(key: key);
@override
_BlocPageState createState() => _BlocPageState();
}
class _BlocPageState extends State<BlocPage> {
TextEditingController _controller;
@chimon2000
chimon2000 / flutter_command.page.dart
Last active Nov 3, 2020
state_examples: flutter command page
View flutter_command.page.dart
class FlutterCommandPage extends StatefulWidget {
const FlutterCommandPage({Key key}) : super(key: key);
@override
_FlutterCommandPageState createState() => _FlutterCommandPageState();
}
class _FlutterCommandPageState extends State<FlutterCommandPage> {
final _notesViewModel = NotesViewModel();
TextEditingController _controller;
@chimon2000
chimon2000 / notes.viewmodel.dart
Created Nov 1, 2020
state_example: flutter command viewmodel
View notes.viewmodel.dart
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 Nov 1, 2020
state_example: mobx note store
View notes.dart
part 'notes.g.dart';
class Notes = NotesBase with _$Notes;
abstract class NotesBase with Store {
@observable
String input = '';
@observable
ObservableList<String> notes = ObservableList();