0

I wanna define a function or something like that to make a new widget on my button press, Also, I wanna create that widget in a specific column. Anybody can help?

M.Taha Basiri
  • 612
  • 1
  • 6
  • 14

2 Answers2

1

you can define bool variable to control show or hide that widget and in button onPressed method change that variable in setState.

something like this code:

var _showWidget = false;

  void changeShow(){
    setState(() {
      _showWidget = !_showWidget;
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          RaisedButton(
            child: _showWidget? Text('Show'): Text('Hide'),
            onPressed: changeShow
          ),
          if (_showWidget)
            Container(
              width: 150,
              height: 50,
              color: Colors.green,
              child: Center(child: Text('Hello Word')),
            ),
        ],
      ),
    );
  }
Wehid Behrami
  • 226
  • 2
  • 6
0

well you can basically use invisible and visible field in your widget i.e. the widget is already made and u can assign a function later to it ..ex when the button is pressed the widget becomes visible and you pass a function or value to it.

austin
  • 517
  • 5
  • 16