首页 > 解决方案 > 函数中的 setState 不起作用 | 扑

问题描述

我正在执行此_optionSelected功能(第 63 行),但它不起作用。我尝试更改很多东西,但每次进入小部件页面时,它只会在我的文本中保留索引 0 和白色。它不遵循setState()功能。图片在这里:

在此处输入图像描述

代码在这里:

import 'package:robotics_community_app/utils/card_image_list.dart';

class Components extends StatefulWidget {
  const Components({Key key}) : super(key: key);

  @override
  _ComponentsState createState() => _ComponentsState();
}

class _ComponentsState extends State<Components> {
  @override
  Widget build(BuildContext context) {
    return PageView(
      physics: BouncingScrollPhysics(),
      children: [
        SingleComponent('Fotorresistor', 'assets/images/fotor1.png',
            'assets/images/fotor2.png', 'assets/images/fotor3.png', '', ''),
        SingleComponent('Potenciómetro', '', '', '', '', ''),
        Testing(Colors.blue),
        Testing(Colors.green),
      ],
    );
  }
}

class Testing extends StatelessWidget {
  final Color color;
  const Testing(this.color);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: this.color,
    );
  }
}



class SingleComponent extends StatefulWidget {
  final String name;
  final String image1;
  final String image2;
  final String image3;
  final String description;
  final String utilities;
  
  int current_section = 0;
  Color info_color = Color(0xffBDE3BE);
  Color videos_color = Color(0xffBDE3BE);
  Color preguntas_color = Color(0xffBDE3BE);

  SingleComponent(this.name, this.image1, this.image2, this.image3,
      this.description, this.utilities);

  @override
  _SingleComponentState createState() => _SingleComponentState();
}

class _SingleComponentState extends State<SingleComponent> {

  _optionSelected(int index) {
    setState(() {
      widget.current_section = index;
      widget.videos_color = Color(0xffBDE3BE);
      widget.preguntas_color = Color(0xffBDE3BE);
      widget.info_color = Color(0xffBDE3BE);
      if (index == 0) {
        debugPrint("Hola");
        // info_color = Color(0xff4AB14E);
        widget.info_color = Colors.white;
      }
      else if (index == 1) {
        debugPrint("Hola2");
        widget.videos_color = Color(0xff4AB14E);
      }else if (index == 2) {
        debugPrint("Hola3");
        widget.preguntas_color = Color(0xff4AB14E);
      }
    });

  }

  @override
  Widget build(BuildContext context) {
    return 
      Scaffold(
      //backgroundColor: Color(0xff333333),
      body: Column(
        children: [
          Container(
            child: Text(
              widget.name,
              style: TextStyle(
                  color: Color(0xff4AB14E),
                  fontSize: 26,
                  fontWeight: FontWeight.bold),
            ),
            alignment: Alignment.topCenter,
            margin: EdgeInsets.only(top: 50),
          ),
          CardImageList(widget.image1, widget.image2, widget.image3),
          Padding(
      padding: const EdgeInsets.only(left:28.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          FlatButton(onPressed: _optionSelected(0), child: Text('Información', style: TextStyle(fontSize: 17, color: widget.info_color),)),
          FlatButton(onPressed: _optionSelected(1), child: Text('Videos', style: TextStyle(fontSize: 17, color: widget.videos_color),)),
          FlatButton(onPressed: _optionSelected(2), child: Text('Preguntas', style: TextStyle(fontSize: 17, color: widget.preguntas_color),)),
        ],
      ),
    ),


        ],
      ),
    );
    
  }
}

如果有人可以帮助我,那就太棒了,谢谢:D

标签: androidflutterdart

解决方案


_optionSelected()以错误的方式调用方法。

而不是这个:onPressed: _optionSelected(0)

像这样调用:onPressed: () => _optionSelected(0)


推荐阅读