How to add the widgets dynamically to column in Flutter?

后端 未结 4 1024
日久生厌
日久生厌 2021-02-08 10:55

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 li

相关标签:
4条回答
  • 2021-02-08 11:20

    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'));
    
    0 讨论(0)
  • 2021-02-08 11:22

    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(() {
    
    });
    
    0 讨论(0)
  • 2021-02-08 11:35

    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.

    0 讨论(0)
  • 2021-02-08 11:41
    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: () {
            }, 
            )  
        };
      }
    }
    
    0 讨论(0)
提交回复
热议问题