0

I want to pass a variable name as a function parameter, but it doesn't seem to work : the content of my variable remains unchanged.

  Widget Field(String changedValue, String label, bool isTextObscured) {
    return TextFormField(
      decoration: InputDecoration(labelText: label),
      validator: checkFieldEmpty,
      onChanged: (value) {
        setState(() {
          changedValue = value;
        });
      },
      obscureText: isTextObscured,
    );
  }

Here, I want to change the value of the variable who has the name "changedValue". When I do it directly with the variable name, it works, but when I try to use the parameter, nothing happens. Here's an example of where I used it :

  Widget LoginFields() {
    return Column(
      children: [
        Field(email, Strings.emailLabel, false),
        Field(password, Strings.passwordLabel, true),
        ValidationButton(),
      ],
    );
  }

Thanks in advance!

gabhar
  • 187
  • 1
  • 4
  • 14
  • See https://stackoverflow.com/q/25170094/. If you need to mutate something in the caller's environment, you will need to pass an object and have the callee mutate (not reassign) that object. – jamesdlin Jun 16 '21 at 10:21

1 Answers1

0

There are many things to clarify here, like:

  • setState() is a method, that must be called inside a StatefullWidget.
  • if you create a function, name it with lowerCamelCase (effective dart).
  • for returning a Widget prefer extend a Widget, especially if you need a State.
  • if you seek a guide for TextField in Flutter - check cookbook here and here.

Here how you can set it up:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
          child: Column(
            children: [
              FieldWidget(changedValueInitial: 'email', label: 'labelConstOne'),
              FieldWidget(changedValueInitial: 'password', label: 'labelConstTwo', isTextObscured: true),
              // ValidationButton(),
            ],
          ),
        ),
      ),
    );
  }
}

class FieldWidget extends StatefulWidget {
  String changedValueInitial;
  String label;
  bool isTextObscured;
  FieldWidget({
    Key? key,
    required this.changedValueInitial,
    required this.label,
    this.isTextObscured = false,
  }) : super(key: key);

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

class _FieldWidgetState extends State<FieldWidget> {
  late String _changedValue;

  @override
  void initState() {
    super.initState();
    _changedValue = widget.changedValueInitial;
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      decoration: InputDecoration(labelText: widget.label),
      // validator: yourValidator,
      initialValue: _changedValue,
      onChanged: (value) {
        setState(() {
          _changedValue = value;
        });
      },
      obscureText: widget.isTextObscured,
    );
  }
}

If that is what you need..

enter image description here

Simon Sot
  • 2,748
  • 2
  • 10
  • 23