android 如何在flutter中使用可扩展面板中的SetState

qxgroojn  于 2023-03-06  发布在  Android
关注(0)|答案(2)|浏览(153)

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

qlvxas9a

qlvxas9a1#

这主要是因为错误的逻辑,即
您已经在代码中建立了一个名为_Scaffold_SyllabusContainer类型变量,并赋予它一个空的Container。当按下按钮时,您将其值设置为Table_Lectures_Scaffold。但是,更改其值不会对用户界面产生任何影响,因为您实际上并没有在代码中的任何地方使用_Scaffold_Syllabus
您必须将SafeArea中的Container小工具替换为_Scaffold Syllabus才能实现您的目标。根据按下的按钮,您还应该将_Scaffold_Syllabus的值修改为Scaffold_Syllabus1Scaffold_Syllabus2

xmakbtuz

xmakbtuz2#

我很抱歉问,但是,为什么你需要容器类型的变量?所以有一个错误,你在构建方法中声明变量,当你使用setstate时,构建方法被重新构建,你的变量保持不变。因为它们也在构建方法中被重新声明。尝试在构建方法外声明变量。例如:

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")),
      ],
    ),
  ),
);
}
}

相关问题