Skip to content

Instantly share code, notes, and snippets.

@bobagold
Last active October 28, 2019 16:13
Show Gist options
  • Save bobagold/4c1494b7bada2c621e0c2ff82a550c0b to your computer and use it in GitHub Desktop.
Save bobagold/4c1494b7bada2c621e0c2ff82a550c0b to your computer and use it in GitHub Desktop.
example of pure widgets in flutter
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
void main() => runApp(ExampleApp());
// <framework-specific part>
typedef MetaBuilder<ViewModel> = Widget Function(ViewModelBuilder<ViewModel>);
int counterReducer(int state, dynamic action) =>
state + (action is int ? action : 0);
Store<int> store = Store<int>(counterReducer, initialState: 0);
Widget counterConnector(ViewModelBuilder<int> builder) =>
StoreConnector<int, int>(
builder: builder,
converter: (store) => store.state,
distinct: true,
);
void increment() => store.dispatch(1);
Widget provideStore(Widget child) => StoreProvider(store: store, child: child);
// </framework-specific part>
class ExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) => provideStore(MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: "/",
routes: {
"/": (context) => HomePage(),
},
));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) => Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: increment,
child: Icon(Icons.add),
),
// here we bind Widget with connectors much like in React
body:
Center(child: CounterWidget<int>(counterBuilder: counterConnector)));
}
// framework-agnostic widget that does not use streams and can repaint partially
// it even can accept anything as counter
class CounterWidget<ViewModel> extends StatelessWidget {
/// see [Builder] as example
final MetaBuilder<ViewModel> counterBuilder;
const CounterWidget({Key key, this.counterBuilder}) : super(key: key);
@override
Widget build(BuildContext context) =>
Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
Text('This text will not be repaint'),
counterBuilder((context, counter) => Text('clicked $counter times')),
Text('And we don\'t use streams'),
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment