Flutter中ElevatedButton的Navigator.push()无法跳转求助
问题描述
ElevatedButton中的Navigator.push()点击后无任何跳转反应,尝试用InkWell的onTap()实现跳转也无效,点击按钮后始终停留在首页。
相关代码
HomeCard组件代码
import 'package:flutter/material.dart'; import 'package:metroguia/constants/colors.dart'; import '../../selecao__linha/selecao_linhas.dart'; class HomeCard extends StatelessWidget { const HomeCard({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Container( width: 281, height: 111, decoration: BoxDecoration( color: orangeDetails.withOpacity(0.8), borderRadius: BorderRadius.circular(9), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.1), spreadRadius: 3, blurRadius: 4, offset: const Offset(0, 2) // muda a posição da sombra ), ], ), child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Image.asset( "assets/images/metro.png", width: 100, ), Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ const Align( alignment: Alignment.center, child: Text( "Acompanhe a rotas das linhas \n e estações do metrô \n de Recife", style: TextStyle( color: Colors.white, fontSize: 12, fontWeight: FontWeight.w700, ), ), ), Padding( padding: const EdgeInsets.only( left: 65 ), child: SizedBox( height: 18, child: ElevatedButton( onPressed: (){ Navigator.push( context, MaterialPageRoute(builder: ((context) => const selecaoLinha()) ) ); }, style: ButtonStyle( elevation: MaterialStateProperty.all(0), foregroundColor: MaterialStateProperty.all(Colors.black), backgroundColor:MaterialStateProperty.all(blueDetails), shape: MaterialStateProperty.all<RoundedRectangleBorder>( RoundedRectangleBorder( borderRadius: BorderRadius.circular(3), ) ) ), child: const Text( "VER LINHAS", style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600 ), ), ), ), ), ], ), ], ), ); } }
目标页面selecaoLinha代码
import 'package:flutter/material.dart'; import 'package:metroguia/pages/linha__selecionada/linha__centro_jaboatao/linha__jaboatao.dart'; class selecaoLinha extends StatefulWidget { const selecaoLinha({Key? key}) : super(key: key); @override State<selecaoLinha> createState() => _selecaoLinhaState(); } class _selecaoLinhaState extends State<selecaoLinha> { @override Widget build(BuildContext context) { return Scaffold( body: Jaboatao() ); } }
排查与解决办法
强制使用根导航器:如果当前
context没有关联到Navigator,可以使用Navigator.of(context, rootNavigator: true)获取根导航器,修改按钮的onPressed回调:onPressed: (){ Navigator.of(context, rootNavigator: true).push( MaterialPageRoute(builder: ((context) => const selecaoLinha())) ); },修正类命名规范:Dart中类名需以大写字母开头,将
selecaoLinha改为SelecaoLinha,避免潜在语法或逻辑问题:
修正后的目标页面代码:class SelecaoLinha extends StatefulWidget { const SelecaoLinha({Key? key}) : super(key: key); @override State<SelecaoLinha> createState() => _SelecaoLinhaState(); } class _SelecaoLinhaState extends State<SelecaoLinha> { @override Widget build(BuildContext context) { return Scaffold( body: Jaboatao() ); } }同步修改跳转代码:
MaterialPageRoute(builder: ((context) => const SelecaoLinha()))排查目标页面的子组件:临时将
Jaboatao()替换为简单测试组件,确认是否是该组件导致页面无法加载:return Scaffold( body: const Center(child: Text("测试跳转成功")), );如果测试页面能正常显示,说明问题出在
Jaboatao组件内部,需要进一步排查该组件的代码逻辑。
内容的提问来源于stack exchange,提问作者Flavio Henrique
相关产品推荐
相关产品推荐

