【问题标题】:Access the widget class from the List of Widgets从小部件列表访问小部件类
【发布时间】:2020-05-04 05:45:41
【问题描述】:

我制作了一个小部件列表,我想访问 DayButton 类中的 setter,但是当我尝试 days[index]. 时,它不显示 setter 方法,它会将输出视为一般小部件而不是 @ 987654323@小部件

这里是代码。

class DayButtonRow extends StatefulWidget{

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

class DBRState extends State<DayButtonRow>{

  currentDay(int i){
    setState(() {
      current = days.elementAt(i).key;
      //I want to access the setter here to change the color of the button
    });
  }

  @override
  Widget build(BuildContext context) {
    currentDay(3);
    return Padding(
      padding: const EdgeInsets.only(top: 8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: days
      ),
    );
  }
}

Key current;
List<Widget> days = [
  DayButton("MON"),
  DayButton("TUE"),
  DayButton("WED"),
  DayButton("THU"),
  DayButton("FRI"),
  DayButton("SAT"),
  DayButton("SUN"),
];

有没有办法从列表中访问DayButton 小部件类?

【问题讨论】:

  • 声明天数为List&lt;DayButton&gt;而不是List&lt;Widget&gt;?
  • 非常感谢你太傻了..

标签: list flutter widget


【解决方案1】:

如cmets中所述,将其声明为List&lt;DayButton&gt;:

List<DayButton> days = [
  DayButton("MON"),
  DayButton("TUE"),
  DayButton("WED"),
  DayButton("THU"),
  DayButton("FRI"),
  DayButton("SAT"),
  DayButton("SUN"),
];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多