dart 为什么我的listview.builder在flutter中没有更新setstate

oknrviil  于 2023-04-27  发布在  Flutter
关注(0)|答案(1)|浏览(204)
class _ProjectDrawerState extends State<ProjectDrawer> {

@override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;
    bool showMore = false;
    print('ls Length --- ${widget.ls.length}');
    void toggleShowMore()
    {
      print('Inside toggleshowMore original value --- $showMore');
      setState(() {
        showMore = !showMore;
      });
      print('Inside toggleshowMOre updated value --- $showMore');
    }
    return Scaffold(
        body: SafeArea(
      child: Container(
        margin: EdgeInsets.only(
            left: 0.04 * width,
            right: 0.04 * width,
            top: 0.14 * height,
            bottom: 0.1 * height),
        decoration: BoxDecoration(
          color: const Color(0xffefefef),
          borderRadius: BorderRadius.circular(0.037 * width),
        ),
        child:  SingleChildScrollView(
              child: IntrinsicHeight(
                child: ResponsiveRowColumnItem(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Container(
                        height: /*showMore ? (widget.ls.length * height * 0.025) :*/ height * 0.25,
                                  child: ListView.builder(
                                    physics:
                                        const AlwaysScrollableScrollPhysics(),
                                    itemCount: showMore ? widget.ls.length : 4,
                                    itemBuilder:
                                        (BuildContext context, int index) {
                                      print('ShowMore value inside ListView.builder  --- $showMore');
                                      return SingleChildScrollView(
                                        child: ListTile(
                                          dense: true,
                                          title: Row(
                                            children: [
                                              SizedBox(
                                                width: width * 0.0255,
                                              ),
                                              Image.asset(
                                                  'assets/logo.png',
                                                  width: 24,
                                                  height: 24),
                                              Text(
                                                '        ${widget.ls[index]}',
                                                textAlign: TextAlign.center,
                                                style: const TextStyle(
                                                    color: Colors.black,
                                                    fontFamily: 'Roboto',
                                                    fontSize: 18,
                                                    letterSpacing: 0,
                                                    fontWeight:
                                                        FontWeight.normal,
                                                    height: 1),
                                              ),
                                            ],
                                          ),
                                        ),
                                      );
                                    },
                                  ),
                                ),
                      MaterialButton(
                        onPressed: toggleShowMore,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            SizedBox(
                              width: width * 0.22,
                              child: const Divider(
                                  color: Color(0xffbdbdbd),
                                  thickness: 1.5,
                                  height: 0.05),
                            ),
                            Text(
                              showMore ? 'See Less' : 'See more',
                              style: const TextStyle(
                                fontFamily: 'Roboto',
                                fontSize: 18,
                                fontWeight: FontWeight.w600,
                                color: Color(0xff000000),
                              ),
                            ),
                            Icon(
                              showMore ? Icons.arrow_drop_up_sharp : Icons.arrow_drop_down_sharp,
                              color: Colors.black,
                              size: 40,
                            ),
                            SizedBox(
                              width: width * 0.2,
                              child: const Divider(
                                  color: Color(0xffbdbdbd),
                                  thickness: 1.5,
                                  height: 0.05),
                        ),
                      ],
                    ),
                  ),

                 
                ],
            ),
          ),
        )),
      
    ),
  ));

{\fnSimHei\bord1\shad1\pos(200,288)}
我添加了print语句用于调试,在setState中showMore更新为true。但是为什么setstate不更新listview.builder中的showmore值为true,并打印list ls中的所有项目?它默认只打印4个项目,但点击setstate后没有发生任何变化,更多的元素没有按照预期呈现。

yqyhoc1h

yqyhoc1h1#

将变量和函数移到生成方法之外

bool showMore = false;
    print('ls Length --- ${widget.ls.length}');
    void toggleShowMore()
    {
      print('Inside toggleshowMore original value --- $showMore');
      setState(() {
        showMore = !showMore;
      });
      print('Inside toggleshowMOre updated value --- $showMore');
    }

@override
Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;
    return Scaffold(
   .....

通过调用setState,意味着我们重新执行这个方法:Widget build(BuildContext context)
如果你把你的变量放在里面,那么这个值就会被重置为你声明的初始值

相关问题