2021年1月15日星期五

Not able to import variable from a different class in Flutter

I am going off of a login screen tempalte,a nd am trying to get a widget class for a button to just show the username input as an alert on the screen. The usernameinput widget is defined but when I import it, it does not work.

class _InputEmailState extends State<InputEmail> {       final myController = new TextEditingController();  

This is the part where I define the input, and this is where I import the class in the button widget:

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

When I try and reference the myController.text value, I get the error

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  

Here is the button widget code:

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

And here is the Input code:

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,              ),            ),          ),        ),      );    }  
https://stackoverflow.com/questions/65745541/not-able-to-import-variable-from-a-different-class-in-flutter January 16, 2021 at 09:09AM

没有评论:

发表评论