Flutter switch between fragments by supporting back to previous fragment. I fixed some logic in your code please carefully check the changes, if you have any question don’t hesitate, here is the working code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( title: 'AndroidMonks', home: Scaffold( appBar: AppBar( title: Text('Androidmonks'), backgroundColor: Colors.orangeAccent, ), body: Home(), ), )); } class Home extends StatefulWidget { Home({ Key key, }) : super(key: key); @override State<Home> createState()=>_Home(); } class _Home extends State<Home> { String title = "Title"; List<Widget> _fragments =[Fragment1(),Fragment2(),Fragment3()]; int _currentIndex = 0; final List<int> _backstack = [0]; @override Widget build(BuildContext context) { //navigateTo(_currentIndex); //each fragment is just a widget which we pass the navigate function //will pop scope catches the back button presses return WillPopScope( onWillPop: () { return customPop(context); }, child: Scaffold( body: Column( children: <Widget>[ RaisedButton( child:Text('PRESS'), onPressed: (){ _currentIndex++; navigateTo(_currentIndex); }, ), Expanded( child: _fragments[_currentIndex], ), ], ), ), ); } void navigateTo(int index) { _backstack.add(index); setState(() { _currentIndex = index; }); _setTitle('$index'); } void navigateBack(int index) { setState(() { _currentIndex = index; }); _setTitle('$index'); } Future<bool> customPop(BuildContext context) { print("CustomPop is called"); print("_backstack = $_backstack"); if (_backstack.length > 1) { _backstack.removeAt(_backstack.length - 1); navigateBack(_backstack[_backstack.length - 1]); return Future.value(false); } else { return Future.value(true); } } //this method could be called by the navigate and navigate back methods _setTitle(String appBarTitle) { setState(() { title = appBarTitle; }); } } class Fragment2 extends StatefulWidget { @override State<Fragment2> createState() => _Fragment2(); } class _Fragment2 extends State<Fragment2> { @override Widget build(BuildContext context) { return Center( child: RaisedButton( child: Text("_Fragment2"), onPressed: (){ }), ); } } class Fragment1 extends StatefulWidget { @override State<Fragment1> createState() => _Fragment1(); } class _Fragment1 extends State<Fragment1> { @override Widget build(BuildContext context) { return Center( child: Text("_Fragment1"), ); } } class Fragment3 extends StatefulWidget { @override State<Fragment3> createState() => _Fragment3(); } class _Fragment3 extends State<Fragment3> { @override Widget build(BuildContext context) { return Center( child: Text("_Fragment3"), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.