【问题标题】:How to call a ontap fuction in another widget indirectly in flutter?如何在颤动中间接调用另一个小部件中的 ontap 函数?
【发布时间】:2020-10-15 13:50:52
【问题描述】:

我正在使用动画容器来获取类似 android 中的切换按钮。在这里,我的代码完美地用于切换按钮功能。但我需要的是,如果我按下其他按钮,切换按钮应该起作用。请参阅下图供您参考(如果我按“按”按钮,它应该操作工具按钮)。提前致谢。

https://i.stack.imgur.com/qJpWT.jpg

class DialogExample extends StatefulWidget {

  @override
  _DialogExampleState createState() => new _DialogExampleState();
}

class _DialogExampleState extends State<DialogExample> {

  bool toggleValue = false;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top:180.0),
            child: Center(
              child: InkWell(
                onTap: toggleButton,
                child: AnimatedContainer(
                  duration: Duration(milliseconds: 100),
                  height: 40.0,
                  width: 100.0,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20.0),
                    color: toggleValue ? Colors.deepOrangeAccent : Colors.grey.withOpacity(0.5),
                  ),
                  child: Stack(
                    children: [
                      AnimatedPositioned(
                        duration: Duration(milliseconds: 300),
                        curve: Curves.easeIn,
                        left: toggleValue? 60.0 : 0.0,
                        right: toggleValue ? 0.0 : 60.0,
                        child: InkWell(
                          onTap: toggleButton,
                          child: AnimatedSwitcher(
                            duration: Duration(milliseconds: 100),
                            child: toggleValue ? Icon(Icons.check_circle,color: Colors.white,size: 39.0, key: UniqueKey(),)
                                : Icon(Icons.check_circle,color: Colors.white,size: 39.0,
                              key: UniqueKey(),),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top:80.0),
            child: RaisedButton(
              child: Text("press"),
              onPressed: (){
                toggleButton;
              },
            ),
          )
        ],
      ),
    );
  }

  toggleButton() {
    setState(() {
      toggleValue = !toggleValue;
    });
  }
}

【问题讨论】:

    标签: flutter flutter-animation flutter-widget flutter-onpressed


    【解决方案1】:

    将您的按钮代码替换为以下代码块。其实你是在一个方法里面调用toggle button,所以它是无法识别的。

    Padding(
                padding: const EdgeInsets.only(top:80.0),
                child: RaisedButton(
                  child: Text("press"),
                  onPressed:toggleButton,
                ),
              )
    

    【讨论】:

      【解决方案2】:

      只需将 toggleButton 替换为,因为这里的语句无效。

      toggleButton();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-08
        • 2020-08-20
        • 2020-09-18
        • 2020-11-15
        • 2020-07-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多