flutter - 如何从列表访问上下文?
问题描述
我正在尝试显示时间选择器 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 中的上下文
解决方案
不要在build()
方法之外定义列表。真的就是这么简单。
推荐阅读
- mysql - 在mysql中的不同选择上选择最大时间戳值
- python - Django NameError: name 'Instractor' is not defined
- mysql - 我如何才能找到每年目前在职的每种性别的员工总数?
- node.js - 用于 redis 会话功能的玩笑模拟
- firebase - 数组内不允许使用 Firebase Firestore 时间戳
- ios - 缺少“通用 iOS 设备”
- python - Azure Function 在本地运行,但在 Docker 容器构建并在本地运行后不执行任何操作
- angular - Web3 合约方法调用在 Angular 中引发错误
- cassandra - 从 Datastax 6.0 到 Cassandra 3 的迁移路径
- angular - Angular 11-对象可能是“空”