in Education by
I'm new to Flutter Redux, I got a problem and I have no idea how to deal with it at all! I extracted the main code to keep this simple - tap indicators to switch PageView, scroll PageView to synchronise the indicator. Here is my code: app state: class AppState { final List menuList; final int currentIndex; AppState({this.menuList, this.currentIndex}); } the reducers: AppState appReducer(AppState state, Object action) { return AppState( menuList: menuListReducer(state.menuList, action), currentIndex: currentIndexReducer(state.currentIndex, action)); } final menuListReducer = combineReducers( [TypedReducer(_setMenuList)]); List _setMenuList(List menuList, SetMenuListAction action) { menuList = action.menuList; return menuList; } final currentIndexReducer = combineReducers( [TypedReducer(_setCurrentIndex)]); int _setCurrentIndex(int currentIndex, SetCurrentIndexAction action) { currentIndex = action.index; return currentIndex; } the action: class SetMenuListAction { List menuList; SetMenuListAction(this.menuList); } class SetCurrentIndexAction { int index; SetCurrentIndexAction(this.index); } the main logic: void main() { final store = Store( appReducer, initialState: AppState(menuList: [ { 'picUrl': 'http://pic3.16pic.com/00/55/42/16pic_5542988_b.jpg', 'description': 'this is the first image' }, { 'picUrl': 'http://photo.16pic.com/00/38/88/16pic_3888084_b.jpg', 'description': 'this is the second image' }, { 'picUrl': 'http://img4.imgtn.bdimg.com/it/u=3434394339,2114652299&fm=214&gp=0.jpg', 'description': 'this is the third image' }, { 'picUrl': 'http://pic1.win4000.com/pic/2/07/8c57e143b1.jpg', 'description': 'this is the fourth image' }, ], currentIndex: 0), ); runApp(App( store: store, )); } // App class App extends StatelessWidget { final Store store; const App({Key key, this.store}) : super(key: key); @override Widget build(BuildContext context) { return StoreProvider( store: store, child: MaterialApp(title: 'Flutter redux example', home: MyDetail()), ); } } class MyDetail extends StatefulWidget { @override _MyDetailState createState() => _MyDetailState(); } class _MyDetailState extends State with TickerProviderStateMixin { PageController _controller; @override void initState() { _controller = PageController(initialPage: 0); super.initState(); } @override Widget build(BuildContext context) { return StoreConnector( converter: (store) => store.state.currentIndex, onDidChange: (newIdx) { //this won't work because the _controller hasn't been attached to PageView _controller.jumpToPage(newIdx); }, builder: (BuildContext context, int idx) { return StoreConnector( converter: (store) => store.state.menuList, onDidChange: (newList) { //maybe do something further }, builder: (BuildContext context, List menus) { return Container( color: Colors.white, child: Column( children: [ //pageview Expanded( child: PageView( children: menus.map((item) { return Column( children: [ Image.network(item['picUrl']), Text( item['description'], style: TextStyle(fontSize: 24.0), ) ], ); }).toList(), onPageChanged: (int index) { StoreProvider.of(context) .dispatch(SetCurrentIndexAction(index)); }, physics: BouncingScrollPhysics(), ), ), //indicators Container( margin: EdgeInsets.only(bottom: 50.0), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: menus .asMap() .map((i, item) => MapEntry( i, GestureDetector( onTap: () { //this won't work either maybe because the widgets is rebuilding _controller.jumpToPage(i); StoreProvider.of(context) .dispatch(SetCurrentIndexAction(i)); }, child: Container( width: 10.0, height: 10.0, color: i == idx ? Colors.purpleAccent : Colors.blue, margin: EdgeInsets.only(right: 10.0), ), ))) .values .toList(), ), ) ], ), ); }, ); }, ); } } Sorry for the long code, but I think maybe this can help to understand my problem: When I tap the indicator, I want to synchronise the PageView, that is _controller.jumpToPage(i), but it will show Errors. So how to make this work? I can change the currentIndex in another screen, how to synchronise the PageView? Is there any method to watch the state changes(separately, not the whole state) and do something? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
After debugging your code I found that you are missing controller: _controller in PageView, this should fix it: Expanded( child: PageView( controller: _controller, children: menus.map((item) { return Column( children: [ Image.network(item['picUrl']), Text( item['description'], style: TextStyle(fontSize: 24.0), ) ], ); }).toList(), onPageChanged: (int index) { StoreProvider.of(context) .dispatch(SetCurrentIndexAction(index)); }, physics: BouncingScrollPhysics(), ), ),

Related questions

0 votes
    I'm working on a program that displays a list from an SQFlite table. The future for the snapshot ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    I am trying to implement the reCaptcha function to my flutter app, but in the captcha registration I need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I need to build a pop up dialog box in my app. I am using Dialog class and wrapped it with ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I am adding an background image to the Splash screen, but is not rendering the image. Sometime it load ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I'm new to flutter and can't resolve the issue. Your application could not be compiled, because its ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I'm new to flutter and can't resolve the issue. Your application could not be compiled, because its ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 15, 2022 in Education by JackTerrance
0 votes
    I have this operation EventModel data = EventModel(_nameEvent, _passEvent, _localEvent, _dateEventString); HashMap ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I'm getting Authorization failure when trying to display a map in a flutter project using the google maps ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Video does not capture in the right orientation when running the Camera Plugin example app on my iPhone X in Landscape. It works well ... .0+1 environment: sdk: ">=2.0.0-dev.68.0...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Currently, when I submit a redux-form, and send the data-transfer-object to the backend, there is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    __________ function is used to watch for all available packages in library. (a) lib() (b) fun.lib() (c ... Started of R Programming Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    national human rights commission is the watch dog of human rights . substantiate Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    A watch which gains uniformly is 2 minutes low at noon on Monday and is 4 min. 48 sec fast at 2 p.m. on the following Monday ... Wednesday C) 3 p.m. on Thursday D) 1 p.m. on Friday...
asked Feb 13, 2021 in Education by JackTerrance
0 votes
    If an transaction is performed in a database and committed, the changes are taken to the previous state ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
...