【问题标题】:how do we execute two function in single button pressed in flutter我们如何在颤动中按下单个按钮执行两个功能
【发布时间】:2020-06-12 06:25:59
【问题描述】:
RaisedButton(
               color: Colors.blueAccent, 
               onPressed: () =>sendData(); //fun1
               signupPage(context) //fun2
               child: 
                Text("Signup"),
             )

此代码出错..应找到')'

enter image description here

【问题讨论】:

  • sendData()函数内调用SignupPage(context)函数?
  • 这两个函数有不同的工作,所以不能互相调用! @Sushil Kumar。
  • 所以运行就像 - onPressed: () { sendData(); signupPage(context); }, - 如答案中所述分开。
  • 已经试过了,它只解决了额外的错误! @Anmol.majhail
  • 有什么错误。? - 确保在 } 的末尾也加上逗号。

标签: button flutter dart


【解决方案1】:

Arrow Function可以运行单语句功能。

选项:

1 - 您可以运行以下两个函数。

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

或者

2 - 你可以在 fun 1 中运行 fun2。

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

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

【讨论】:

    【解决方案2】:
    onPressed:()=>[rollDice(),rollDice2()],
    

    我用这种方式一键调用多个函数。

    【讨论】:

      【解决方案3】:

      我用它来做类似你问的事情

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

      在小部件构建上

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

      【讨论】:

        【解决方案4】:
        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)),
                    ],
                  ),
                ),
              ),
            );
          }
        }
        

        【讨论】:

        • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-30
        • 2021-11-17
        • 2021-03-06
        • 2013-02-04
        • 2020-07-07
        • 1970-01-01
        • 2023-02-02
        相关资源
        最近更新 更多