Flutter introduction_screen组件如何手动切换页面,实现skip按钮返回上一页
实现自定义页面跳转的方法
你的判断是对的,IntroductionScreen内部单独维护了页面滚动状态,外部自行修改currentIndex不会同步到组件内部,需要用官方提供的控制器实现跳转操作,修改步骤如下:
- 在State类中声明
IntroductionScreenController实例
class _IntroPageState extends State<IntroPage> { int currentIndex = 0; // 新增引导页控制器 final introController = IntroductionScreenController(); // 其余原有代码保持不变 void _onIntroEnd(context) { getIt<IntroLocalDataSource>().setIntroSeenState(true); Navigator.of(context).pushReplacementNamed(SignInPage.id); } // ... }
- 给
IntroductionScreen组件绑定控制器
IntroductionScreen( // 新增controller参数绑定实例 controller: introController, onChange: (index) { setState(() { currentIndex = index; }); }, // 其余原有参数保持不变 rtl: true, globalBackgroundColor: Colors.transparent, showNextButton: false, // ... )
- 修改skip按钮的点击逻辑,调用控制器提供的跳转方法
skip: ElevatedButton( style: ElevatedButton.styleFrom( elevation: 0, primary: kLightTextColor, shape: CircleBorder(), ), onPressed: () { // 替换原有自行修改currentIndex的逻辑 introController.previous(); }, child: Icon( Icons.keyboard_arrow_left_rounded, color: Theme.of(context).primaryColor, ) ),
如果需要跳转到指定索引的页面,可以调用introController.animateTo(目标索引)方法,页面索引从0开始计算。你之前配置的onChange回调会在页面跳转后自动更新currentIndex的值,不需要手动修改,按钮显隐逻辑也可以正常生效。
内容的提问来源于stack exchange,提问作者Sajad Rezvani
相关产品推荐
相关产品推荐

