Flutter项目Dine In按钮点击无法跳转至DineIn页面求助
Flutter页面跳转问题解决:Dine In按钮无响应
你的问题根源很清晰:Dine In按钮的onPressed方法仅更新了选中状态,没有添加页面跳转的核心代码。对比Takeaway按钮的实现就能发现,它的onPressed里同时包含了状态更新和Navigator.push跳转逻辑,而Dine In按钮只做了前者。
修改后的Dine In按钮代码
将原来的Dine In按钮onPressed部分替换为:
onPressed: () { setState(() { _selectedButtonIndex = 0; // 更新选中状态 }); // 添加页面跳转代码 Navigator.push( context, MaterialPageRoute(builder: (context) => DineIn()), ); },
完整修改后的按钮片段
ElevatedButton( onPressed: () { setState(() { _selectedButtonIndex = 0; // Set the selected button index to 0 (Dine In) }); Navigator.push( context, MaterialPageRoute(builder: (context) => DineIn()), ); }, style: ElevatedButton.styleFrom( primary: _selectedButtonIndex == 0 ? Colors.lightGreen :Color(0xffb3ff66), // Highlight the button if selected elevation: 4.0, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), ), child: const Text( 'Dine In', style: TextStyle( color: Colors.black, fontWeight: FontWeight.w500, fontFamily: 'Montserrat', fontSize: 16.0, ), ), ),
修改后,点击Dine In按钮会先更新按钮选中状态,然后跳转到DineIn页面,和Takeaway按钮的逻辑保持一致。
内容的提问来源于stack exchange,提问作者Saloni Tyagi
相关产品推荐
相关产品推荐

