如何从其他小部件访问变量
how to access variables from other widgets
你好我想知道如何从其他小部件访问变量。例如:我希望当我触摸小部件 BotonLogin 的按钮时,它会打印小部件 CamposUsuario 的用户变量。
class CamposUsuario extends StatelessWidget {
final String texto;
CamposUsuario({required this.texto});
final usuario = TextEditingController();
@override
Widget build(BuildContext context) {
return TextFormField(
decoration: InputDecoration(
labelStyle: TextStyle(fontSize: 20), labelText: "$texto"),
obscureText: (this.texto == "Password") ? true : false,
controller: usuario,
);
}
}
class BotonLogin extends StatelessWidget {
const BotonLogin({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return RaisedButton(
padding: EdgeInsets.symmetric(horizontal: 60),
elevation: 7,
onPressed: () {
print('si');
},
color: Colors.amber[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: Text("Login"),
);
}
}
如果你想解决你的问题,那么你需要状态管理。
查看 office 文档以获取状态管理方法列表
https://flutter.dev/docs/development/data-and-backend/state-mgmt/options
您只需将 onPressed
回调传递给您的 BotonLogin
并将 controller
传递给您的 CamposUsuario
。然后父控件将使用传递的 TextEditingController
来获取 CamposeUsuario
.
的值
示例...
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final TextEditingController usernameController = TextEditingController();
// UPDATE
final TextEditingController passwordController = TextEditingController();
return Scaffold(
body: Column(
children: <Widget>[
CamposUsuario(
texto: 'Username',
usuario: usernameController,
),
// UPDATE
CamposUsuario(
texto: 'Password',
usuario: passwordController,
),
BotonLogin(
onPressed: () {
print('Si ${usernameController.text}');
// UPDATE
print('Password: ${passwordController.text}');
},
),
],
),
);
}
}
class CamposUsuario extends StatelessWidget {
CamposUsuario({
required this.texto,
required this.usuario,
Key? key,
}) : super(key: key);
final String texto;
final TextEditingController usuario;
@override
Widget build(BuildContext context) {
return TextFormField(
decoration: InputDecoration(
labelStyle: const TextStyle(fontSize: 20),
labelText: texto,
),
obscureText: (this.texto == "Password") ? true : false,
controller: usuario,
);
}
}
class BotonLogin extends StatelessWidget {
BotonLogin({
required this.onPressed,
Key? key,
}) : super(key: key);
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
child: Text("Login"),
);
}
}
你好我想知道如何从其他小部件访问变量。例如:我希望当我触摸小部件 BotonLogin 的按钮时,它会打印小部件 CamposUsuario 的用户变量。
class CamposUsuario extends StatelessWidget {
final String texto;
CamposUsuario({required this.texto});
final usuario = TextEditingController();
@override
Widget build(BuildContext context) {
return TextFormField(
decoration: InputDecoration(
labelStyle: TextStyle(fontSize: 20), labelText: "$texto"),
obscureText: (this.texto == "Password") ? true : false,
controller: usuario,
);
}
}
class BotonLogin extends StatelessWidget {
const BotonLogin({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return RaisedButton(
padding: EdgeInsets.symmetric(horizontal: 60),
elevation: 7,
onPressed: () {
print('si');
},
color: Colors.amber[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: Text("Login"),
);
}
}
如果你想解决你的问题,那么你需要状态管理。
查看 office 文档以获取状态管理方法列表
https://flutter.dev/docs/development/data-and-backend/state-mgmt/options
您只需将 onPressed
回调传递给您的 BotonLogin
并将 controller
传递给您的 CamposUsuario
。然后父控件将使用传递的 TextEditingController
来获取 CamposeUsuario
.
示例...
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final TextEditingController usernameController = TextEditingController();
// UPDATE
final TextEditingController passwordController = TextEditingController();
return Scaffold(
body: Column(
children: <Widget>[
CamposUsuario(
texto: 'Username',
usuario: usernameController,
),
// UPDATE
CamposUsuario(
texto: 'Password',
usuario: passwordController,
),
BotonLogin(
onPressed: () {
print('Si ${usernameController.text}');
// UPDATE
print('Password: ${passwordController.text}');
},
),
],
),
);
}
}
class CamposUsuario extends StatelessWidget {
CamposUsuario({
required this.texto,
required this.usuario,
Key? key,
}) : super(key: key);
final String texto;
final TextEditingController usuario;
@override
Widget build(BuildContext context) {
return TextFormField(
decoration: InputDecoration(
labelStyle: const TextStyle(fontSize: 20),
labelText: texto,
),
obscureText: (this.texto == "Password") ? true : false,
controller: usuario,
);
}
}
class BotonLogin extends StatelessWidget {
BotonLogin({
required this.onPressed,
Key? key,
}) : super(key: key);
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
child: Text("Login"),
);
}
}