How can i access context from List<Widget>?

i am trying to show a time picker onPress a raisedButton but have this error, i understand a bit what it means but cant fix it...

Error:''Only static members can be accessed in initializers.''

I have my own class to 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,

    ),
  ),
  );
}
}

I dont know why cant access context in List

Upvotes: 1

Views: 773

Answers (1)

Michael Pfaff
Michael Pfaff

Reputation: 1263

Don't define the list outside the build() method. It's really that simple.

Upvotes: 5

Related Questions