Hello i am new to Flutter and I want to know if there is a way to add new Widgets with a button click. I looked into many stack overflow similar Questions. but due to my poor knowledge most of them seems complex to me and hard to grasp. All i need to do is add some containers below old build containers

class MedPreC extends StatefulWidget {
  @override
  _MedPreCState createState() => _MedPreCState();
}
Widget returnWidget() {
  return Padding(
    padding: const EdgeInsets.all(8.0),
    child: Container(
      width: double.infinity,
      height: 40,
      color: Colors.red,
    ),
  );
}
class _MedPreCState extends State<MedPreC> {
  var child2 = Column(
    children: [
      returnWidget(),
    ],
  );
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Expanded(
            child: Container(
              color: Colors.yellow,
              height: 400,
              width: double.infinity,
              child: child2,
            ),
          ),
          RaisedButton(
            child: Text("Add"),
            onPressed: () {
              setState(() {
                //
                child2.children.add(returnWidget());
                //
              });
            },
          )
        ],
      ),
    );
  }
}

This is the code i have made so far. This whole code will be called inside another class with scafold and stuffs returnWidget() Returns a red container child2 is a Column called inside a yellow Container with one red container as one of its children i need to add more redcontainers on button press Thank you thats all


Solution 1: Raine Dale Holgado

Try this

class AddWidget extends StatefulWidget {
  @override
  _AddWidgetState createState() => _AddWidgetState();
}

class _AddWidgetState extends State<AddWidget> {
  List<Widget> containerList = [];

  Widget returnWidget() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Container(
        width: double.infinity,
        height: 40,
        color: Colors.red,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Expanded(
            child: Container(
              color: Colors.yellow,
              height: 400,
              width: double.infinity,
              child: Column(children: containerList),
            ),
          ),
          RaisedButton(
            child: Text("Add"),
            onPressed: () {
              setState(() {
                containerList.add(returnWidget());
              });
            },
          )
        ],
      ),
    );
  }
}


Solution 2: Salim Murshed

For this you need also check the overflow of widget. So you can check the below code.

import 'package:flutter/material.dart';

class MedPreC extends StatefulWidget {
  @override
  _MedPreCState createState() => _MedPreCState ();
}

class _MedPreCState extends State<MedPreC > {
  List<Widget> data = [];

  Widget CustomWidget() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Container(
        width: double.infinity,
        height: 40,
        color: Colors.red,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Container(
            color: Colors.yellow,
            height: MediaQuery.of(context).size.height - 60,
            width: double.infinity,
            child: SingleChildScrollView(child: Column(children: data)),
          ),
        ],
      ),
      bottomNavigationBar: Container(
        height: 60,
        width: MediaQuery.of(context).size.width,
        color: Colors.white,
        child: InkWell(
          child: Center(
            child: Container(
                color: Colors.red,
                width: 100,
                height: 40,
                child: Center(child: Text("Add"))),
          ),
          onTap: () {
            setState(() {
              data.add(CustomWidget());
            });
          },
        ),
      ),
    );
  }
}