下面是大部分代码:
我已经尝试修改我如何通过变量,在我的其他项目一个简单的函数传递是足够的,但我必须初始化它不正确。如果我把它作为一个延迟的字符串密码,那么我收到一个错误,它从未初始化。我希望看到在控制台中打印密码,尽管现在它显示为空。' '
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
late String email;
String? password;
late String firstName;
late String lastName;
class RegistrationScreen extends StatefulWidget {
static const String id = 'registration_screen';
@override
State<RegistrationScreen> createState() => _RegistrationScreenState();
}
class _RegistrationScreenState extends State<RegistrationScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light,
child: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Stack(
children: [
Container(
height: double.infinity,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.symmetric(
horizontal: 40.0,
vertical: 120.0,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
children: [
SuildTextField(
'Password',
'Enter your password',
Icons.key_outlined,
true,
TextInputType.text,
(value) {
password = value;
},
),
SizedBox(height: 10.0),
_BuildCreateAccountButton(),
],
)
],
),
),
)
],
),
),
),
);
}
}
class _BuildCreateAccountButton extends StatefulWidget {
const _BuildCreateAccountButton();
@override
State<_BuildCreateAccountButton> createState() =>
_BuildCreateAccountButtonState();
}
class _BuildCreateAccountButtonState extends State<_BuildCreateAccountButton> {
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 25.0),
width: double.infinity,
child: ElevatedButton(
onPressed: () {
setState(() {
print(
'Create Account Button was pressed. print: $password'); //TODO Link registration_screen: Create Account button
});
},
child: Row(
children: const [
Material(
child: Text(
'SIGN UP ',
),
),
],
),
),
);
}
}
class SuildTextField extends StatelessWidget {
SuildTextField(this.titleLine, this.textField, this.fieldIcon, this.isSecure,
this.textInputType, this.userTyped,
{super.key});
final String titleLine;
final String textField;
final IconData fieldIcon;
final bool isSecure;
final TextInputType textInputType;
final Function userTyped;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(titleLine, style: kLabelStyle),
const SizedBox(height: 10.0),
Container(
child: TextField(
obscureText: isSecure,
keyboardType: textInputType,
onChanged: (value) => userTyped,
onSubmitted: (value) {},
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: const EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
fieldIcon,
color: Colors.white,
),
hintText: textField,
hintStyle: kHintTextStyle,
),
),
),
],
);
}
}`
应该可以:
onChanged:(value) => userTyped(value)