Search code examples
flutterflutter-animationflutter-widgetflutter-onpressed

How to call a ontap fuction in another widget indirectly in flutter?


I'm using animated container for getting toggle button like in android. Here my code is working for toggle button function perfectly. But what i need is, if i press some other button, toggle button should function. see the below image for your reference(if i press "Press" button it should operate toogle button). Thanks in advance.

https://i.sstatic.net/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;
    });
  }
}

Solution

  • Replace your button code with the below code block. Actually you are calling toggle button inside a method, So, it is unable to recognition it.

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