flutter 使用文本编辑控制器更新文本字段值

djp7away  于 2023-01-21  发布在  Flutter
关注(0)|答案(2)|浏览(259)

我想更新TextField中的值,但它变成了这样

这是我的发行代码

Widget build(BuildContext context) {
        final certificatesData = Provider.of < Certificates > (context);
        final cerData = certificatesData.certData;

        if (cerData != null) {
            print("test in");
            inspectionTypeDis = cerData['inspectionType'];
            _clientDataL = clientDis;
            if (_clientDataL != null) {
                getClientEmailL(_clientDataL);
            }

        }
        if (emailClientDataL != null) {
            _emailClientDataL = clientEmailDis;
        }

        return Form(
                key: _formKey,
                child: AlertDialog(
                    title: Container(
                        color: Color.fromARGB(255, 75, 185, 159),
                        child: Text('Edit',
                            textAlign: TextAlign.center, style: TextStyle(color: Color.fromARGB(255, 250, 251, 250))),
                        padding: const EdgeInsets.all(17),
                            margin: const EdgeInsets.all(0),
                    ),

                    content: SingleChildScrollView(
                        child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: < Widget > [
                                SizedBox(
                                    width: 630,
                                    height: 100,
                                    child: ListView(
                                        shrinkWrap: true,
                                        scrollDirection: Axis.horizontal,
                                        children: < Widget > [
                                            Container(
                                                width: 310,
                                                height: 20,
                                                // color: Colors.purple[600],
                                                child: ListTile(
                                                    title: Text('Inspection Type'),
                                                    subtitle: TextField(
                                                        controller: TextEditingController(text: inspectionTypeDis),
                                                        onChanged: (text) {
                                                            inspectionTypeDis = text;
                                                        },
                                                        decoration: const InputDecoration(
                                                            border: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(4))),
                                                        ),
                                                    ),
                                                ),
                                            ),

                                        ),
                                    ]),
                            ],
                        ));

                }

明细代码

class EditCertificateInspection extends StatefulWidget with InputValidationMixin {
    EditCertificateInspection({
        Key key
    }): super(key: key);
    @override
    _EditCertificateInspection createState() => _EditCertificateInspection();
}

class InputValidationMixin {}

class _EditCertificateInspection extends State < EditCertificateInspection > {
        final navigatorKey = GlobalKey < NavigatorState > ();
        final _formKey = GlobalKey < FormState > ();


        Widget build(BuildContext context) {

            final certificatesData = Provider.of < Certificates > (context);
            final cerData = certificatesData.certData;

            if (cerData != null) {
                print("test in");
                inspectionTypeDis = cerData['inspectionType'];
                _clientDataL = clientDis;
                if (_clientDataL != null) {
                    getClientEmailL(_clientDataL);
                }

            }
            if (emailClientDataL != null) {
                _emailClientDataL = clientEmailDis;
            }

            return Form(
                key: _formKey,
                child: AlertDialog(
                    title: Container(
                        color: Color.fromARGB(255, 75, 185, 159),
                        child: Text('Edit',
                            textAlign: TextAlign.center, style: TextStyle(color: Color.fromARGB(255, 250, 251, 250))),
                        padding: const EdgeInsets.all(17),
                            margin: const EdgeInsets.all(0),
                    ),

                    content: SingleChildScrollView(
                        child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: < Widget > [
                                SizedBox(
                                    width: 630,
                                    height: 100,
                                    child: ListView(
                                        shrinkWrap: true,
                                        scrollDirection: Axis.horizontal,
                                        children: < Widget > [
                                            Container(
                                                width: 310,
                                                height: 20,
                                                child: ListTile(
                                                    title: Text('Inspection Type'),
                                                    subtitle: TextField(
                                                        controller: TextEditingController(text: inspectionTypeDis),
                                                        onChanged: (text) {
                                                            inspectionTypeDis = text;
                                                        },
                                                        decoration: const InputDecoration(
                                                            border: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(4))),
                                                        ),
                                                    ),
                                                ),
                                            ),

                                        ),
                                    ]),
                            ],
                        ));

                },
            }
wkyowqbh

wkyowqbh1#

我建议为statefulwidget的TextEditingController创建一个状态变量并设置文本。

TextEditingController.fromValue(TextEditingValue(text: inspectionTypeDis));

小部件结构可以是

final TextEditingController controller = TextEditingController();
@override
Widget build(BuildContext context) {
  return Consumer<Certificates>(builder: (context, value, child) {
    final cerData = value....;
    if (cerData != null) {

    //  your logic
      controller.text =  "";
    }
    return TextFormField(
      controller: controller,
    );
  });
}
dz6r00yl

dz6r00yl2#

您可以做的是将您的TextField更改为具有initialValueTextFormField

TextFormField(
        initialValue: cerData['inspectionType'],
        onChanged: (text) {
          inspectionTypeDis = text;
        },
        decoration: const InputDecoration(
          border: OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(4))),
        ),
    • 编辑**

下面是使用StatefulWidget的完整示例,其中使用了我上面提供的代码片段。

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

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

class _TestState extends State<Test> {
  String _inspectionTypeDis = 'initialValue';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          TextFormField(
            initialValue: _inspectionTypeDis,
            onChanged: (text) {
              _inspectionTypeDis = text;
            },
            decoration: const InputDecoration(
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(4))),
            ),
          ),
          TextButton(
              child: const Text('Print value'),
              onPressed: () => print(_inspectionTypeDis)),
        ],
      ),
    );
  }
}

相关问题