flutter - 来自另一个 class 的 texteditingcontroller

flutter - texteditingcontroller from another class

我是 flutter 的新手,有人可以帮助我吗?请

我希望在单击 RawMaterialButton() 后,我在 TextField() 中写入的内容在 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),
      ),
    );
  }
}

希望有人能帮助我。

谢谢 :) 我写这篇是因为它不让我上传我写这篇是因为它不让我上传我写这篇是因为它不让我上传

您需要将 changeClassValue 的类型更新为 ValueChanged<String> - 现在您可以在 textFieldchat 状态之间传递数据。

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),
      ),
    );
  }
}