Verc
Verc

Reputation: 195

How do I transfer the text entered in the TextField to another widget?

I have a TextField where I enter text, I would like to transfer this text to another file in Text, if possible. The problem is that I already use the controller to check if there is text in the TextField and, depending on the state, I change the color in the ElevatedButton. How do I create another controller for text transmission ? or how to use an existing one

  class __AurButtnonWidghetState extends State<_AurButtnonWidghet> {
  final _textController = TextEditingController();
  final _formKey = GlobalKey<FormState>();
  final successColor =
      MaterialStateProperty.all(Color.fromARGB(255, 62, 126, 199));
  final errorColor =
      MaterialStateProperty.all(Color.fromARGB(255, 145, 144, 144));

  bool isValid = false;
  @override
  void initState() {
    super.initState();
    _textController.addListener(() {
      if (_textController.text.isEmpty) {
        setState(() {
          isValid = false;
        });
      } else {
        setState(() {
          isValid = true;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      autovalidateMode: AutovalidateMode.onUserInteraction,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          children: [
            TextFormField(
              controller: _textController,
              decoration: AppTextFieldStyle.LinkButton,
              validator: (value) {
                () {};
              },
            ),
            SizedBox(height: 15),
            ElevatedButton(
                onPressed: !isValid
                    ? null
                    : () {
                        Navigator.of(context).pushNamed('/password_screen');
                        // go  further.
                      },
                style: ButtonStyle(
                    backgroundColor: isValid ? successColor : errorColor,
                    minimumSize: MaterialStateProperty.all(Size.fromHeight(1)),
                    foregroundColor: MaterialStateProperty.all(Colors.white),
                    textStyle: MaterialStateProperty.all(
                        TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
                    padding: MaterialStateProperty.all(
                        EdgeInsets.symmetric(horizontal: 10, vertical: 10))),
                child: Text('Next')),

In another file I have a Text where I would like to insert the entered text

  Text(
            'Use the password specified during registration  (+Entered Text)' ,
            style: AppDescribeTittleStyle.LinkButton,
            textAlign: TextAlign.center,
          ),

Upvotes: 0

Views: 764

Answers (1)

Kaushik Chandru
Kaushik Chandru

Reputation: 17732

In screen1 use

Navigator.push(
    context,
    MaterialPageRoute<void>(
      builder: (BuildContext context) => PasswordScreen(passwordText : _textController.text),
    ),
  );

In screen2

class SecondScreen extends StatelessWidget {
  final String passwordText;
  SecondScreen({Key key, required this.passwordText}) : super(key: key);
.....

Now in the second screen you can use it like

Text('$passwordText also someother text here');

Upvotes: 1

Related Questions