Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 我不能发送控制器映射到返回小部件的函数?_Flutter_Dart - Fatal编程技术网

Flutter 我不能发送控制器映射到返回小部件的函数?

Flutter 我不能发送控制器映射到返回小部件的函数?,flutter,dart,Flutter,Dart,我尝试创建textededitingcontroller并将其发送到此控制器,以实现我的功能,我想使用按下按钮on的方法。当我点击按钮并打印函数变量时,它是空的映射。如何将控制器发送到其他函数 这是我的密码: import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'funcs.dart'; class EmailRegister extends StatefulWidget

我尝试创建
textededitingcontroller
并将其发送到此控制器,以实现我的功能,我想使用按下按钮
on
的方法。当我点击按钮并打印函数变量时,它是空的映射。如何将控制器发送到其他函数

这是我的密码:

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

import 'funcs.dart';

class EmailRegister extends StatefulWidget {
  EmailRegister() : super();

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

class _EmailRegisterState extends State<EmailRegister> {
  TextEditingController _email;
  TextEditingController _pass;
  TextEditingController _name;
  var Controllers = new Map();

  @override
  void initState() {
    _email = new TextEditingController();
    _pass = new TextEditingController();
    _name = new TextEditingController();
    Controllers['email'] = _email;
    Controllers['name'] = _name;
    Controllers['pass'] = _pass;
    super.initState();
  }

  @override
  void dispose(){
    _email.dispose();
    _pass.dispose();
    _name.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const SizedBox(height: 30),
            CreateFormLabel("Email", 20, 20, false, _email),
            const SizedBox(height: 20),
            CreateFormLabel("UserName", 20, 20, false, _name),
            const SizedBox(height: 20),
            CreateFormLabel("Password", 20, 20, true, _pass),
            const SizedBox(height: 20),
            CreateFormButton(
                "register",
                18,
                40.0,
                10.0,
                40.0,
                10.0,
                Colors.blue,
                Colors.white,
                Colors.white, Controllers),
          ],
        ),
      ),
    );
  }
}
您可以使用
Map Controllers=newmap()

代码片段

Widget CreateFormButton(
      String text,
      double FontSize,
      double L,
      double T,
      double R,
      double B,
      color,
      textClor,
      splashColor,
      Map<String, TextEditingController> controllers) {
    return Center(
      child: FlatButton(
        onPressed: () {
          print(Controllers['email'].text);
          print(Controllers['name'].text);
          print(Controllers['pass'].text);
小部件CreateFormButton(
字符串文本,
双倍大小,
双L,
双T,
双R,
双B,
颜色
textClor,
飞溅的颜色,
地图控制器){
返回中心(
孩子:扁平按钮(
已按下:(){
打印(控制器['email'].text);
打印(控制器['name'].text);
打印(控制器['pass'].text);
完整代码

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: EmailRegister(),
    );
  }
}


class EmailRegister extends StatefulWidget {
  EmailRegister() : super();

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

class _EmailRegisterState extends State<EmailRegister> {
  TextEditingController _email;
  TextEditingController _pass;
  TextEditingController _name;
  Map<String, TextEditingController> Controllers = new Map();

  @override
  void initState() {
    _email = new TextEditingController();
    _pass = new TextEditingController();
    _name = new TextEditingController();
    Controllers['email'] = _email;
    Controllers['name'] = _name;
    Controllers['pass'] = _pass;
    super.initState();
  }

  @override
  void dispose() {
    _email.dispose();
    _pass.dispose();
    _name.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const SizedBox(height: 30),
            CreateFormLabel("Email", 20, 20, false, _email),
            const SizedBox(height: 20),
            CreateFormLabel("UserName", 20, 20, false, _name),
            const SizedBox(height: 20),
            CreateFormLabel("Password", 20, 20, true, _pass),
            const SizedBox(height: 20),
            CreateFormButton("register", 18, 40.0, 10.0, 40.0, 10.0,
                Colors.blue, Colors.white, Colors.white, Controllers),
          ],
        ),
      ),
    );
  }

  Widget CreateFormLabel(String text, double edgeInsetL, double edgeInsetR,
      bool obscureText, controller) {
    return Container(
      margin: EdgeInsets.only(right: edgeInsetR, left: edgeInsetL),
      child: TextField(
        obscureText: obscureText,
        decoration: InputDecoration(
          border: OutlineInputBorder(),
          labelText: text,
        ),
        controller: controller,
      ),
    );
  }

  Widget CreateFormButton(
      String text,
      double FontSize,
      double L,
      double T,
      double R,
      double B,
      color,
      textClor,
      splashColor,
      Map<String, TextEditingController> controllers) {
    return Center(
      child: FlatButton(
        onPressed: () {
          print(Controllers['email'].text);
          print(Controllers['name'].text);
          print(Controllers['pass'].text);
        },
        child: Text(
          text,
          style: TextStyle(fontSize: FontSize),
        ),
        padding: EdgeInsets.fromLTRB(L, T, R, B),
        color: color,
        textColor: textClor,
        splashColor: splashColor,
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
//这是应用程序的主题。
//
//尝试使用“flutter run”运行应用程序。您将看到
//应用程序有一个蓝色工具栏。然后,在不退出应用程序的情况下,重试
//将下面的primarySwatch更改为Colors.green,然后调用
//“热重新加载”(在运行“颤振运行”的控制台中按“r”,
//或者只需将更改保存到颤振IDE中的“热重新加载”。
//请注意,计数器没有重置回零;应用程序
//未重新启动。
主样本:颜色。蓝色,
),
主页:EmailRegister(),
);
}
}
类EmailRegister扩展StatefulWidget{
EmailRegister():super();
@凌驾
_EmailRegisterState createState()=>\u EmailRegisterState();
}
类\u EmailRegisterState扩展状态{
文本编辑控制器\u电子邮件;
文本编辑控制器_pass;
TextEditingController\u名称;
映射控制器=新映射();
@凌驾
void initState(){
_电子邮件=新文本编辑控制器();
_pass=新文本编辑控制器();
_name=新文本编辑控制器();
控制器['email']=\u电子邮件;
控制器['name']=\u名称;
控制器['pass']=\u pass;
super.initState();
}
@凌驾
无效处置(){
_email.dispose();
_pass.dispose();
_name.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
//TODO:实现构建
返回脚手架(
正文:中(
子:列(
crossAxisAlignment:crossAxisAlignment.start,
mainAxisSize:mainAxisSize.min,
儿童:[
const SizedBox(高度:30),
CreateFormLabel(“电子邮件”,20,20,false,_电子邮件),
const SizedBox(高度:20),
CreateFormLabel(“用户名”,20,20,false,_name),
const SizedBox(高度:20),
CreateFormLabel(“密码”,20,20,true,_pass),
const SizedBox(高度:20),
CreateFormButton(“寄存器”,18,40.0,10.0,40.0,10.0,
颜色。蓝色,颜色。白色,颜色。白色,控制器),
],
),
),
);
}
Widget CreateFormLabel(字符串文本、双edgeInsetL、双edgeInsetR、,
布尔模糊文本(控制器){
返回容器(
边距:仅限边集(右:边集,左:边集),
孩子:TextField(
蒙蔽文本:蒙蔽文本,
装饰:输入装饰(
边框:OutlineInputBorder(),
labelText:text,
),
控制器:控制器,
),
);
}
小部件CreateFormButton(
字符串文本,
双倍大小,
双L,
双T,
双R,
双B,
颜色
textClor,
飞溅的颜色,
地图控制器){
返回中心(
孩子:扁平按钮(
已按下:(){
打印(控制器['email'].text);
打印(控制器['name'].text);
打印(控制器['pass'].text);
},
子:文本(
文本
样式:文本样式(fontSize:fontSize),
),
填充:从LTRB(L、T、R、B)开始的边缘设置,
颜色:颜色,
textColor:textClor,
splashColor:splashColor,
),
);
}
}
工作演示

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: EmailRegister(),
    );
  }
}


