如何从List <Widget>访问上下文?

时间:2019-06-26 21:27:02

标签: flutter dart

我试图显示一个onPress的时间选择器,但出现此错误,我有点理解它的意思,但无法解决...

错误:``初始化器中只能访问静态成员。''

我有自己的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,

    ),
  ),
  );
}
}

我不知道为什么无法访问列表中的上下文

1 个答案:

答案 0 :(得分:0)

不要在build()方法之外定义列表。真的很简单。