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

后端 未结 2 1802
感动是毒
感动是毒 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条回答
  •  梦毁少年i
    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 {
      List 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());
                });
              },
            ),
          ),
        );
      }
    }
    

提交回复
热议问题