在ExpandedPanel中,我在按下第一个更改容器常量时放置了两个按钮,但setState()无法将Scaffold_* Syllabus****的值更改为Table_Lecture*****_Scaffold**的值
该守则
class Syllabus extends StatefulWidget {
const Syllabus({super.key});
@override
State<Syllabus> createState() => _SyllabusState();
}
class _SyllabusState extends State<Syllabus> {
@override
Widget build(BuildContext context) {
Size? _size = MediaQuery.of(context).size;
Container _Scaffold_Syllabus = Container();
final Color color1 = HexColor('#3E6BA9');
return SingleChildScrollView(
child: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ExpansionPanelList(
children: [
ExpansionPanel(
headerBuilder: (context, isExpanded) {
return Text("dc");
},
body: SingleChildScrollView(
child: Container(
padding: _size.width > 450
? EdgeInsets.all(40)
: EdgeInsets.only(top: 40),
decoration: BoxDecoration(
border: Border.all(width: 3, color: color1)),
child: Column(
children: [
Row(
children: [
Expanded(
child: Center(
child: ElevatedButton(
onPressed: () {
// the next code want change the container contant
setState(() {
_Scaffold_Syllabus =
Table_Lectures_Scaffold;
});
},
child: new Text("ترم أول",
style: TextStyle(
fontSize: _size.width <= 435
? 9
: 15)),
style: ElevatedButton.styleFrom(
foregroundColor: _Scaffold_Syllabus ==
Table_Lectures_Scaffold
? color1
: Colors.white,
backgroundColor: _Scaffold_Syllabus ==
Table_Lectures_Scaffold
? Colors.white
: color1,
),
),
),
),
Expanded(
child: Center(
child: ElevatedButton(
onPressed: () {
setState(() {
_Scaffold_Syllabus =
Table_Lectures_Scaffold;
});
},
child: new Text("ترم ثان",
style: TextStyle(
fontSize: _size.width <= 435
? 9
: 15)),
style: ElevatedButton.styleFrom(
foregroundColor: _Scaffold_Syllabus ==
Table_Lectures_Scaffold
? color1
: Colors.white,
backgroundColor: _Scaffold_Syllabus ==
Table_Lectures_Scaffold
? Colors.white
: color1,
),
),
),
),
],
),
SizedBox(
height: 10,
),
SafeArea(
child: Container(
width: 500,
height: 500,
padding: _size.width > 450
? EdgeInsets.all(40)
: EdgeInsets.only(top: 40),
decoration: BoxDecoration(
border:
Border.all(width: 3, color: color1)),
child: _Scaffold_Syllabus,
),
)
],
),
),
))
],
),
ExpandablePanel(
header: Text("منهج السنه الدراسية الرابعة"),
collapsed: Text(""),
expanded: SingleChildScrollView(
child: Container(
padding: _size.width > 450
? EdgeInsets.all(40)
: EdgeInsets.only(top: 40),
decoration: BoxDecoration(
border: Border.all(width: 3, color: color1)),
child: Column(
children: [
Row(
children: [
Expanded(
child: Center(
child: ElevatedButton(
onPressed: () {
setState(() {
_Scaffold_Syllabus =
Table_Lectures_Scaffold;
});
},
child: new Text("ترم أول",
style: TextStyle(
fontSize:
_size.width <= 435 ? 9 : 15)),
style: ElevatedButton.styleFrom(
foregroundColor: _Scaffold_Syllabus ==
Table_Lectures_Scaffold
? color1
: Colors.white,
backgroundColor: _Scaffold_Syllabus ==
Table_Lectures_Scaffold
? Colors.white
: color1,
),
),
),
),
Expanded(
child: Center(
child: ElevatedButton(
onPressed: () {
setState(() {
_Scaffold_Syllabus =
Table_Lectures_Scaffold;
});
},
child: new Text("ترم ثان",
style: TextStyle(
fontSize:
_size.width <= 435 ? 9 : 15)),
style: ElevatedButton.styleFrom(
foregroundColor: _Scaffold_Syllabus ==
Table_Lectures_Scaffold
? color1
: Colors.white,
backgroundColor: _Scaffold_Syllabus ==
Table_Lectures_Scaffold
? Colors.white
: color1,
),
),
),
),
],
),
SizedBox(
height: 10,
),
SafeArea(
child: Container(
width: 500,
height: 500,
padding: _size.width > 450
? EdgeInsets.all(40)
: EdgeInsets.only(top: 40),
decoration: BoxDecoration(
border: Border.all(width: 3, color: color1)),
child: _Scaffold_Syllabus,
),
)
],
),
),
))
],
),
),
),
);
}
}
当我按下按钮时,我尝试更改容器的常量,但setstate不起作用
2条答案
按热度按时间qlvxas9a1#
这主要是因为错误的逻辑,即
您已经在代码中建立了一个名为
_Scaffold_Syllabus
的Container
类型变量,并赋予它一个空的Container
。当按下按钮时,您将其值设置为Table_Lectures_Scaffold
。但是,更改其值不会对用户界面产生任何影响,因为您实际上并没有在代码中的任何地方使用_Scaffold_Syllabus
。您必须将
SafeArea
中的Container
小工具替换为_Scaffold Syllabus
才能实现您的目标。根据按下的按钮,您还应该将_Scaffold_Syllabus
的值修改为Scaffold_Syllabus1
或Scaffold_Syllabus2
。xmakbtuz2#
我很抱歉问,但是,为什么你需要容器类型的变量?所以有一个错误,你在构建方法中声明变量,当你使用setstate时,构建方法被重新构建,你的变量保持不变。因为它们也在构建方法中被重新声明。尝试在构建方法外声明变量。例如: