如何在 flutter 的 ExpandablePanel 中使用 SetState

How To Use SetState In ExpandablePanel in flutter

提问人:Bavli Badry 提问时间:3/5/2023 更新时间:3/5/2023 访问量:74

问:

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 不起作用

Android Flutter Dart SetState 可扩展

评论


答:

0赞 Wodota ML 3/5/2023 #1

很抱歉问,但是,为什么您需要带有容器类型的变量? 因此,有一个错误,即您在 build 方法中声明变量,当您使用 setstate 时,build 方法是 rebuild,并且您的变量保持不变。因为它们也在 build 方法中重新声明。尝试从构建方法中声明变量。 例:

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';

class TestPage extends StatefulWidget {
const TestPage({super.key});

@override
State<TestPage> createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {

@override
void initState() {
container = Container(color: Colors.black,);
super.initState();
}
Container? _Scaffold_Syllabus=Container(); //Declare variables
here
@override
Widget build(BuildContext context) {
return Scaffold(
  body: Container(
    color: Colors.amber,
    child: Column(
      children: [
        Expanded(child: container!),//this container is changed when 
button is pressed
        ElevatedButton(onPressed: (){
          container = Container(color: Colors.red,);
          setState(() {});
        }, child: Text("red Container")),
        ElevatedButton(onPressed: (){
          container = Container(color: Colors.blue,);
          setState(() {});
        }, child: Text("blue Container")),
      ],
    ),
  ),
);
}
}

评论

0赞 Bavli Badry 3/5/2023
在Table_Lectures_Scaffold中,我将 StatefulWidget 放入容器中,然后当我将其放入容器时Scaffold_Syllabus容器应该会发生变化,但它不起作用
0赞 Bavli Badry 3/5/2023
我需要它将容器从 bage 更改为另一个
0赞 Wodota ML 3/5/2023
好的,我明白了,我的朋友我是这么说的,每次使用方法时都会重新构建,所以你需要在构建方法之外或方法的顶部声明变量。看看我声明变量的答案。你的问题只是你在构建方法中声明变量,这是错误的。buildsetstate
0赞 Wodota ML 3/5/2023
这是另一个例子:class _TestPage extends State<TestPage>{ Container _Scaffold_Syllabus=Container() //Declare variables here @override build(BuildContext context){ return Scaffold(); } }
0赞 krishnaacharyaa 3/5/2023 #2

这主要是因为错误的逻辑,即

您已经建立了一个在代码中命名的类型变量,并为其指定了一个空的 .按下按钮后,将其值设置为 。但是,更改其值不会对用户界面产生影响,因为您并没有真正利用代码中的任何位置。Container_Scaffold_SyllabusContainerTable_Lectures_Scaffold_Scaffold_Syllabus

您必须更换 for 中的小部件才能实现您的目标。根据按下的按钮,还应修改 to 或 的值。ContainerSafeArea_Scaffold Syllabus_Scaffold_SyllabusScaffold_Syllabus1Scaffold_Syllabus2