简体   繁体   English

Flutter 从小部件返回数据

[英]Flutter Returning Data from Widget

Before anything I first apology for my English.在任何事情之前,我首先为我的英语道歉。

So in order to write pretty code, I've parsed my code in multiple file.因此,为了编写漂亮的代码,我在多个文件中解析了我的代码。 To just call widget and make easy to read code.只需调用小部件并使代码易于阅读。

But one of my widget is a FormField and I don't know how to recover the input out of the widget in my main screen.但是我的一个小部件是 FormField,我不知道如何从主屏幕的小部件中恢复输入。

Here is the main screen code:这是主屏幕代码:

import 'package:ChatApp/pretty_form.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: SignUp(),
    );
  }
}

class SignUp extends StatefulWidget {
  SignUp({
    Key key,
  }) : super(key: key);

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

class _SignUpState extends State<SignUp> {
  String email = "";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("SignUp"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            email = PrettyForm(text: "Enter your email adress"),
            PrettyForm(text: "Enter your mdp"),
          ],
        ),
      ),
    );
  }
}

And here is the code the I import in the first file:这是我在第一个文件中导入的代码:

  import 'package:flutter/material.dart';
  import 'tools.dart';

  class PrettyForm extends StatefulWidget {
    PrettyForm({Key key, this.text, this.height, this.width}) : super(key: key);
    String text;
    double height;
    double width;
    @override
    _PrettyFormState createState() => _PrettyFormState();
  }

  class _PrettyFormState extends State<PrettyForm> {
    @override
    Widget build(BuildContext context) {
      double screen_height = MediaQuery.of(context).size.height;
      double screen_width = MediaQuery.of(context).size.width;
      final form_controller = TextEditingController();
      double height = my_percent(5, screen_height);

      return Container(
        width: width,
        height: height,
        margin: EdgeInsets.all(my_percent(2, screen_height)),
        decoration: BoxDecoration(
            borderRadius: new BorderRadius.all(Radius.circular(18.0)),
            border: Border.all(color: Colors.grey)),
        child: TextFormField(
          textAlign: TextAlign.center,
          controller: form_controller,
          decoration:
              InputDecoration(border: InputBorder.none, hintText: widget.text),
          onChanged: (value) {
            print(value);
          },
        ),
      );
    }
  }

If you look at the first PrettyForm call I'm trying to recover the email input in a String.如果您查看第一个 PrettyForm 调用,我正在尝试恢复字符串中的 email 输入。 How can I return the input value from the call to PrettyForm widget to the SignUp widget (SignUp is the main screen)如何将输入值从对 PrettyForm 小部件的调用返回到 SignUp 小部件(SignUp 是主屏幕)

you need to use onValueChanged property你需要使用onValueChanged 属性

import 'package:ChatApp/pretty_form.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: SignUp(),
    );
  }
}

class SignUp extends StatefulWidget {
  SignUp({
    Key key,
  }) : super(key: key);

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

class _SignUpState extends State<SignUp> {
  String email = "";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("SignUp"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            email = PrettyForm(text: "Enter your email adress",(newValue) => email),
            PrettyForm(text: "Enter your mdp",(newValue) => password),
          ],
        ),
      ),
    );
  }
}

form形式

import 'package:flutter/material.dart';
import 'tools.dart';

class PrettyForm extends StatefulWidget {
  PrettyForm({
    Key key,
    this.text,
    this.height,
    this.width,
    this.onChanged,
  }) : super(key: key);
  String text;
  double height;
  double width;

  // add this
  final ValueChanged<String> onChanged;

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

class _PrettyFormState extends State<PrettyForm> {
  @override
  Widget build(BuildContext context) {
    double screen_height = MediaQuery.of(context).size.height;
    double screen_width = MediaQuery.of(context).size.width;
    final form_controller = TextEditingController();
    double height = my_percent(5, screen_height);

    return Container(
      width: width,
      height: height,
      margin: EdgeInsets.all(my_percent(2, screen_height)),
      decoration: BoxDecoration(
          borderRadius: new BorderRadius.all(Radius.circular(18.0)),
          border: Border.all(color: Colors.grey)),
      child: TextFormField(
        textAlign: TextAlign.center,
        controller: form_controller,
        decoration:
            InputDecoration(border: InputBorder.none, hintText: widget.text),
        onChanged: (value) {
          print(value);
          widget.onChanged(newValue);
        },
      ),
    );
  }
}

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

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