shri
shri

Reputation: 874

Button OnPressed is not working in Flutter

I have issue with the Raised button click. The method inside OnPressed() is not getting called. Ideally on the OnPressed() method i would like to have the pop up or a slider shown. I have created the example to show the problem currently faced.

The main.dart file calls 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().

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.
But the ButtonPressed() method is not getting called . I am not able to see the print msg('inside button press') in the console. Pl. let me what could be the reason for ButtonPressed method not getting called. Attached the snapshot for your reference.

Upvotes: 3

Views: 25638

Answers (2)

live-love
live-love

Reputation: 52366

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,
                ),
              ),
            )
          ],
        ),
      ),
    );

Upvotes: 0

Jay Mungara
Jay Mungara

Reputation: 7148

You are calling a Widget on your RaisedButton's onPressed method. 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.

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()));
      },

Upvotes: 3

Related Questions