Senior Software Engineer @ Originate
class MyHomepage extends StatefulWidget {
@override
_MyHomepageState createState() => _MyHomepageState();
}
class _MyHomepageState extends State<MyHomepage> {
int _index = 0;
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
currentIndex: _index,
onTap: (newIndex) {
setState(() {
_index = newIndex;
});
},
// ... items ...
);
}
}
@override
Widget build(BuildContext context) {
return SomeWidget(
// Construct the widget, passing it a reference to the method above.
MyListItem(myTapCallback),
);
}
void myTapCallback(Item item) {
print('user tapped on $item');
}
class CounterState extends InheritedWidget {
CounterState({Key key, this.count, this.incrementer, this.decrementer, Widget child})
: super(key: key, child: child);
final int count;
final Function incrementer;
final Function decrementer;
@override
bool updateShouldNotify(CounterState oldWidget) {
return count != oldWidget.count;
}
static of(BuildContext context) {
return context.inheritFromWidgetOfExactType(CounterState);
}
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int count = 0;
void incrementer() {
setState(() {
count++;
});
}
void decrementer() {
setState(() {
count--;
});
}
@override
Widget build(BuildContext context) {
return CounterState(
count: count,
incrementer: incrementer,
decrementer: decrementer,
child: HomePage(),
);
}
}
class CountSection extends StatelessWidget {
@override
Widget build(BuildContext context) {
final counterState = CounterState.of(context);
return new Text('${counterState.count}');
}
}
class ButtonSection extends StatelessWidget {
@override
Widget build(BuildContext context) {
final counterState = CounterState.of(context);
return new Column(
children: <Widget>[
ButtonBar(
children: <Widget>[
RaisedButton(child: Text('incrementer'),
onPressed: counterState.incrementer,),
RaisedButton(child: Text('decrementer'),
onPressed: counterState.decrementer,),
],
),
],
);
}
}
// actions
class IncrementCountAction {}
class DecrememtCountAction {}
// reducer
int counterReducer(int currentCount, action) {
if (action is IncrementCountAction) {
currentCount++;
return currentCount;
} else if (action is DecrememtCountAction) {
currentCount--;
return currentCount;
} else {
return currentCount;
}
}
// app state
class AppState {
final int count;
AppState({this.count = 0});
}
// app reducer
AppState appReducer(AppState state, action) {
return new AppState(
count: counterReducer(state.count, action),
);
}
// main app
class MainApp extends StatelessWidget {
final store = new Store<AppState>(
appReducer,
initialState: new AppState(),
middleware: [],
);
@override
Widget build(BuildContext context) {
return new StoreProvider(
store: store,
child: new MaterialApp(
title: 'title',
home: new HomePage(),
),
);
}
}
class IncreaseCountButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new StoreConnector<AppState, VoidCallback>(
converter: (Store<AppState> store) {
return () {
store.dispatch(new IncrementCountAction());
};
},
builder: (BuildContext context, VoidCallback increase) {
return new FloatingActionButton(
onPressed: increase,
child: new Icon(Icons.add),
);
},
);
}
}
class Counter extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new StoreConnector<AppState, _ViewModel>(
converter: _ViewModel.fromStore,
builder: (BuildContext context, _ViewModel vm) {
return new Text(vm.count.toString());
}
);
}
}
class _ViewModel {
final int count;
_ViewModel({
@required this.count,
});
static _ViewModel fromStore(Store<AppState> store) {
return new _ViewModel(count: store.state.count);
}
}
void main() {
runApp(
// Provide the model to all widgets within the app
ChangeNotifierProvider(
// Initialize the model in the builder
builder: (context) => Counter(),
child: MyHomePage(),
),
);
}
class Counter with ChangeNotifier {
int value = 0;
void increment() {
value += 1;
notifyListeners();
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('You have pushed the button this many times:'),
Consumer<Counter>(
builder: (context, counter, child) => Text(
'${counter.value}',
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () =>
Provider.of<Counter>(context, listen: false).increment(),
child: Icon(Icons.add),
),
);
}
}
// Model
class Counter {
int value = 0;
}
// ScopedModel
import 'package:scoped_model/scoped_model.dart';
class ScopedCounter extends Model {
Counter counter = Counter();
increment() {
counter.value += 1;
notifyListeners();
}
}
class MyHomePage extends StatelessWidget {
final ScopedCounter scopedCounter = ScopedCounter();
@override
Widget build(BuildContext context) {
return ScopedModel(
model: scopedCounter,
child: Scaffold(
appBar: AppBar(
title: Text('Hello World with Scoped Model'),
),
body: Center(
child: WidgetCounter(),
),
floatingActionButton: FloatingActionButton(
onPressed: () => scopedCounter.increment(),
tooltip: 'Increment',
child: Icon(Icons.add),
),
),
);
}
}
class WidgetCounter extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModelDescendant(
builder: (context, child, model) =>
Text('WidgetCounter counter is ${model.counter.count}'));
}
}
ScopedModel.of<ScopedCounter>(context, rebuildOnChange: false)
class CounterProvider {
int count = 0;
void increaseCount() => count++;
}
class CounterBloc {
final counterController = StreamController();
final CounterProvider provider = CounterProvider();
Stream get getCount => counterController.stream;
// the rxdart stream controllers returns an Observable
void updateCount() {
provider.increaseCount();
counterController.sink.add(provider.count);
}
void dispose() {
// close our StreamController to avoid memory leak
counterController.close();
}
}
final bloc = CounterBloc();
// import file that contains our bloc
class Counter extends StatefulWidget {
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> {
@override
void dispose() {
bloc.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: bloc.getCount,
initialData: CounterProvider().count,
builder: (context, snapshot) => Center(
child: Column(
children: <Widget>[
Text('You have pushed the button this many times:'),
Text('${snapshot.data}'),
],
),
),
);
}
}
class Counter {
Counter() {
increment = Action(_increment);
}
final _value = Observable(0);
int get value => _value.value;
set value(int newValue) => _value.value = newValue;
Action increment;
void _increment() {
_value.value++;
}
}
part 'counter.g.dart';
// _$Counter comes from generated code included above
class Counter = CounterBase with _$Counter;
abstract class CounterBase with Store {
@observable
int value = 0;
@action
void increment() {
value++;
}
}
class CounterWidget extends StatefulWidget {
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<CounterWidget> {
final Counter counter = Counter();
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('MobX Counter')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Observer(builder: (_) => Text('${counter.value}')),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: counter.increment,
child: const Icon(Icons.add),
),
);
}