29

I have added a few widgets inside the ListView. So that I can scroll all widgets. Now I required to add one more widget to the ListView to load the list of comments. I can not add the ListView inside the ListView. And moreover, I do not require the separate scroll for my comments. It should be scroll along with the widgets inside the ListView. So I planned to add the Column instead of ListView. Could any help to add my comments dynamically in the Columns?

new Expanded(
          child:
          new ListView(
            shrinkWrap: true,
            children: <Widget>[

              // Title

              new Padding(padding: const EdgeInsets.only(
                  top: 10.00, left: 10.00),
                child: new Text(
                  _feed.title, textAlign: TextAlign.start,),
              ),

              // content

              new Container(
                child: new Text(
                  _feed.content, textAlign: TextAlign.start,),
              ),

              // Comments List will go here

            ],
          ),
        ),

7 Answers 7

40

If you have the comments data already, simply create a List, then pass it to the children property of the Column. Something like:

var commentWidgets = List<Widget>();
for (var comment in comments) {
  commentWidgets.Add(Text(comment.text)); // TODO: Whatever layout you need for each widget.
}
…

new Expanded(
      child:
      new ListView(
        shrinkWrap: true,
        children: <Widget>[

          // Title

          new Padding(padding: const EdgeInsets.only(
              top: 10.00, left: 10.00),
            child: new Text(
              _feed.title, textAlign: TextAlign.start,),
          ),

          // content

          new Container(
            child: new Text(
              _feed.content, textAlign: TextAlign.start,),
          ),

          // Comments List will go here
          Column(children: commentWidgets,),
        ],
      ),
    ),

If you don't have the comments data already and need to fetch it, use a FutureBuilder to build the UI once the future completes.

2
  • Thanks for the link to the FutureBuilder ! It saved my day.
    – Jérémy
    Aug 3, 2019 at 18:24
  • Hi, I would also like to know how I can dynamically add widget and remove that same widget e.g if say I add a widget how do I target this same widget to remove it?
    – samezedi
    Feb 10, 2020 at 22:07
15

Another way:

return Column(
      children: [
        for(String item in list) Text(item);

    ]);

You can also mix static and dynamic fields eaisly in this case:

return Column(
          children: [
            for(String item in list) Text(item),
          Text("Static text control"),
   
        ]);
6

By maintaining a reference to the Column object, the .children field/property can be referenced after the Column object has been declared - like so:

Column someColumn = Column(
  children: [],
);

someColumn.children.add(Text('Hello 123'));
6

Currently(2021-06), Flutter 2.x implemented null-safe. The answer from @Gene Bo should work but needs little modification.

This should work.

var pwdWidgets = <Widget>[];

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  crossAxisAlignment: CrossAxisAlignment.center,
  children: this.pwdWidgets,
),

ElevatedButton(
  onPressed: (){
    setState((){
      this.pwdWidgets.add(Text("Hello World"),);
    });
  },
  child: Text("click me"),
),
1

In Addition to @Derek Lakin's Answer which worked for me too, it is also required to call setState(), if you need to update the comments and reload.

var commentWidgets = List<Widget>();
for (var comment in comments) {
      commentWidgets.Add(Text(comment.text)); // TODO: Whatever layout you need foreach widget.
}
setState(() {

});
1

Yes you can add dynamic child on column widgets.

 class CategoriesItemWidget extends StatelessWidget {
  final List<String> list;

  const CategoriesItemWidget({Key? key, required this.list})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: list
          .map((string) => Text(string))
          .toList(),
    );
  }
}
0
import 'package:flutter/material.dart';

class LoginRegisterPage extends StatefulWidget {
  @override
  _LoginRegisterPageState createState() => _LoginRegisterPageState();
}

class _LoginRegisterPageState extends State<LoginRegisterPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("App"),
      ),
      body: new Container(
        margin: EdgeInsets.all(15.0),
        child: new Form(
          child: new Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: createInputs() + createButtons(),
          ),
          ),
      ),
    );
  } 

  List<Widget> createInputs(){
    return{
      SizedBox(height: 10.0),
      logo(),
      SizedBox(height: 20.0),
      new TextFormField(
        decoration: new InputDecoration(labelText: 'Email'),
      ),

      SizedBox(height: 10.0),
      new TextFormField(
        decoration: new InputDecoration(labelText: 'Passwors')
      ),
      SizedBox(height: 20.0), 
    };
  }

  Widget logo(){
      return new Hero(        
        tag:'hero',
        child: new CircleAvatar(
          backgroundColor:Colors.transparent,
          radius: 110.0,
          child: Image.asset("images\logo.PNG"),          
        ),         
        );
    }

    List<Widget> createButtons(){
    return{
      new RaisedButton(
        child: new Text("Login", style: new TextStyle(fontSize: 20.0),),
        textColor: Colors.pink,         
        onPressed: () {          
        },
        ),  

        new FlatButton(
        child: new Text("Already not have an account?", style: new TextStyle(fontSize: 14.0),),
        textColor: Colors.white,         
        onPressed: () {
        }, 
        )  
    };
  }
}
1
  • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
    – David Buck
    Mar 23, 2020 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.