简体   繁体   English

flutter - 从文本到文本字段

[英]flutter - from text to textField

i am fairly new to flutter, can anyone help me?我是 flutter 的新手,有人可以帮助我吗? pls

This code make that what written in the TextField() class, once the RawMaterialButton() is pressed, transform it into text inside the container() class.这段代码使得在 TextField() class 中写入的内容,一旦按下 RawMaterialButton(),将其转换为 container() class 中的文本。

When I press the other RawMaterialButton() the code returns from the container() class to the textField() class.当我按下另一个 RawMaterialButton() 时,代码从 container() class 返回到 textField() class。

I would like once back to the textField class, i would like that once back to the textField class, the text previously written is written in the TextField I would once back to the textField class, I would that once back to the textField class, 之前写的文字写在TextField

How can I do?我能怎么做? 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;
  String? text;
  changeClassValue(String? newText) {
    setState(() {
      changeClass = !changeClass;
      text = newText;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: changeClass
          ? container(
              text: text ?? "",
              changeClassValue: changeClassValue,
            )
          : 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, required this.changeClassValue})
      : super(key: key);

  String text;
  ValueChanged<String> changeClassValue;

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

class _containerState extends State<container> {
  @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.changeClassValue(widget.text);
            },
            child: Icon(Icons.edit),
          )
        ],
      ),
    );
  }
}

hope someone can help me.希望可以有人帮帮我。

. .

Before we get into this, definitely make sure your class names start with a capital letter.在我们开始之前,一定要确保您的 class 名称以大写字母开头。 Names like Container and TextField are already defined in Flutter so you'll probably need different names for these widgets. ContainerTextField等名称已在 Flutter 中定义,因此您可能需要为这些小部件使用不同的名称。

The two widgets don't have to be StatefulWidgets but StatelessWidgets will make it easier to work with.这两个小部件不必是StatefulWidgets ,但StatelessWidgets将使它更容易使用。 The Chat widget should however be the only one with state here.然而, Chat小部件应该是这里唯一带有 state 的小部件。 This widget should have the TextEditingController in its state and pass it along to the textField to use.此小部件应在其 state 中包含TextEditingController ,并将其传递给textField以供使用。

The container should get its text property from this same TextEditingController . container应从同一个TextEditingController获取其text属性。

Both widgets should also get a callback function, such as a VoidCallback to toggle between which one to show.两个小部件还应获得回调 function,例如VoidCallback以在显示哪个小部件之间切换。

Here's an example:这是一个例子:

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 StatelessWidget {
  final TextEditingController textController;
  final VoidCallback toggleClass;

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

  @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: toggleClass,
            child: Icon(Icons.send),
          )
        ],
      ),
    );
  }
}

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM