Flutter GestureDetector点击无响应,无法跳转至Home页面
解决GestureDetector点击无响应及页面跳转失败问题
问题根源
你自定义的navigator getter直接返回null,点击时调用null.push()自然不会触发任何导航操作,这就是点击无响应的核心原因。
修正步骤
- 删除错误的
get navigator => null;代码行 - 将
onTap回调中的navigator.push替换为Navigator.of(context).push,通过context获取正确的导航器实例
修正后的完整代码
import 'package:flutter/material.dart'; import 'home.dart'; class IntroPage extends StatelessWidget { const IntroPage({super.key}); @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.blue, body: Center( child: Padding( padding: const EdgeInsets.all(25), child: Column( children: [ // 间距 const SizedBox(height: 180), // 标题 const Text('Trade App', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 32, color: Colors.white, )), // 副标题 const Text('Verkaufen und Kaufen', textAlign: TextAlign.center, style: TextStyle( fontSize: 18, color: Color.fromARGB(255, 255, 255, 255), )), // 间距 const SizedBox(height: 50), // 启动按钮 GestureDetector( onTap: () => Navigator.of(context).push( MaterialPageRoute( builder: (context) => const Home(), ), ), child: Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(30), ), padding: const EdgeInsets.all(20), child: const Text('Loslegen'), ), ), ], ), ), ), ); } }
补充说明
Navigator.of(context)会从当前context中获取关联的导航器实例,这是Flutter中实现页面跳转的标准方式- 如果你觉得
Navigator.of(context)写起来繁琐,也可以使用context.push(...)(需确保已导入package:flutter/material.dart),效果完全一致
内容的提问来源于stack exchange,提问作者Fabian Weger
相关产品推荐
相关产品推荐

