Flutter 无法从颤振中的其他类导入变量

Flutter 无法从颤振中的其他类导入变量,flutter,authentication,Flutter,Authentication,我正在退出一个登录屏幕tempalte,并试图为一个按钮获取一个小部件类,以便在屏幕上以警告的形式显示用户名输入。usernameinput小部件已定义,但当我导入它时,它不起作用 class _InputEmailState extends State<InputEmail> { final myController = new TextEditingController(); 当我尝试引用myController.text值时,会出现错误 The getter 'myC

我正在退出一个登录屏幕tempalte,并试图为一个按钮获取一个小部件类,以便在屏幕上以警告的形式显示用户名输入。usernameinput小部件已定义,但当我导入它时,它不起作用

class _InputEmailState extends State<InputEmail> {

   final myController = new TextEditingController();
当我尝试引用myController.text值时,会出现错误

The getter 'myController' isn't defined for the class '_ButtonLoginState'.

import 'package:flutter/material.dart';
import 'package:login_minimalist/widget/inputEmail.dart';

class ButtonLogin extends StatefulWidget {
  @override
  ButtonLoginState createState() => ButtonLoginState();
}

class ButtonLoginState extends State<ButtonLogin> {
  @override
未为类“\u ButtonLogginState”定义获取程序“myController”。
进口“包装:颤振/材料.省道”;
导入“package:login_minimalist/widget/inputEmail.dart”;
类ButtonLogin扩展StatefulWidget{
@凌驾
ButtonLoginState createState()=>ButtonLoginState();
}
类ButtonLoginState扩展状态{
@凌驾
以下是按钮小部件代码:

Widget build(BuildContext context) {
return Padding(
  padding: const EdgeInsets.only(top: 40, right: 50, left: 200),
  child: Container(
    alignment: Alignment.bottomRight,
    height: 50,
    width: MediaQuery.of(context).size.width,
    decoration: BoxDecoration(
      boxShadow: [
        BoxShadow(
          color: Colors.blue[300],
          blurRadius: 10.0, // has the effect of softening the shadow
          spreadRadius: 1.0, // has the effect of extending the shadow
          offset: Offset(
            5.0, // horizontal, move right 10
            5.0, // vertical, move down 10
          ),
        ),
      ],
      color: Colors.white,
      borderRadius: BorderRadius.circular(30),
    ),
    child: FlatButton(
      onPressed: () {
        return showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              // Retrieve the text the user has entered by using the
              // TextEditingController.
              content: Text(InputEmailState.getUsername),
            );
          },
        );

      },
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            'Sign in',
            style: TextStyle(
              color: Colors.lightBlueAccent,
              fontSize: 14,
              fontWeight: FontWeight.w700,
            ),
          ),
          Icon(
            Icons.arrow_forward,
            color: Colors.lightBlueAccent,
          ),
        ],
      ),
    ),
  ),
);
小部件构建(构建上下文){
返回填充(
填充:仅限常量边集(顶部:40,右侧:50,左侧:200),
子:容器(
对齐:对齐。右下角,
身高:50,
宽度:MediaQuery.of(context).size.width,
装饰:盒子装饰(
boxShadow:[
箱形阴影(
颜色:颜色。蓝色[300],
blurRadius:10.0,//具有软化阴影的效果
spreadRadius:1.0,//具有延伸阴影的效果
偏移量:偏移量(
5.0,//水平,向右移动10
5.0,//垂直,向下移动10
),
),
],
颜色:颜色,白色,
边界半径:边界半径。圆形(30),
),
孩子:扁平按钮(
已按下:(){
返回显示对话框(
上下文:上下文,
生成器:(上下文){
返回警报对话框(
//通过使用检索用户输入的文本
//文本编辑控制器。
内容:文本(InputEmailState.getUsername),
);
},
);
},
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“登录”,
样式:TextStyle(
颜色:Colors.lightBlueAccent,
尺寸:14,
fontWeight:fontWeight.w700,
),
),
图标(
Icons.arrow_向前,
颜色:Colors.lightBlueAccent,
),
],
),
),
),
);
这是输入代码:

