gpt4 book ai didi

flutter - 如何停止小部件以在Flutter的StatefulWidget类中的setState()方法上重新加载

转载 作者:行者123 更新时间:2023-12-03 02:40:57 25 4
gpt4 key购买 nike

我试图实现的是保存小部件的状态,即在调用setState()方法时不应刷新它的状态。

class _GenderSelectionPageState extends State<GenderSelectionPage> {
bool isFemaleSelected = false;
AdmobBannerSize bannerSize;
GlobalKey _globalKey = new GlobalKey();

bool isLoaded = false;

@override
void initState() {
// TODO: implement initState
super.initState();
bannerSize = AdmobBannerSize.BANNER;
}

@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;

return new Scaffold(
body: new Container(
width: width,
child: new Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Flexible(
child: new Hero(
tag: "gender",
child: Material(
child: new Row(
children: <Widget>[
InkWell(
onTap: () {
setState(() {
if (isFemaleSelected) {
isFemaleSelected = false;
} else {
isFemaleSelected = true;
}
});
},
child: Opacity(
child: Image.asset(
"assets/woman.png",
height: height / 4,
width: width / 2 - 12,
),
opacity: isFemaleSelected ? 1.0 : 0.30,
),
),
InkWell(
onTap: () {
setState(() {
if (isFemaleSelected) {
isFemaleSelected = false;
} else {
isFemaleSelected = true;
}
});
},
child: Opacity(
opacity: !isFemaleSelected ? 1.0 : 0.30,
child: Image.asset(
"assets/boy.png",
height: height / 4,
width: width / 2 - 12,
),
),
),
],
),
),
),
),
],
),
flex: 1,
),
InkWell(
onTap: () {
setState(() {

});
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext) =>
new HeightWeightSelection(isFemaleSelected
? "assets/woman.png"
: "assets/boy.png")));
},
child: Container(
margin: EdgeInsets.only(bottom: 12.0),
child: new Image.asset(
"assets/next.png",
height: 64.0,
width: 64.0,
),
)),
new AdmobBannerWrapper(adUnitId: getBannerAdUnitId(),adSize: bannerSize,key: _globalKey,),
/* new AdmobBanner(
adUnitId: getBannerAdUnitId(),
adSize: bannerSize,
listener:
(AdmobAdEvent event, Map<String, dynamic> args) {
handleEvent(event, args, 'Banner');
},
),*/
],
),
)),
);
}

我不想每次我按下底部的图像按钮时都调用AdmobBannerWrapper.AdmobBannerWrapper应该只加载一次,但问题是每当我单击Next Image每次都加载AdmobBannerWrapper方法时。

最佳答案

initState()构建它,然后在需要时使用它的引用,直到父窗口小部件重新初始化后,它才可以再次构建。

  var banner;

@override
void initState() {
// TODO: implement initState
super.initState();
bannerSize = AdmobBannerSize.BANNER;
banner = AdmobBannerWrapper(adUnitId: getBannerAdUnitId(),adSize: bannerSize,key:_globalKey,);
}

比在这里通过引用调用它 banner

@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;

return new Scaffold(
body: new Container(
width: width,
child: new Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Flexible(
child: new Hero(
tag: "gender",
child: Material(
child: new Row(
children: <Widget>[
InkWell(
onTap: () {
setState(() {
if (isFemaleSelected) {
isFemaleSelected = false;
} else {
isFemaleSelected = true;
}
});
},
child: Opacity(
child: Image.asset(
"assets/woman.png",
height: height / 4,
width: width / 2 - 12,
),
opacity: isFemaleSelected ? 1.0 : 0.30,
),
),
InkWell(
onTap: () {
setState(() {
if (isFemaleSelected) {
isFemaleSelected = false;
} else {
isFemaleSelected = true;
}
});
},
child: Opacity(
opacity: !isFemaleSelected ? 1.0 : 0.30,
child: Image.asset(
"assets/boy.png",
height: height / 4,
width: width / 2 - 12,
),
),
),
],
),
),
),
),
],
),
flex: 1,
),
InkWell(
onTap: () {
setState(() {

});
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext) =>
new HeightWeightSelection(isFemaleSelected
? "assets/woman.png"
: "assets/boy.png")));
},
child: Container(
margin: EdgeInsets.only(bottom: 12.0),
child: new Image.asset(
"assets/next.png",
height: 64.0,
width: 64.0,
),
)),
banner, /*this is our variable */
/* new AdmobBanner(
adUnitId: getBannerAdUnitId(),
adSize: bannerSize,
listener:
(AdmobAdEvent event, Map<String, dynamic> args) {
handleEvent(event, args, 'Banner');
},
),*/
],
),
)),
);
}

关于flutter - 如何停止小部件以在Flutter的StatefulWidget类中的setState()方法上重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60374255/

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