0

我正在尝试在按下按钮时使用 textformfields 构建每张卡片,直到达到某个限制,例如仅 10 张卡片,所以当我按下浮动按钮时,卡片正在添加,但每次按下时都会擦除 textformfields 中的文本。任何人都可以帮助我使用 textformfields 动态构建卡片,而无需每次按下都擦除吗?下面我附上应用程序的屏幕截图。第一张卡按下按钮后文本正在擦除

 import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CardWithTextformfield(),
    );
  }
}

class CardWithTextformfield extends StatefulWidget {
  const CardWithTextformfield({Key? key}) : super(key: key);

  @override
  _CardWithTextformfieldState createState() => _CardWithTextformfieldState();
}

class _CardWithTextformfieldState extends State<CardWithTextformfield> {

  var name =<TextEditingController>[];
  var id =<TextEditingController>[];

  var addCard =0;

  void incrementcard(){
    setState(() {
      if(addCard >=7){
        return;
      }
      addCard++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Card with TextformField'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: incrementcard,
        child: Icon(Icons.add),
      ),
      body: Container(
        child:ListView.builder(
          itemCount: addCard,
            itemBuilder: (context,index){
            return cardslist();
            }
        ),
      ),
    );
  }
  Widget cardslist(){
    var nameController = TextEditingController();
    var idController = TextEditingController();
    name.add(nameController);
    id.add(idController);
    return Card(
      margin: EdgeInsets.all(10),
      child: Container(
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                    margin: EdgeInsets.all(10),
                    child: Text('Name: ')),
                Expanded(child: TextFormField(
                  controller: nameController,
                    decoration: InputDecoration(hintText: 'Name'),
                ),),
                Container(
                  margin: EdgeInsets.all(10),
                  child: Text('EmpId: '),),
                Expanded(child: TextFormField(
                  controller: idController,
                    decoration: InputDecoration(hintText: 'EmpId'),
                ),),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
4

1 回答 1

1

您的方法的问题是在插入新卡时重新创建每个控制器,这就是输入值丢失的原因。相反,您应该检查当前卡的阵列中是否已经存在控制器,如果是,请使用现有控制器,否则创建一个新控制器。

为此,您必须将index其作为参数添加到您的cardsList方法中。我认为下面的代码可以解决您的问题。

重要提示:当这个小部件被销毁时,控制器应该被销毁,请添加该部分。你应该重写dispose你的方法_CardWithTextformfieldState,循环遍历两个数组中的所有控制器,并调用dispose()每个控制器。最后别忘了打电话super.dispose()

import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CardWithTextformfield(),
    );
  }
}

class CardWithTextformfield extends StatefulWidget {
  const CardWithTextformfield({Key? key}) : super(key: key);

  @override
  _CardWithTextformfieldState createState() => _CardWithTextformfieldState();
}

class _CardWithTextformfieldState extends State<CardWithTextformfield> {
  final _name = <TextEditingController>[];
  final _id = <TextEditingController>[];
  int _addCard = 0;

  void _incrementcard() {
    setState(() {
      if (_addCard >= 7) {
        return;
      }
      _addCard++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Card with TextformField'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementcard,
        child: Icon(Icons.add),
      ),
      body: Container(
        child: ListView.builder(
            itemCount: _addCard,
            itemBuilder: (context, index) {
              return cardslist(index);
            }),
      ),
    );
  }

  Widget cardslist(int index) {
    if (_name.length <= index) {
      _name.add(TextEditingController());
      _id.add(TextEditingController());
    }

    return Card(
      margin: EdgeInsets.all(10),
      child: Container(
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(margin: EdgeInsets.all(10), child: Text('Name: ')),
                Expanded(
                  child: TextFormField(
                    controller: _name[index],
                    decoration: InputDecoration(hintText: 'Name'),
                  ),
                ),
                Container(
                  margin: EdgeInsets.all(10),
                  child: Text('EmpId: '),
                ),
                Expanded(
                  child: TextFormField(
                    controller: _id[index],
                    decoration: InputDecoration(hintText: 'EmpId'),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

于 2021-08-26T17:17:17.187 回答