class InputEmailState extends State<InputEmail> {

  final myController = new TextEditingController();

  getUsername() {
    return(myController.text);
  }
  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    myController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 50, left: 50, right: 50),
      child: Container(
        height: 60,
        width: MediaQuery.of(context).size.width,
        child: TextField(
          controller: myController,
          style: TextStyle(
            color: Colors.white,
          ),
          decoration: InputDecoration(
            border: InputBorder.none,
            fillColor: Colors.lightBlueAccent,
            labelText: 'Student ID',
            labelStyle: TextStyle(
              color: Colors.white70,
              fontSize: 20,
            ),
          ),
        ),
      ),
    );
  }
类InputEmailState扩展状态{
final myController=新文本编辑控制器();
getUsername(){
返回(myController.text);
}
@凌驾
无效处置(){
//处置小部件时清理控制器。
myController.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回填充(
填充:仅限常量边集(顶部:50,左侧:50,右侧:50),
子:容器(
身高:60,
宽度:MediaQuery.of(context).size.width,
孩子:TextField(
控制器:myController,
样式:TextStyle(
颜色:颜色,白色,
),
装饰:输入装饰(
边框:InputBorder.none,
fillColor:Colors.lightBlueAccent,
labelText:“学生ID”,
标签样式:文本样式(
颜色:颜色。白色70,
尺寸:20,
),
),
),
),
);
}

如果我正确理解了您的问题,您正在尝试从另一个类/文件访问
\u按钮登录状态
。但是,在Dart中,以下划线(“\u”)开头的类、成员、变量等是。您无法从其他文件访问它们(除非在库的某些特殊情况下)

要解决这个问题,您可以将类的名称更改为
ButtonLoginState
,它应该可以工作

编辑:响应更多信息:

您似乎还没有完全理解StatefulWidget中的状态概念。我强烈建议您仔细阅读有关该主题的文章

有许多不同的管理状态的方法,我将要解释的在大多数情况下几乎肯定不是最好的选择(这是前段时间的介绍性方法,但我甚至找不到示例),但是,它确实有效。对于更一般的选择,我推荐

在您的例子中,问题是从以下内容开始的:
Text(InputEmailState.getUsername)
。您没有调用
InputEmailState.getUsername
,您只是传递了对它的引用。您需要包括圆括号来实际调用它-
InputEmailState.getUsername()

但是,这并不是问题的全部。您试图使用类的名称访问此函数,这意味着您试图将其用作。但是,它是一个实例方法(即:您需要类的特定实例来访问它。这就是我所说的状态)

要简单地访问特定小部件的状态对象,可以使用键(通常是GlobalKey)。例如,您可以在父小部件中定义它,并将其作为
InputEmail
小部件的
参数传递,并在父类中保留一个引用。然后,要获取用户名,您可以调用
.currentState.getUsername()
它将返回实例值。具体的实现方式各不相同,我没有您的代码知道应该如何实现它


正如我所说,这不再是真正推荐的方法。我强烈建议大家掌握状态的概念,那么最好的方法应该是显而易见的。

我们可以看到
\u ButtonLoginState
以及您试图访问控制器的地方吗?为什么不在
\u ButtonLoginState
中使用控制器se为您的问题添加更多代码。例如,您的错误指的是一个_button登录状态,但您从未提及过。当然。按钮登录状态正是我的登录按钮所在的类。
import'package:flatter/material.dart';import'pack
class InputEmailState extends State<InputEmail> {

  final myController = new TextEditingController();

  getUsername() {
    return(myController.text);
  }
  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    myController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 50, left: 50, right: 50),
      child: Container(
        height: 60,
        width: MediaQuery.of(context).size.width,
        child: TextField(
          controller: myController,
          style: TextStyle(
            color: Colors.white,
          ),
          decoration: InputDecoration(
            border: InputBorder.none,
            fillColor: Colors.lightBlueAccent,
            labelText: 'Student ID',
            labelStyle: TextStyle(
              color: Colors.white70,
              fontSize: 20,
            ),
          ),
        ),
      ),
    );
  }