我正在尝试解决这个问题,它有一个小部件(main-widget),它有一个孩子(colum-widget)并构建按钮小部件列表。因为我正在使用许多有状态的小部件,我如何从另一个小部件调用主页(全局访问从另一个小部件调用)。
您可以运行我制作的这段代码并添加一些 TODO: 注释以供参考:)
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Main(param: 1),
);
}
}
class Main extends StatefulWidget {
final int param;
const Main({Key key,this.param}) : super(key: key);
@override
_MainState createState() => _MainState();
}
class _MainState extends State<Main> {
bool change1;
bool change2;
bool change3;
@override
initState() {
super.initState();
if (widget.param == 1) {
setChange1();
}
else if (widget.param == 2) {
setChange2();
}
else if (widget.param == 3) {
setChange3();
}
}
void setChange1() {
setState(() {
change1 = true;
change2 = change3 = false;
});
}
void setChange2() {
setState(() {
change1 = change3 = false;
change2 = true;
});
}
void setChange3() {
setState(() {
change1 = change2 = false;
change3 = true;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Container(
color: Colors.grey,
constraints: BoxConstraints.expand(
height: 60.0,
),
child: Stack(
children: <Widget>[
Container(
alignment: Alignment.bottomCenter,
child: Row(
children: <Widget>[
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
setChange1();
build(context);
});
},
child: Container(
height: 50.0,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: Text(
"Button1",
style: TextStyle(
fontSize: 20.0, color: Colors.white),
),
),
),
),
),
),
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
setChange2();
build(context);
});
},
child: Container(
height: 50.0,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: Text(
"Button2",
style: TextStyle(
fontSize: 20.0, color: Colors.white),
),
),
),
),
),
),
],
),
),
],
),
),
//TODO:
new Visibility(
//Called changed and viewOne
visible: change1,
child: view1(),
),
new Visibility(
//Called not changed and viewTwo
visible: change2,
child: view2(),
),
new Visibility(
//Called not changed and viewTwo
visible: change3,
child: view3(),
),
],
),
),
);
}
}
class view1 extends StatefulWidget {
@override
_view1State createState() => _view1State();
}
class _view1State extends State<view1> {
@override
Widget build(BuildContext context) {
return Container(
height: 600.0,
color: Colors.blue,
);
}
}
class view2 extends StatefulWidget {
@override
_view2State createState() => _view2State();
}
class _view2State extends State<view2> {
@override
Widget build(BuildContext context) {
return Container(
height: 600.0,
color: Colors.orange,
child: Column(
children: <Widget>[
ListWithButton(),
],
),
);
}
}
class view3 extends StatefulWidget {
@override
_view3State createState() => _view3State();
}
class _view3State extends State<view3> {
@override
Widget build(BuildContext context) {
return Container(
height: 600.0,
color: Colors.green,
);
}
}
class ListWithButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 3.0,horizontal: 5.0),
child: GestureDetector(
onTap: (){
//TODO: this will trigger the view_1 upon Main page
Main(param: 1).createState();
build(context);
//TODO: that was id like to try.
},
child: Container(
height: 50.0,
color: Colors.red,
child:Center(
child: Text(
"list_Button1",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 3.0,horizontal: 5.0),
child: GestureDetector(
onTap: (){
//TODO: this will trigger the view_3 upon Main page
Main(param: 3).createState();
build(context);
//TODO: that was id like to try.
},
child: Container(
height: 50.0,
color: Colors.red,
child:Center(
child: Text(
"list_Button2",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
),
),
),
],
);
}
}