gpt4 book ai didi

flutter - flutter希望在其子级上调用父 View 以刷新主视图

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

我有一个有状态的小部件,其中有一个子按钮和一组有状态的容器(可见和不可见)。
我想尝试的是,当我调用特定按钮时,特定按钮将刷新所有布局,并通过将可见设置为可见来更改其布局 View ,而其他按钮则不可见。

像这样:

button1 = view1;
button2 = view2;
button3 = view3;

if (button1 is pressed){
view1 is visible}
else{
not visible}

在我的代码上,在我的第一个 View (登录按钮)上,我已设置为与main_page一样,如下所示:
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MainPage(change1: true,change2: false,change3: false,)),
);
},

now i was display my main_page view (with a child view that has visibility property).

这是我在main_page上的代码:
class MainPage extends StatefulWidget {

final bool change1 ;
final bool change2;
final bool change3 ;

const MainPage({Key key, this.change1,this.change2,this.change3}) : super(key: key);

@override
_MainPageState createState() => _MainPageState();

}

class _MainPageState extends State<MainPage> {

@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Container(
constraints: BoxConstraints.expand(
height: 280.0,
),
decoration: BoxDecoration(
image: new DecorationImage(
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(
Colors.blueAccent, BlendMode.colorBurn),
image: new ExactAssetImage("images/vector.jpg"),
),
),
child: Stack(
children: <Widget>[

Container(
alignment: Alignment.bottomCenter,
child: Row(
children: <Widget>[
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
print("i pressed Official Business");

MainPage(change1: true,change2: false,change3: false);
//TODO: here is my problem, when i call the main_page on its page,
// the value of change1, change2, and chaange3 is not updating
// so that icanot update my view .

});
},
child: Container(
height: 50.0,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: Text(
"Official Business",
style: TextStyle(
fontSize: 20.0, color: Colors.white),
),
),
),
),
),
),
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
print("i pressed file an OB");
MainPage(change1: false,change2: true,change3: false);

//TODO: here is my problem, when i call the main_page on its page,
// the value of change1, change2, and chaange3 is not updating
// so that icanot update my view .

});
},
child: Container(
height: 50.0,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: Text(
"File an OB",
style: TextStyle(
fontSize: 20.0, color: Colors.white),
),
),
),
),
),
),
],
),
),

],
),
),
//TODO:
new Visibility(
//Called changed and viewOne
visible: widget.change1,
child: OfficialBusiness(),
),
new Visibility(
//Called not changed and viewTwo
visible: widget.change2,
child: FilingOb(),
),
new Visibility(
//Called not changed and viewTwo
visible: widget.change3,
child: ViewOfficialBusiness(),
)


],
),
),
);

}
}

包括的fillob / officialbusiness / ViewOfficialBusiness是有状态布局的集合,我没有添加代码来防止 View 过度。

抱歉,以这种编程语言的新知识,我想启发我遇到可能遇到的这些问题,如果我的代码是否可行,还需要引用更多内容,请仅在注释上注明,以便我可以提供其他代码

最佳答案

  bool change1;
bool change2;
bool change3;

@override
initState() {
super.initState();
change1 = widget.change1;
change2 = widget.change2;
change3 = widget.change3;
}

void setChange1() {
setState(() {
change1 = true;
change2 = change3 = false;
});
}

// GestureDetector(onTap: setChange1)

// Visibility(
// visible: change1,
// child: OfficialBusiness(),
// )


或使用enum:
enum MyView {
officialBusiness,
filingOb,
viewOfficialBusiness,
}
MyView current;

// GestureDetector(onTap: () => setState(() { current = MyView.officialBusiness; }))

// Visibility(
// visible: current == MyView.officialBusiness,
// child: OfficialBusiness(),
// )

关于flutter - flutter希望在其子级上调用父 View 以刷新主视图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60314237/

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