class EmailRegister extends StatefulWidget {
  EmailRegister() : super();

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

class _EmailRegisterState extends State<EmailRegister> {
  TextEditingController _email;
  TextEditingController _pass;
  TextEditingController _name;
  Map<String, TextEditingController> Controllers = new Map();

  @override
  void initState() {
    _email = new TextEditingController();
    _pass = new TextEditingController();
    _name = new TextEditingController();
    Controllers['email'] = _email;
    Controllers['name'] = _name;
    Controllers['pass'] = _pass;
    super.initState();
  }

  @override
  void dispose() {
    _email.dispose();
    _pass.dispose();
    _name.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const SizedBox(height: 30),
            CreateFormLabel("Email", 20, 20, false, _email),
            const SizedBox(height: 20),
            CreateFormLabel("UserName", 20, 20, false, _name),
            const SizedBox(height: 20),
            CreateFormLabel("Password", 20, 20, true, _pass),
            const SizedBox(height: 20),
            CreateFormButton("register", 18, 40.0, 10.0, 40.0, 10.0,
                Colors.blue, Colors.white, Colors.white, Controllers),
          ],
        ),
      ),
    );
  }

  Widget CreateFormLabel(String text, double edgeInsetL, double edgeInsetR,
      bool obscureText, controller) {
    return Container(
      margin: EdgeInsets.only(right: edgeInsetR, left: edgeInsetL),
      child: TextField(
        obscureText: obscureText,
        decoration: InputDecoration(
          border: OutlineInputBorder(),
          labelText: text,
        ),
        controller: controller,
      ),
    );
  }

  Widget CreateFormButton(
      String text,
      double FontSize,
      double L,
      double T,
      double R,
      double B,
      color,
      textClor,
      splashColor,
      Map<String, TextEditingController> controllers) {
    return Center(
      child: FlatButton(
        onPressed: () {
          print(Controllers['email'].text);
          print(Controllers['name'].text);
          print(Controllers['pass'].text);
        },
        child: Text(
          text,
          style: TextStyle(fontSize: FontSize),
        ),
        padding: EdgeInsets.fromLTRB(L, T, R, B),
        color: color,
        textColor: textClor,
        splashColor: splashColor,
      ),
    );
  }
}