Adding dynamic widget with button

Viewed 1015

I am facing a strange bug when I try to add a dynamic widget to my app. When press add button screen turns completely white I can not find why it happens. I use https://www.youtube.com/watch?v=xPW1vtDDlt4 as resource I am really new at Flutter maybe I forget something to add bu I check many times. Here is my code,

class DynamicWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: new TextField(
        decoration: new InputDecoration(hintText: 'Press + to Add Field'),
      ),
    );
  }
}

Initialization of the list.

  List<DynamicWidget> listDynamic = [];

My function to add widgets to the list.

  addDynamic() {
    listDynamic.add(new DynamicWidget());
    print("addDynamic");
    setState(() {});
  }

I am not sure but problem might be here,

final testText = Visibility(
  child: new Column(
    children: <Widget>[
      new Flexible(
        child: new ListView.builder(
          itemCount: listDynamic.length,
          itemBuilder: (_, index) => listDynamic[index],
        ),
      ),
    ],
  ),
);

Here I call my widget which I declare it to variable here.

final body = Container(
      child: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            testText,
            strPhoto
          ],
        ),
      ),
    );

And finally my button.

    return Scaffold(
        appBar: new AppBar(title: new Text(device_type), centerTitle: true),
        drawer: Menu(),
        body: body,
        floatingActionButton: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            FloatingActionButton(
              heroTag: null,
              child: Icon(
                Icons.add,
                color: Colors.white,
              ),
              onPressed: () {
                addDynamic();
              },
            ),
          ],
        ));
 

Thanks for helping me.

2 Answers

you must create variable Widget and add to Build Context on Scaffold

In the first step, i created a Widget called CustomTextField

import 'package:flutter/material.dart';

class CustomTextField extends StatelessWidget {
final String hint;
final TextEditingController controllers;

CustomTextField(this.hint, this.controllers);

    @override
    Widget build(BuildContext context) {
    return Padding(
        padding: EdgeInsets.only(top: 2.0, left: 6.0, right: 6.0, bottom: 2.0),
        child: Column(
          children: <Widget>[
            TextField(
                decoration: InputDecoration(hintText: hint),
                controller: controllers),
            SizedBox(height: 4.0),
          ],
        ),
      );
    }
}

this widget give me one text for display on hint and one controller for control textfield

And in the next step, I change the homepage class this way i have list of Custom TextField (my widget) and display on listview using mapping list of build method

import 'package:flutter/material.dart';
import 'CustomTextField.dart';

class PageTutorial extends StatefulWidget {
  @override
  _PageTutorialState createState() => _PageTutorialState();
}

class _PageTutorialState extends State<PageTutorial> {
  List<CustomTextField> widgets = [
    CustomTextField("UserName", TextEditingController())
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: widgets.map<Widget>((widget) => widget).toList(),
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() {
              widgets.add(CustomTextField("Password", TextEditingController()));
            });
          },
          child: Icon(Icons.add)),
    );
  }
}

have fun

Related