首页 > 解决方案 > 如何从列表访问上下文?

问题描述

我正在尝试显示时间选择器 onPress a raiseButton 但有此错误,我了解它的含义但无法修复它...

错误:''在初始化程序中只能访问静态成员。''

我有自己的 DateTimePicker 课程。

List<Widget> _widgetOptions = <Widget>[
  new Text('Home'),
  Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          ButtonTheme(
            minWidth: 200.0,
            height: 100.0,
            child: RaisedButton(
              highlightElevation: 2.0 ,
              elevation: 10.0,
              onPressed: (){

              //HERE new DateTimePicker().selectTime(context)

                  },
                child: const Text('Buscar partido', style: TextStyle(fontSize: 45,fontStyle FontStyle.italic , color: Colors.black54)),
                shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(50.0)),
            ),
          )
        ],
      ),
    ),
    new Text('Historial')];

@override
Widget build(BuildContext context) {
  return new WillPopScope(
    onWillPop: _onWillPop,
  child: Scaffold(
    appBar: AppBar(
      title: const Text('Sportive',style: TextStyle(color: Colors.black54 ,fontStyle: FontStyle.italic, fontSize:32.0, letterSpacing: 1.5, fontWeight: FontWeight.bold )),
      centerTitle: true,
      leading: new Container()
    ),
    body: Center(
      child: _widgetOptions.elementAt(_selectedIndex),
    ),
    bottomNavigationBar: BottomNavigationBar(
      items: const <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          title: Text('Home'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.directions_run),
          title: Text('Jugar'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.menu),
          title: Text('Historial'),
        ),
      ],
      currentIndex: _selectedIndex,
      selectedItemColor: Colors.amber[800],
      onTap: _onItemTapped,

    ),
  ),
  );
}
}

我不知道为什么不能访问 List 中的上下文

标签: flutterdart

解决方案


不要在build()方法之外定义列表。真的就是这么简单。


推荐阅读