Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - onPressed function

Tags:

flutter

I need to insert onpressed()function (navigator push to other page) on the icons bellow:

Column buildButtonColumn(IconData icon, String label) {
    Color color = Theme.of(context).primaryColor;
      return Column(
      mainAxisSize: MainAxisSize.min,
       mainAxisAlignment: MainAxisAlignment.center,
       children: [
      Icon(icon, color: color),
      Container(
        margin: const EdgeInsets.only(top: 8.0),
        child: Text(
          label,
          style: TextStyle(
            fontSize: 12.0,
            fontWeight: FontWeight.w400,
            color: color,
             ),
           ),
         ),
       ],
     );
   }

   Widget buttonSection = Container(
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        buildButtonColumn(Icons.call, 'CALL'),
        buildButtonColumn(Icons.near_me, 'ROUTE'),
        buildButtonColumn(Icons.share, 'SHARE'),
      ],
    ),
  );
like image 299
Sergio Guerjik Avatar asked Oct 26 '18 00:10

Sergio Guerjik


People also ask

How to execute a function when Button is pressed in flutter?

To execute a function when button is pressed, use onPressed () property of the button. Following is a sample code snippet to execute a function when RaisedButton is pressed. In this example Flutter Application, we shall take a RaisedButton and execute a function when the button is pressed.

What is a route in flutter?

Terminology: In Flutter, screens and pages are called routes . The remainder of this recipe refers to routes. In Android, a route is equivalent to an Activity.

What are screens and pages in flutter?

Most apps contain several screens for displaying different types of information. For example, an app might have a screen that displays products. When the user taps the image of a product, a new screen displays details about the product. Terminology: In Flutter, screens and pages are called routes .

How to execute a function when user pressed a button?

In this tutorial, we will learn how to execute a function when user pressed a button. To execute a function when button is pressed, use onPressed () property of the button. Following is a sample code snippet to execute a function when RaisedButton is pressed.


2 Answers

You can make use of an IconButton widget for this.

IconButton(
     icon:  Icon(Icons.info),
     onPressed: (){
           // Do something               
     },
 )

of you can make use of GestureDetector

GestureDetector(
  onTap: () {
    //Do something
  },
  child: icon: Icon(Icons.info),
)

EDIT:

With a little modification to the codes pasted below. Change buildButtonColumn tho this:

buildButtonColumn(icon, label, onTap) {
      Color color = Theme.of(context).primaryColor;
      return GestureDetector(
        onTap: onTap,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(icon, color: color),
            Container(
              margin: const EdgeInsets.only(top: 8.0),
              child: Text(
                label,
                style: TextStyle(
                  fontSize: 12.0,
                  fontWeight: FontWeight.w400,
                  color: color,
                ),
              ),
            ),
          ],
        ),
      );
    }

and For button Section, use this.

Widget buttonSection = Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          buildButtonColumn(Icons.call, 'CALL', (){
                print('call');
          }),
          buildButtonColumn(Icons.near_me, 'ROUTE', (){
            print('route');
          }),
          buildButtonColumn(Icons.share, 'SHARE', (){
            print('share');
          }),
        ],
      ),
    );
like image 92
nonybrighto Avatar answered Sep 21 '22 01:09

nonybrighto


You can use this:

GestureDetector buildButtonColumn(IconData icon, String label){
    onTap(){
      print("Pressed");
    }
    Color color = Theme.of(context).primaryColor;
    return GestureDetector(
      onTap: onTap,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(icon, color: color),
          Container(
            margin: const EdgeInsets.only(top: 8.0),
            child: Text(
              label,
              style: TextStyle(
                fontSize: 12.0,
                fontWeight: FontWeight.w400,
                color: color,
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
    home: Scaffold(
      body: Center(
        child:Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            buildButtonColumn(Icons.call, 'CALL'),
            buildButtonColumn(Icons.near_me, 'ROUTE'),
            buildButtonColumn(Icons.share, 'SHARE'),
          ],
        ),
      ),
    ),
  );
}
like image 22
yashthakkar1173 Avatar answered Sep 18 '22 01:09

yashthakkar1173