title: How to add a widget when a button is pressed flutter
body: <p>For my flutter app I need a container that can be added when I press the add button. So I then looked at other Stack Overflow questions such as: <a href="https://stackoverflow.com/questions/43665784/flutter-render-new-widgets-on-clicktried">Flutter - Render new Widgets on click</a> and  <a href="https://stackoverflow.com/questions/50777895/flutter-add-new-widget-on-click">Flutter - Add new Widget on click</a>. After, this is what I came up with. </p>
  
  <pre><code>class Body extends StatelessWidget {
  
    @override
    Widget build(BuildContext context) {
  
      var tPadding= MediaQuery.of(context).size.width  * 0.08; 
      var bPadding= MediaQuery.of(context).size.width  * 0.15; 
      var vPadding = MediaQuery.of(context).size.height  * 0.015;
  
      return Expanded (
        child: Container (
          child: NotificationListener&lt;OverscrollIndicatorNotification&gt; (
            onNotification: (OverscrollIndicatorNotification overscroll) {
              overscroll.disallowGlow();
            },
            child: PageView.builder(
              pageSnapping: false,
              controller: PageController(viewportFraction: 0.85),
              itemCount: container.length,
              itemBuilder: (context, i) {
                return Padding (
                  padding: EdgeInsets.only(
                    left: vPadding,
                    right: vPadding,
                    top: tPadding,
                    bottom: bPadding
                  ),
                  child: container[i]
                );
              },
            )
          )
        )
      );
    }
  
  }
  
   int _count = 1;
  
  List container = [
    List.generate(_count, (int i) =&gt; ContainerCard),
    AddContainer()
  ];
  
  class ContainerCard extends StatelessWidget {
  
    @override
    Widget build(BuildContext context) {
      return Material (
        borderRadius: BorderRadius.circular(50.0),
        color: Colors.white,
        elevation: 8.0,
      );
    }
  
  }
  
  class AddContainer extends StatefulWidget {
  
    @override
    AddContainerState createState() =&gt; AddContainerState();
  
  }
  
  class AddContainerState extends State&lt;AddContainer&gt; {
  
    @override
    Widget build(BuildContext context) {
      return Material(
        borderRadius: BorderRadius.circular(50.0),
        color: Colors.white,
        elevation: 8.0,
        child: InkWell (
          onTap: _addContainer,
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
          child: Column (
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Icon (
                Icons.add,
                size: 50.0,
              )
            ]
          ),
        )
      );
    }
    void _addContainer() {
      setState(() {
        _count = _count + 1;
      });
    }
  
  }
  </code></pre>
  
  <p>But for some reason this is not working. What is wrong and how can I fix this?</p>
  
  <p>Full Code: </p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  class MainPage extends StatelessWidget {
  
    @override
      Widget build(BuildContext context) {
  
        return MaterialApp (
          debugShowCheckedModeBanner: false,
          home: Scaffold (
            backgroundColor: Colors.white,
            body: Column (
              children: &lt;Widget&gt; [
                AppBar(),
                Body(),
              ]
            )
          )
        );
      }
  
  }
  
  class AppBar extends StatelessWidget {
  
    @override
    Widget build(BuildContext context) {
  
      var abHeight = MediaQuery.of(context).size.height  * 0.2;
  
      var vPadding = MediaQuery.of(context).size.height  * 0.07;
  
      return Container (
        height: abHeight,
        child:  Column (
          mainAxisAlignment: MainAxisAlignment.end,
          children: &lt;Widget&gt; [
            Row (
              mainAxisAlignment: MainAxisAlignment.start,
              children: &lt;Widget&gt;[
                Padding(
                  padding: EdgeInsets.only(left: vPadding),
                  child: PoppinsText (
                    text: "App",
                    fontSize: 40.0,
                    fontWeight: FontWeight.bold,
                    color: Colors.black,
                  ),
                )
              ]
            )
          ]
        )
      );
    }
  
  }
  
  class Body extends StatelessWidget {
  
    @override
    Widget build(BuildContext context) {
  
      var tPadding= MediaQuery.of(context).size.width  * 0.08; 
      var bPadding= MediaQuery.of(context).size.width  * 0.15; 
      var vPadding = MediaQuery.of(context).size.height  * 0.015;
  
      return Expanded (
        child: Container (
          child: NotificationListener&lt;OverscrollIndicatorNotification&gt; (
            onNotification: (OverscrollIndicatorNotification overscroll) {
              overscroll.disallowGlow();
            },
            child: PageView.builder(
              pageSnapping: false,
              controller: PageController(viewportFraction: 0.85),
              itemCount: container.length,
              itemBuilder: (context, i) {
                return Padding (
                  padding: EdgeInsets.only(
                    left: vPadding,
                    right: vPadding,
                    top: tPadding,
                    bottom: bPadding
                  ),
                  child: container[i]
                );
              },
            )
          )
        )
      );
    }
  
  }
  
   int _count = 1;
  
  List container = [
    List.generate(_count, (int i) =&gt; ContainerCard),
    AddContainer()
  ];
  
  class ContainerCard extends StatelessWidget {
  
    @override
    Widget build(BuildContext context) {
      return Material (
        borderRadius: BorderRadius.circular(50.0),
        color: Colors.white,
        elevation: 8.0,
      );
    }
  
  }
  
  class AddContainer extends StatefulWidget {
  
    @override
    AddContainerState createState() =&gt; AddContainerState();
  
  }
  
  class AddContainerState extends State&lt;AddContainer&gt; {
  
    @override
    Widget build(BuildContext context) {
      return Material(
        borderRadius: BorderRadius.circular(50.0),
        color: Colors.white,
        elevation: 8.0,
        child: InkWell (
          onTap: _addContainer,
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
          child: Column (
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Icon (
                Icons.add,
                size: 50.0,
              )
            ]
          ),
        )
      );
    }
    void _addContainer() {
      setState(() {
        _count = _count + 1;
      });
    }
  
  }
  
  class PoppinsText extends StatelessWidget {
  
    PoppinsText ({Key key, 
      this.text, 
      this.fontSize, 
      this.fontWeight,
      this.color}) : super(key: key);
  
    final String text;
    final double fontSize;
    final FontWeight fontWeight;
    final Color color;
  
    @override
    Widget build(BuildContext context) {
      return Text (
        text,
        style: TextStyle (
          fontFamily: 'Poppins',
          fontWeight: fontWeight,
          fontSize: fontSize,
          color: color
        ),
      );
    }
  
  }
  </code></pre>