简体   繁体   English

按钮 OnPressed 在 Flutter 中不起作用

[英]Button OnPressed is not working in Flutter

I have issue with the Raised button click.我对凸起按钮的点击有问题。 The method inside OnPressed() is not getting called. OnPressed() 中的方法没有被调用。 Ideally on the OnPressed() method i would like to have the pop up or a slider shown.理想情况下,在 OnPressed() 方法上,我希望显示弹出窗口或滑块。 I have created the example to show the problem currently faced.我创建了示例来显示当前面临的问题。

The main.dart file calls Screen2() main.dart 文件调用 Screen2()

import 'package:flutter/material.dart';
//import 'package:flutter_app/main1.dart';
import 'screen2.dart';

void main() => runApp(Lesson1());

class Lesson1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
  home: Screen2(),
);
}
}

and in Screen2()i have just have a RaisedButton and OnPressed() it needs to call the function ButtonPressed().在 Screen2() 中,我只有一个 RaisedButton 和 OnPressed(),它需要调用函数 ButtonPressed()。

import 'package:flutter/material.dart';
class Screen2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    backgroundColor: Colors.blue,
    title: Text('Screen 2'),
  ),
  body: Center(
    child: RaisedButton(
      color: Colors.blue,
      child: Text('Go Back To Screen 1'),
      onPressed: () {
        print('centrebutton');
        ButtonPressed();
      },
    ),
  ),
  );
 }
 }
class ButtonPressed extends StatefulWidget {
@override
_ButtonPressedState createState() => _ButtonPressedState();

}

class _ButtonPressedState extends State<ButtonPressed> {
@override
Widget build(BuildContext context) {
print ('inside button press');

return Scaffold(
  appBar: AppBar(
    backgroundColor: Colors.red,
    title: Text('Screen 3'),
  ),
  // create a popup to show some msg. 
 );
 }
 }

On clicking the Raised button the print statement ('centerbutton') gets printed.单击 Raised 按钮后,打印语句 ('centerbutton') 将被打印出来。
But the ButtonPressed() method is not getting called .但是 ButtonPressed() 方法没有被调用。 I am not able to see the print msg('inside button press') in the console.我无法在控制台中看到打印 msg('inside button press')。 Pl. Pl。 let me what could be the reason for ButtonPressed method not getting called.让我看看 ButtonPressed 方法没有被调用的原因。 Attached the snapshot for your reference.附上截图供大家参考。

You are calling a Widget on your RaisedButton's onPressed method.您正在 RaisedButton 的 onPressed 方法上调用一个小部件。 Your widget is getting called but will not render anywhere in the screen.您的小部件正在被调用,但不会在屏幕的任何地方呈现。

You should call a function for processing your data in a tap event.您应该调用一个函数来处理点击事件中的数据。 But you are calling a widget or say a UI view.但是您正在调用一个小部件或说一个 UI 视图。

If you want to navigate to the respective screen then you should use navigator.如果要导航到相应的屏幕,则应使用导航器。

For ex :例如:

onPressed: () {
        print('centrebutton');
        Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => ButtonPressed()));
      },

This could be a cause: If you have an asset as a child of your button, and that asset does not exist, the button onpressed will not work.这可能是一个原因:如果您有一个资产作为按钮的子项,并且该资产不存在,则按下的按钮将不起作用。

Solution: remove the asset.解决方案:删除资产。

Example:例子:

return RaisedButton(
      splashColor: Colors.grey,
      onPressed: () {
       
      },
      child: Padding(
        padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
                Image(image: AssetImage("assets/google_logo.png"), height: 35.0), //remove this line
            Padding(
              padding: const EdgeInsets.only(left: 10),
              child: Text(
                'Sign in with Google',
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.grey,
                ),
              ),
            )
          ],
        ),
      ),
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM