Navigator.push()在我的ElevatedButton()上为什么不起作用?

huangapple go评论53阅读模式
英文:

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 &#39;package:flutter/material.dart&#39;;
import &#39;package:metroguia/constants/colors.dart&#39;;
import &#39;../../selecao__linha/selecao_linhas.dart&#39;;
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&#231;&#227;o da sombra
),
],
),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Image.asset(
&quot;assets/images/metro.png&quot;,
width: 100,
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Align(
alignment: Alignment.center,
child: Text(
&quot;Acompanhe a rotas das linhas \n e esta&#231;&#245;es do metr&#244; \n de Recife&quot;,
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) =&gt; const selecaoLinha())
)
);
},
style: ButtonStyle(
elevation: MaterialStateProperty.all(0),
foregroundColor: MaterialStateProperty.all(Colors.black),
backgroundColor:MaterialStateProperty.all(blueDetails),
shape: MaterialStateProperty.all&lt;RoundedRectangleBorder&gt;(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(3),
)
)
),
child: const Text(
&quot;VER LINHAS&quot;,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600
),
),
),
),
),
],
),
],
),
);
}
}

I want to make the screen navigation to this screen below:

import &#39;package:flutter/material.dart&#39;;
import &#39;package:metroguia/pages/linha__selecionada/linha__centro_jaboatao/linha__jaboatao.dart&#39;;
class selecaoLinha extends StatefulWidget {
const selecaoLinha({Key? key}) : super(key: key);
@override
State&lt;selecaoLinha&gt; createState() =&gt; _selecaoLinhaState();
}
class _selecaoLinhaState extends State&lt;selecaoLinha&gt; {
@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) =&gt; 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!

huangapple
  • 本文由 发表于 2023年6月5日 00:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定