CastoldiG
CastoldiG

Reputation: 312

flutter - texteditingcontroller from another class

i am fairly new to flutter, can anyone help me? pls

I would like that once I click the RawMaterialButton(), what I wrote in the TextField() appears as text in the container() class.

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

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

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

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

class _chatState extends State<chat> {
  bool changeClass = false;
  changeClassValue() {
    setState(() {
      changeClass = !changeClass;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: changeClass
          ? container(
              text: _textFieldState().textController.text,
            )
          : textField(
              changeClassValue: changeClassValue,
            ),
    );
  }
}

class textField extends StatefulWidget {
  textField({Key? key, required this.changeClassValue}) : super(key: key);

  Function changeClassValue;

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

class _textFieldState extends State<textField> {
  final textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Container(
            width: 300.0,
            height: 60.0,
            color: Colors.red,
            child: TextField(
              controller: textController,
            ),
          ),
          RawMaterialButton(
            onPressed: () {
              setState(() {
                widget.changeClassValue();
                print(textController.text);
              });
            },
            child: Icon(Icons.send),
          )
        ],
      ),
    );
  }
}

class container extends StatefulWidget {
  container({Key? key, required this.text}) : super(key: key);

  String text;

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

class _containerState extends State<container> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: double.infinity,
        height: 60.0,
        color: Colors.grey,
        child: Text(widget.text),
      ),
    );
  }
}

hope someone can help me.

Thank you :) I write this piece because otherwise it won't let me upload itI write this piece because otherwise it won't let me upload itI write this piece because otherwise it won't let me upload it

Upvotes: 0

Views: 1847

Answers (1)

Balint Takacs
Balint Takacs

Reputation: 26

You need to update the type of changeClassValue to ValueChanged<String> - now you can pass data between textField and chat states.

import 'dart:math';

import 'package:flutter/material.dart';

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

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

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

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

class _chatState extends State<chat> {
  bool changeClass = false;
  String? text;
  changeClassValue(String? newText) {
    setState(() {
      changeClass = !changeClass;
      text = newText;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: changeClass
          ? container(
              text: text ?? "",
            )
          : textField(
              changeClassValue: changeClassValue,
            ),
    );
  }
}

class textField extends StatefulWidget {
  textField({Key? key, required this.changeClassValue}) : super(key: key);

  ValueChanged<String> changeClassValue;

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

class _textFieldState extends State<textField> {
  final textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Container(
            width: 300.0,
            height: 60.0,
            color: Colors.red,
            child: TextField(
              controller: textController,
            ),
          ),
          RawMaterialButton(
            onPressed: () {
               widget.changeClassValue(textController.text);
            },
            child: Icon(Icons.send),
          )
        ],
      ),
    );
  }
}

class container extends StatefulWidget {
  container({Key? key, required this.text}) : super(key: key);

  String text;

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

class _containerState extends State<container> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: double.infinity,
        height: 60.0,
        color: Colors.grey,
        child: Text(widget.text),
      ),
    );
  }
}

Upvotes: 1

Related Questions