'flutter - text to textfield()

I'm new to flutter, can someone help me?

I created this code that transforms the ChatTextField() class into ChatContainer(), the text written in the TextField becomes text in the ChatContainer and then pressing a Button returns the text to the TextField.

However, once back in the Textfield, I cannot write to the TextField anymore.

Anyone know how I can fix it?

Thank you.

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;
  final textController = TextEditingController();

  toggleClass() {
    setState(() {
      changeClass = !changeClass;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: changeClass
          ? ChatContainer(
              text: textController.text,
              toggleClass: toggleClass,
            )
          : ChatTextField(
              textController: textController,
              toggleClass: toggleClass,
            ),
    );
  }
}

class ChatTextField extends StatefulWidget {
  final TextEditingController textController;
  final VoidCallback toggleClass;

  ChatTextField({
    Key? key,
    required this.textController,
    required this.toggleClass,
  }) : super(key: key);

  @override
  State<ChatTextField> createState() => _ChatTextFieldState();
}

class _ChatTextFieldState extends State<ChatTextField> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Container(
            width: 300.0,
            height: 60.0,
            color: Colors.red,
            child: TextField(
              controller: widget.textController,
            ),
          ),
          RawMaterialButton(
            onPressed: widget.toggleClass,
            child: Icon(Icons.send),
          )
        ],
      ),
    );
  }
}

class ChatContainer extends StatefulWidget {
  final String text;
  final VoidCallback toggleClass;

  ChatContainer({
    Key? key,
    required this.text,
    required this.toggleClass,
  }) : super(key: key);

  @override
  State<ChatContainer> createState() => _ChatContainerState();
}

class _ChatContainerState extends State<ChatContainer> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Container(
            width: 300.0,
            height: 60.0,
            color: Colors.grey,
            child: Text(widget.text),
          ),
          RawMaterialButton(
            onPressed: widget.toggleClass,
            child: Icon(Icons.edit),
          )
        ],
      ),
    );
  }
}



Solution 1:[1]

Just added const to some places in your code and its working fine now.

import 'package:flutter/material.dart';

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

class MainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const 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;
  final textController = TextEditingController();

  toggleClass() {
    setState(() {
      changeClass = !changeClass;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: changeClass
          ? ChatContainer(
              text: textController.text,
              toggleClass: toggleClass,
            )
          : ChatTextField(
              textController: textController,
              toggleClass: toggleClass,
            ),
    );
  }
}

class ChatTextField extends StatefulWidget {
  final TextEditingController textController;
  final VoidCallback toggleClass;

  const ChatTextField({
    Key? key,
    required this.textController,
    required this.toggleClass,
  }) : super(key: key);

  @override
  State<ChatTextField> createState() => _ChatTextFieldState();
}

class _ChatTextFieldState extends State<ChatTextField> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Container(
            width: 150.0,
            height: 60.0,
            color: Colors.red,
            child: TextField(
              controller: widget.textController,
            ),
          ),
          RawMaterialButton(
            onPressed: widget.toggleClass,
            child: const Icon(Icons.send),
          )
        ],
      ),
    );
  }
}

class ChatContainer extends StatefulWidget {
  final String text;
  final VoidCallback toggleClass;

  const ChatContainer({
    Key? key,
    required this.text,
    required this.toggleClass,
  }) : super(key: key);

  @override
  State<ChatContainer> createState() => _ChatContainerState();
}

class _ChatContainerState extends State<ChatContainer> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Container(
            width: 150.0,
            height: 60.0,
            color: Colors.grey,
            child: Text(widget.text),
          ),
          RawMaterialButton(
            onPressed: widget.toggleClass,
            child: const Icon(Icons.edit),
          )
        ],
      ),
    );
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bilal Saeed