【发布时间】:2020-08-25 15:03:28
【问题描述】:
感谢您在前面的回答。我正在尝试将 GetTextField StatelessWidget 中的 TextField 小部件中的“用户名、电子邮件、密码”传递给 GetSignup StatelessWidget 中的 GestureDetector 小部件中的打印功能。我尝试了两种方法,但在填写字段并按下按钮后,都会出现“用户名:null,用户电子邮件:null,用户密码:null”消息。可能有什么问题?
方法一:
class SignupScreen extends StatefulWidget {
static const id = "signup_screen";
@override
_SignupScreenState createState() => _SignupScreenState();
}
class _SignupScreenState extends State<SignupScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomPaint(
painter: BackgroundSignup(),
child: Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(
horizontal: 35,
),
child: Column(
children: <Widget>[
GetHeader(),
GetTextField(),
GetSignup(),
GetButtonRow(),
],
),
),
GetBackButton(),
],
),
),
);
}
}
class GetTextField extends StatelessWidget {
String email;
String password;
String username;
@override
Widget build(BuildContext context) {
return Expanded(
flex: 4,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
SizedBox(
height: 15,
),
Directionality(
textDirection: TextDirection.rtl,
child: TextField(
onChanged: (value) {
username = value;
},
textAlign: TextAlign.start,
decoration: InputDecoration(
labelText: "الاسم كاملا",
labelStyle:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
),
),
SizedBox(
height: 15,
),
Directionality(
textDirection: TextDirection.rtl,
child: TextField(
onChanged: (value) {
email = value;
},
textAlign: TextAlign.start,
decoration: InputDecoration(
labelText: "الإيميل",
labelStyle:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
),
),
SizedBox(
height: 15,
),
Directionality(
textDirection: TextDirection.rtl,
child: TextField(
onChanged: (value) {
password = value;
},
obscureText: true,
textAlign: TextAlign.start,
decoration: InputDecoration(
labelText: "كلمة السر",
labelStyle:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
),
),
SizedBox(
height: 25,
),
],
),
);
}
}
class GetSignup extends StatelessWidget {
GetTextField getTextField = GetTextField();
@override
Widget build(BuildContext context) {
return Expanded(
flex: 1,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
GestureDetector(
onTap: () {
print('user name: ${getTextField.username}');
print('user email: {getTextField.email}');
print('user password: ${getTextField.password}');
},
child: CircleAvatar(
backgroundColor: Colors.grey.shade800,
radius: 40,
child: Icon(
Icons.arrow_back,
color: Colors.white,
),
),
),
Text(
"انشئ حسابك",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
],
),
);
}
}
方法二:
class SignupScreen extends StatefulWidget {
static const id = "signup_screen";
@override
_SignupScreenState createState() => _SignupScreenState();
}
class _SignupScreenState extends State<SignupScreen> {
GetTextField getTextField = GetTextField();
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomPaint(
painter: BackgroundSignup(),
child: Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(
horizontal: 35,
),
child: Column(
children: <Widget>[
GetHeader(),
GetTextField(),
GetSignup(
username: getTextField.username,
email: getTextField.email,
password: getTextField.password,
),
GetButtonRow(),
],
),
),
GetBackButton(),
],
),
),
);
}
}
class GetTextField extends StatelessWidget {
String email;
String password;
String username;
@override
Widget build(BuildContext context) {
return Expanded(
flex: 4,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
SizedBox(
height: 15,
),
Directionality(
textDirection: TextDirection.rtl,
child: TextField(
onChanged: (value) {
username = value;
},
textAlign: TextAlign.start,
decoration: InputDecoration(
labelText: "الاسم كاملا",
labelStyle:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
),
),
SizedBox(
height: 15,
),
Directionality(
textDirection: TextDirection.rtl,
child: TextField(
onChanged: (value) {
email = value;
},
textAlign: TextAlign.start,
decoration: InputDecoration(
labelText: "الإيميل",
labelStyle:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
),
),
SizedBox(
height: 15,
),
Directionality(
textDirection: TextDirection.rtl,
child: TextField(
onChanged: (value) {
password = value;
},
obscureText: true,
textAlign: TextAlign.start,
decoration: InputDecoration(
labelText: "كلمة السر",
labelStyle:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
),
),
SizedBox(
height: 25,
),
],
),
);
}
}
class GetSignup extends StatelessWidget {
final username;
final email;
final password;
GetSignup({this.username, this.email, this.password});
@override
Widget build(BuildContext context) {
return Expanded(
flex: 1,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
GestureDetector(
onTap: () {
print('user name: $username');
print('user email: $email');
print('user password: $password');
},
child: CircleAvatar(
backgroundColor: Colors.grey.shade800,
radius: 40,
child: Icon(
Icons.arrow_back,
color: Colors.white,
),
),
),
Text(
"انشئ حسابك",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
],
),
);
}
}
【问题讨论】:
-
你已经声明了 GetTextField getTextField = GetTextField();获得价值。但在 Column 中,您再次创建了 GetTextField()。在 Column 中使用 getTextField。
标签: flutter