Yashwant Kumar
Yashwant Kumar

Reputation: 143

how do we execute two function in single button pressed in flutter

RaisedButton(
               color: Colors.blueAccent, 
               onPressed: () =>sendData(); //fun1
               signupPage(context) //fun2
               child: 
                Text("Signup"),
             )

this code gives an error..Expected to find ')'

enter image description here

Upvotes: 11

Views: 26250

Answers (4)

VinayRaj010
VinayRaj010

Reputation: 9

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'My Fast Claim',
      home: First(),
    ),
  );
}

class First extends StatefulWidget {
  @override
  _FirstState createState() => _FirstState();
}

class _FirstState extends State<First> {
  int a = 0;
  int b = 0;

  void add() {
    setState(() {
      a++;
    });
  }

  void minus() {
    setState(() {
      b++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(title: new Text("Number")),
      body: new Container(
        child: new Center(
          child: new Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              FloatingActionButton(
                onPressed: () {
                  add();
                  minus();
                },
                child: new Icon(
                  Icons.add,
                  color: Colors.black,
                ),
                backgroundColor: Colors.white,
              ),
              new Text('$a', style: new TextStyle(fontSize: 60.0)),
              new Text('$b', style: new TextStyle(fontSize: 60.0)),
            ],
          ),
        ),
      ),
    );
  }
}

Upvotes: -1

Harshit Bagga
Harshit Bagga

Reputation: 121

onPressed:()=>[rollDice(),rollDice2()],

I used this approach to call multiple functions on one click.

Upvotes: 12

Richardd
Richardd

Reputation: 1012

I using this to do similar thing as you are asking

 Widget _btn(String txt, VoidCallback onPressed) {
    return ButtonTheme(
        minWidth: 48.0,
        child: RaisedButton(child: Text(txt), onPressed: onPressed));
  }



  Widget localAsset() {
    return _tab([
      Text('Some text:'),
      _btn('2 func btn', () => [sendData(), signupPage(context)  ], ),
    ]);
  }

and on the widget build

body: TabBarView(
  children: [localAsset()],
),

Upvotes: 3

anmol.majhail
anmol.majhail

Reputation: 51186

Arrow Function can run single statement function.

Options:

1 - You can run two Functions as Below.

RaisedButton(
               color: Colors.blueAccent, 
               onPressed: () {
               sendData(); //fun1
               signupPage(context); //fun2
               },
               child: 
                Text("Signup"),
             )

Or

2 - You can run fun2 in fun 1.

RaisedButton(
               color: Colors.blueAccent, 
               onPressed: () => sendData(context), //fun1
               child: 
                Text("Signup"),
             )

void sendData(BuildContext context){
//sendData Code ...
signupPage(context); //fun2
...
}

Upvotes: 19

Related Questions