英文:
Why the Navigator.push() doesn't work on my ElevatedButton()?
问题
当我按下按钮时,为什么Navigator.push()
不起作用?什么都不发生:
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),
),
],
),
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),
//Button
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,
),
),
),
),
),
],
),
],
),
);
}
}
我想要导航到下面这个屏幕:
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());
}
}
我尝试使用Inkwell()
和onTap()
,但也不起作用,什么都不发生。我不知道出了什么问题,我按下按钮后仍然停留在homePage()
。
英文:
Can someone help me? Why the Navigator.push()
doesn't work on my ElevatedButton()
? When I press the button, nothing happens:
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
),
//Button
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
),
),
),
),
),
],
),
],
),
);
}
}
I want to make the screen navigation to this screen below:
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()
);
}
}
I try to use the Inkwell()
with onTap()
but doesn't work too, nothing happens. I don't know what's going on, I press the button and continue on to homePage()
.
答案1
得分: 1
我个人稍微不同地编写了 .push()
方法,它完美地工作,试试这个:
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext builder) => const Page()));
将 const Page()
更改为你需要的内容,应该没问题,如果有问题,请提供一些堆栈跟踪信息或更新你的问题并提供更多细节,愉快编码!
英文:
I personally write the .push()
method a little bit different and it works perfectly, try this:
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext builder) => const Page()));
Change const Page()
with what you need and you should be fine, if not, please provide some stack trace or update your question with more details, happy coding!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论