首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在flutter中间接调用另一个widget中的ontap函数?

如何在flutter中间接调用另一个widget中的ontap函数?
EN

Stack Overflow用户
提问于 2020-06-25 15:05:43
回答 2查看 700关注 0票数 0

我使用动画容器来获得切换按钮,就像在android中一样。在这里,我的代码可以完美地实现切换按钮功能。但我需要的是,如果我按下其他按钮,切换按钮应该起作用。请看下面的图片供你参考(如果我按下“按下”按钮,它应该操作到谷歌按钮)。提前谢谢。

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

代码语言:javascript
运行
复制
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;
    });
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-25 15:15:06

用下面的代码块替换您的按钮代码。实际上你是在一个方法中调用切换按钮,所以它无法识别它。

代码语言:javascript
运行
复制
Padding(
            padding: const EdgeInsets.only(top:80.0),
            child: RaisedButton(
              child: Text("press"),
              onPressed:toggleButton,
            ),
          )
票数 0
EN

Stack Overflow用户

发布于 2020-06-25 15:14:57

只需将toggleButton替换为,因为这在这里不是有效语句。

代码语言:javascript
运行
复制
toggleButton();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62569695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档