Flutter中如何将一个变量的值return改成前面的class?
How to return the value of a variable to the previous class in Flutter?
我将 isSendEmail 布尔变量传递给另一个 AboutPageWidget 小部件,在这个小部件中我更改了这个变量的值,我希望它 return 到屏幕 1,如何做到这一点,如何获得值屏幕 1 上的 isSendEmail 变量?
屏幕 1
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
AboutPageWidget(isSendEmail: isSendEmail),
const SizedBox(
height: 42,
),
isSendEmail
? SetNewPasswordFields(
newPasswordCubit: newPasswordCubit,
)
: const EmailFieldWidget(),
],
),
屏幕 2
class AboutPageWidget extends StatefulWidget {
AboutPageWidget({
Key? key,
required this.isSendEmail,
}) : super(key: key);
bool isSendEmail;
@override
State<AboutPageWidget> createState() => _AboutPageWidgetState();
}
class _AboutPageWidgetState extends State<AboutPageWidget> {
@override
Widget build(BuildContext context) {
return Column(
children: [
Row(
children: [
Expanded(
flex: 1,
child: Align(
alignment: Alignment.centerLeft,
child: IconButton(
onPressed: () => widget.isSendEmail
? setState(
() => widget.isSendEmail = !widget.isSendEmail,
)
: _onBackPressed(context),
除非你希望你的代码真的很乱,否则使用简单的状态管理会好得多。 Provider应该能派上用场。
您必须在 screen2 中定义一个回调,然后在屏幕上收听它。
例如:
屏幕 1:
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
AboutPageWidget(
isSendEmail: isSendEmail,
isSendEmailChanged: (value) => setState(() => isSendEmail = value);
),
const SizedBox(
height: 42,
),
isSendEmail
? SetNewPasswordFields(
newPasswordCubit: newPasswordCubit,
)
: const EmailFieldWidget(),
],
),
屏幕 2:
class AboutPageWidget extends StatefulWidget {
AboutPageWidget({
Key? key,
required this.isSendEmail,
required this.isSendEmailChanged,
}) : super(key: key);
ValueChanged<bool> isSendEmailChanged;
bool isSendEmail;
@override
State<AboutPageWidget> createState() => _AboutPageWidgetState();
}
class _AboutPageWidgetState extends State<AboutPageWidget> {
@override
Widget build(BuildContext context) {
return Column(
children: [
Row(
children: [
Expanded(
flex: 1,
child: Align(
alignment: Alignment.centerLeft,
child: IconButton(
onPressed: () => widget.isSendEmail
? widget.isSendEmailChanged(!widget.isSendEmail)
: _onBackPressed(context),
然后在 _AboutPageWidgetState 中,每当您想发出 isSendEmail 已更改时,只需使用新值调用 widget.isSendEmailChanged
。
对于更复杂的状态,我建议使用 Provider 或其他一些状态管理解决方案。
Navigater 有一个 .then 方法,所以你可以这样做
Navigator.of(context).push(nextscreen).then(result=>{
// Result will contain the data you passed back
});
并在下一个屏幕上
Navigator.pop(variable);
我将 isSendEmail 布尔变量传递给另一个 AboutPageWidget 小部件,在这个小部件中我更改了这个变量的值,我希望它 return 到屏幕 1,如何做到这一点,如何获得值屏幕 1 上的 isSendEmail 变量?
屏幕 1
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
AboutPageWidget(isSendEmail: isSendEmail),
const SizedBox(
height: 42,
),
isSendEmail
? SetNewPasswordFields(
newPasswordCubit: newPasswordCubit,
)
: const EmailFieldWidget(),
],
),
屏幕 2
class AboutPageWidget extends StatefulWidget {
AboutPageWidget({
Key? key,
required this.isSendEmail,
}) : super(key: key);
bool isSendEmail;
@override
State<AboutPageWidget> createState() => _AboutPageWidgetState();
}
class _AboutPageWidgetState extends State<AboutPageWidget> {
@override
Widget build(BuildContext context) {
return Column(
children: [
Row(
children: [
Expanded(
flex: 1,
child: Align(
alignment: Alignment.centerLeft,
child: IconButton(
onPressed: () => widget.isSendEmail
? setState(
() => widget.isSendEmail = !widget.isSendEmail,
)
: _onBackPressed(context),
除非你希望你的代码真的很乱,否则使用简单的状态管理会好得多。 Provider应该能派上用场。
您必须在 screen2 中定义一个回调,然后在屏幕上收听它。
例如:
屏幕 1:
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
AboutPageWidget(
isSendEmail: isSendEmail,
isSendEmailChanged: (value) => setState(() => isSendEmail = value);
),
const SizedBox(
height: 42,
),
isSendEmail
? SetNewPasswordFields(
newPasswordCubit: newPasswordCubit,
)
: const EmailFieldWidget(),
],
),
屏幕 2:
class AboutPageWidget extends StatefulWidget {
AboutPageWidget({
Key? key,
required this.isSendEmail,
required this.isSendEmailChanged,
}) : super(key: key);
ValueChanged<bool> isSendEmailChanged;
bool isSendEmail;
@override
State<AboutPageWidget> createState() => _AboutPageWidgetState();
}
class _AboutPageWidgetState extends State<AboutPageWidget> {
@override
Widget build(BuildContext context) {
return Column(
children: [
Row(
children: [
Expanded(
flex: 1,
child: Align(
alignment: Alignment.centerLeft,
child: IconButton(
onPressed: () => widget.isSendEmail
? widget.isSendEmailChanged(!widget.isSendEmail)
: _onBackPressed(context),
然后在 _AboutPageWidgetState 中,每当您想发出 isSendEmail 已更改时,只需使用新值调用 widget.isSendEmailChanged
。
对于更复杂的状态,我建议使用 Provider 或其他一些状态管理解决方案。
Navigater 有一个 .then 方法,所以你可以这样做
Navigator.of(context).push(nextscreen).then(result=>{
// Result will contain the data you passed back
});
并在下一个屏幕上
Navigator.pop(variable);