Created
December 6, 2018 03:00
-
-
Save nyancodeid/a68d794d4750a16192c126a8e0a45e20 to your computer and use it in GitHub Desktop.
Flutter Dart Redux List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:redux/redux.dart'; | |
| import 'package:flutter_redux/flutter_redux.dart'; | |
| import 'globals.dart'; | |
| class User { | |
| String userName; | |
| String userEmail; | |
| User({ this.userName, this.userEmail }); | |
| } | |
| class ListState { | |
| final List<String> items; | |
| ListState({ this.items }); | |
| ListState.initialState() : items = []; | |
| } | |
| class AddAction { | |
| final String input; | |
| AddAction({ this.input }); | |
| } | |
| ListState reducer(ListState state, action) { | |
| if (action is AddAction) { | |
| return ListState( | |
| items: [] | |
| .. addAll(state.items) | |
| .. add(action.input) | |
| ); | |
| } | |
| return ListState(items: state.items); | |
| } | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| final store = Store<ListState>(reducer, initialState: ListState.initialState()); | |
| @override | |
| Widget build(BuildContext context) { | |
| return StoreProvider<ListState>( | |
| store: store, | |
| child: MaterialApp( | |
| title: appName, | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| initialRoute: '/', | |
| routes: { | |
| '/': (context) => Home(title: appHomePageTitle), | |
| '/login': (context) => LoginPage(title: "Login"), | |
| }, | |
| ), | |
| ); | |
| } | |
| } | |
| typedef AddItem(String text); | |
| class _ViewModel { | |
| final AddItem addItemIntoList; | |
| _ViewModel({ this.addItemIntoList }); | |
| } | |
| class ListInput extends StatefulWidget { | |
| @override | |
| ListInputState createState() => ListInputState(); | |
| } | |
| class ListInputState extends State<ListInput> { | |
| final TextEditingController controller = TextEditingController(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return StoreConnector<ListState, _ViewModel>( | |
| converter: (store) => _ViewModel( | |
| addItemIntoList: (inputText) => store.dispatch(AddAction(input: inputText)) | |
| ), | |
| builder: (context, viewModel) { | |
| return TextField( | |
| controller: controller, | |
| onSubmitted: (text) { | |
| viewModel.addItemIntoList(text); | |
| controller.text = ""; | |
| }, | |
| ); | |
| }, | |
| ); | |
| } | |
| } | |
| class ViewList extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return StoreConnector<ListState, List<String>> ( | |
| converter: (store) => store.state.items, | |
| builder: (context, items) => Column(children: items.map((i) => ListTile(title: Text(i))).toList(), | |
| ), | |
| ); | |
| } | |
| } | |
| class Home extends StatelessWidget { | |
| Home({Key key, this.title}) : super(key: key); | |
| final String title; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(title), | |
| ), | |
| body: Center( | |
| child: Column( | |
| children: <Widget>[ | |
| ListInput(), | |
| ViewList() | |
| ], | |
| ) | |
| ) | |
| ); | |
| } | |
| } | |
| class LoginInput extends StatefulWidget { | |
| @override | |
| LoginInputState createState() => LoginInputState(); | |
| } | |
| class LoginInputState extends State<LoginInput> { | |
| final TextEditingController controller = TextEditingController(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return StoreConnector<ListState, _ViewModel>( | |
| converter: (store) => _ViewModel( | |
| addItemIntoList: (inputText) => store.dispatch(AddAction(input: inputText)) | |
| ), | |
| builder: (context, viewModel) { | |
| return TextField( | |
| controller: controller, | |
| onSubmitted: (text) { | |
| viewModel.addItemIntoList(text); | |
| controller.text = ""; | |
| }, | |
| ); | |
| }, | |
| ); | |
| } | |
| } | |
| class LoginPage extends StatelessWidget { | |
| LoginPage({Key key, this.title}) : super(key: key); | |
| final String title; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(title), | |
| ), | |
| body: Center( | |
| child: new Container( | |
| child: Column( | |
| children: <Widget>[ | |
| LoginInput(), | |
| ], | |
| ), | |
| ) | |
| ) | |
| ); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment