Add New Widget on button click with a finction that returns a widget

后端 未结 2 1801
感动是毒
感动是毒 2021-01-27 16:26

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 kn

相关标签:
2条回答
  • 2021-01-27 16:45

    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());
                  });
                },
              )
            ],
          ),
        );
      }
    }
    
    
    0 讨论(0)
  • 2021-01-27 16:51

    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());
                });
              },
            ),
          ),
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题