gpt4 book ai didi

flutter - 将字符串值从一个有状态小部件更改为另一个有状态小部件

转载 作者:行者123 更新时间:2023-12-03 04:39:29 25 4
gpt4 key购买 nike

我有一个有状态的小部件:

class PractiseInterview extends StatefulWidget {
@override
_PractiseInterviewState createState() => _PractiseInterviewState();
}

class _PractiseInterviewState extends State<PractiseInterview> {
String outputText = '0';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Practise'),
),
body: Container(
child: Column(
children: <Widget>[
Text(
outputText, //**outputText is here**
),
Expanded(
child: Divider(),
),
Column(
children: <Widget>[
Row(
children: <Widget>[
ChnageText(text: 'changed',),
],
),
],
)
],
),
),
);
}
}
注意我的 outputText = '0'在这里。
然后我在有状态小部件中为我的OutlineButton提供了一个无状态小部件:
class ChnageText extends StatelessWidget {
const ChnageText({ this.text});
final String text;

buttonPressed(String text) {
if (text == 'changed') {
//TODO: change outputText value to 'changed' at the click of this button
}
}

@override
Widget build(BuildContext context) {
return Expanded(
child: OutlineButton(
onPressed: () {
buttonPressed(text);
},
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Text(
text,
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
),
),
);
}
}
我在我的无状态窗口小部件中提到了TODO,在单击按钮时,我想将 outputText = '0'更改为 outputText = 'changed'。我不知道该怎么做
我是新手,我无法理解这一点。基本上,我是在做一个计算器,单击该按钮时应显示该按钮的值。

最佳答案

从父级传递回ChnageText的回调,以更改outputText并调用setState

class ChnageText extends StatelessWidget {
const ChnageText({this.text, this.callback});
final String text;
final VoidCallback callback;

buttonPressed(String text) {
if (text == 'changed') {
callback();//Call callback here
}
}

@override
Widget build(BuildContext context) {
return Expanded(
child: OutlineButton(
onPressed: () {
buttonPressed(text);
},
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Text(
text,
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
),
),
);
}
}

class _PractiseInterviewState extends State<PractiseInterview> {
String outputText = '0';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Practise'),
),
body: Container(
child: Column(
children: <Widget>[
Text(
outputText, //**outputText is here**
),
Expanded(
child: Divider(),
),
Column(
children: <Widget>[
Row(
children: <Widget>[
ChnageText(text: 'changed', callback: () {setState((){outputText = 'changed';});}),// Pass callback here
],
),
],
)
],
),
),
);
}
}
理想情况下,您根本不需要这样做。 ChnageText中的内容不需要在自己的 StatelessWidget中。将所有这些内容直接放在父项中可以解决此问题。
class _PractiseInterviewState extends State<PractiseInterview> {
String outputText = '0';

buttonPressed(String text) {
if (text == 'changed') {
setState(() {
outputText = 'changed';
});
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Practise'),
),
body: Container(
child: Column(
children: <Widget>[
Text(
outputText, //**outputText is here**
),
Expanded(
child: Divider(),
),
Column(
children: <Widget>[
Row(
children: <Widget>[
//Put ChnageText directly into the build of the parent
Expanded(
child: OutlineButton(
onPressed: () {
buttonPressed('changed');
},
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Text(
'changed',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
],
),
],
)
],
),
),
);
}
}

关于flutter - 将字符串值从一个有状态小部件更改为另一个有状态小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63494663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com