Ishwar Kumar
Ishwar Kumar

Reputation: 29

How to add a new widget on clicking a button

    class CustomFlatButton extends StatefulWidget {
  String ButtonName="";
  CustomFlatButton(String ButtonName){
    this.ButtonName=ButtonName;
  }
  @override
  _CustomFlatButtonState createState() => _CustomFlatButtonState(ButtonName);
}

class _CustomFlatButtonState extends State<CustomFlatButton> {
  String ButtonName="";
  _CustomFlatButtonState(String ButtonName){
    this.ButtonName=ButtonName;
  }
  @override
  Widget build(BuildContext context) {
    return FlatButton(
                                  color: Colors.blue,
                                  textColor: Colors.white,
                                  disabledColor: Colors.grey,
                                  disabledTextColor: Colors.black,
                                  padding: EdgeInsets.all(8.0),
                                  splashColor: Colors.blueAccent,
                                  onPressed: () {
                                    ListWheelScrollView(itemExtent: 31,useMagnifier: true,magnification: 1.5,children: <Widget>[
                                      Container(color: Colors.red,height: 200,width: 200)
                                    ],);
                                  },
                                  child: Text(
                                    ButtonName,
                                    style: TextStyle(fontSize: 20.0),
                                  ),
                                );
  }
}

How to render a widget when users click the Button. Here in the code

onPressed: () {
                                ListWheelScrollView(itemExtent: 31,useMagnifier: true,magnification: 1.5,children: <Widget>[
                                  Container(color: Colors.red,height: 200,width: 200)
                                ],);
                              }

I want to display a ListWheelScrollView Widget on pressing the button means when button is pressed then a ListWheelScrollView should be displayed.

I am new to flutter. Please let me know if you find any difficulty in understanding my question

Upvotes: 0

Views: 99

Answers (1)

Gabe
Gabe

Reputation: 6885

You can create a variable that says if the button is pressed, we will call this hasPressed and if that is true display the ListWheelScrollView, but if it is false display an empty Container(height: 0, width: 0). When the button is pressed setState() is called to rebuild the widget. This would look something like the following:

Row(
      children: <Widget>[
        FlatButton(
          color: Colors.blue,
          textColor: Colors.white,
          disabledColor: Colors.grey,
          disabledTextColor: Colors.black,
          padding: EdgeInsets.all(8.0),
          splashColor: Colors.blueAccent,
          onPressed: () {
            setState(() {
              isPressed = true;
            });
          },
          child: Text(
            ButtonName,
            style: TextStyle(fontSize: 20.0),
          ),
        ),
        (isPressed)
            ? ListWheelScrollView(
                itemExtent: 31,
                useMagnifier: true,
                magnification: 1.5,
                children: <Widget>[
                  Container(color: Colors.red, height: 200, width: 200)
                ],
              )
            : Container(
                height: 0,
                width: 0,
              )
      ],
    );

You would declare bool isPressed = false where you declare ButtonName. Hope this helps and if so please up vote, otherwise leave a comment.

Upvotes: 1

Related Questions