0
votes

Remettez un nouveau widget sur le bouton Cliquez sur la même page, dans une colonne de Flutter

Je veux retourner un nouveau widget sur le bouton Cliquez sur la même page, dans une colonne. J'essaie donc de retourner un conteneur sur la même page que l'un des enfants dans une colonne, lorsqu'un bouton est cliqué sur un bouton.

Ceci est mon code d'exemple: P>

Column(
      children: <Widget>[
       // here I want to return my container in the show function

        Text(
          "This is a sample text",
          style: TextStyle(fontSize: 40),

        ),
        RaisedButton(
          child: Text("click"),
          onPressed: () {
            debugPrint("clicked");
            show();
          },
        )
      ],
    );



show() {
    return SampleContainer();
  }



class SampleContainer extends StatefulWidget {
  @override
  _SampleContainerState createState() => _SampleContainerState();
}

class _SampleContainerState extends State<SampleContainer> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("hi"),


);
  }
}


0 commentaires

3 Réponses :


0
votes

Créer une variable xxx

puis un widget de colonne comme xxx


0 commentaires

0
votes

Vous pouvez utiliser le Classe de visibilité

Voici un Exemple complet Comment vous pouvez le faire. P>

class VisibiltyExampleContainer extends StatefulWidget {
  const VisibiltyExampleContainer({
    Key key,
  }) : super(key: key);

  @override
  _VisibiltyExampleContainerState createState() =>
      _VisibiltyExampleContainerState();
}

class _VisibiltyExampleContainerState extends State<VisibiltyExampleContainer> {
  var _showContainer;

  @override
  void initState() {
    _showContainer = false;
    super.initState();
  }

  void show() {
    setState(() {
      _showContainer = !_showContainer;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Container(
        child: Column(
          children: <Widget>[
            Visibility(
              child: SampleContainer(),
              visible: _showContainer,
            ),
            Text(
              "This is a sample text",
              style: TextStyle(fontSize: 40),
            ),
            RaisedButton(
              child: Text("click"),
              onPressed: () {
                debugPrint("clicked");
                show();
              },
            )
          ],
        ),
      )),
    );
  }
}


0 commentaires

0
votes

Autre alternative à Réponse de dev . Vous pouvez utiliser cette approche pour ajouter un widget à chaque fois que vous cliquez sur le bouton:

class Demo extends StatefulWidget {
  @override
  _DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {
  List<Widget> widgets = List<Widget>();

  @override
  void initState(){
    widgets = <Widget>[
      Text(
        "This is a sample text",
        style: TextStyle(fontSize: 40),
      ),
      RaisedButton(
        child: Text("click"),
        onPressed: () {
          debugPrint("clicked");
          debugPrint('widgets: $widgets');
          widgets.insert(0, SampleContainer());
          setState(() {});
        },
      )
    ];
  }
  @override
  Widget build(BuildContext context) {

    return Center(
      child: Column(
        children: widgets,
      ),
    );
  }
}

show() {
  return SampleContainer();
}

class SampleContainer extends StatefulWidget {
  @override
  _SampleContainerState createState() => _SampleContainerState();
}

class _SampleContainerState extends State<SampleContainer> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child: Text(
        "This is an additional Widget!!",
        style: TextStyle(fontSize: 40),
      ),
    );
  }
}


0 commentaires