Flutter 当我尝试在initState中初始化列表时,它会给出一个错误,表示它可以';不要使用主题,因为主题可能会改变
我有一个用于设置页面的小部件,该小部件有一个带有设置的列表。但是因为ListView有一个ItemBuild,它通过数组循环,所以我尝试在initState中初始化该数组。但是当我尝试这样做时,我得到了以下错误 我无法在initState中使用主题数据,因为主题可能会更改。列表中的项目是静态的,不会动态创建。是否可以跳过使用项目生成,而只将项目作为子项渲染?我不想使用列,因为我喜欢带有分隔符的列表样式Flutter 当我尝试在initState中初始化列表时,它会给出一个错误,表示它可以';不要使用主题,因为主题可能会改变,flutter,Flutter,我有一个用于设置页面的小部件,该小部件有一个带有设置的列表。但是因为ListView有一个ItemBuild,它通过数组循环,所以我尝试在initState中初始化该数组。但是当我尝试这样做时,我得到了以下错误 我无法在initState中使用主题数据,因为主题可能会更改。列表中的项目是静态的,不会动态创建。是否可以跳过使用项目生成,而只将项目作为子项渲染?我不想使用列,因为我喜欢带有分隔符的列表样式 class _TheSettingsPageState extends State<T
class _TheSettingsPageState extends State<TheSettingsPage> {
List<Widget> settings;
bool _isMilesPerHour = globals.preferences.speedNotation.isMilesPerHour;
bool _isKilometersPerHour = globals.preferences.speedNotation.isKilometersPerHour;
File _pickedImage;
@override
@mustCallSuper
void initState(){
super.initState();
setState(() {
settings =
[
Text(
'Account',
style: Theme.of(context).textTheme.body1,
),
ListTile
(
title: Text('Profile'),
trailing: Icon(Icons.edit),
),
Text('Style'),
ListTile
(
title: Text('Background Image'),
trailing: Icon(Icons.edit),
),
ListTile
(
title: Text('Dark Mode'),
trailing: Switch(
value: true,
onChanged: (isDarkmode) => print(isDarkmode),
),
),
Text('Units'),
ListTile
(
title: Text(SpeedNotation.milesPerHour.shortNotation),
trailing: Switch(
value: _isMilesPerHour,
onChanged: _onMilesPerHourToggled
),
),
ListTile
(
title: Text(SpeedNotation.kilometersPerHour.shortNotation),
trailing: Switch(
value: _isKilometersPerHour,
onChanged: _onKilometersPerHourToggled
),
),
];
});
}
_onMilesPerHourToggled(bool isMilesPerHour){
setState(() {
_isMilesPerHour = isMilesPerHour;
_isKilometersPerHour = !isMilesPerHour;
});
globals.preferences.writeSettings();
}
_onKilometersPerHourToggled(bool isKilometersPerHour){
setState(() {
_isKilometersPerHour = isKilometersPerHour;
_isMilesPerHour = !isKilometersPerHour;
});
globals.preferences.writeSettings();
}
Future getImageAsync() async{
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_pickedImage = image;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: Padding(
padding: EdgeInsets.all(30.0),
child: Column
(
mainAxisAlignment: MainAxisAlignment.center,
children:
[
Expanded(
flex: 1,
child: Stack
(
children: [
Align(
alignment: Alignment.topLeft,
child: NavigatePopButton(),
),
Align
(
alignment: Alignment.topCenter,
child: Text
(
'Settings',
style: Theme.of(context).textTheme.title,
),
),
],
)
),
Expanded
(
flex: 4,
child: MediaQuery.removePadding
(
context: context,
removeTop: true,
child: ListView.separated
(
itemCount: settings.length,
separatorBuilder: (BuildContext context, int index) => Divider(height: 1.0),
itemBuilder: (BuildContext context, int index)
{
return settings[index];
}
),
),
),
]
),
),
);
}
}
class\u设置孕母扩展状态{
列表设置;
bool_isMilesPerHour=globals.preferences.speedNotation.isMilesPerHour;
bool_iskm/h=globals.preferences.speedNotation.iskm/h;
文件_pickedImage;
@凌驾
@超级
void initState(){
super.initState();
设置状态(){
设置=
[
正文(
“账户”,
样式:Theme.of(context).textTheme.body1,
),
列表砖
(
标题:文本(“概要文件”),
尾随:图标(Icons.edit),
),
文本(“样式”),
列表砖
(
标题:文本(“背景图像”),
尾随:图标(Icons.edit),
),
列表砖
(
标题:文本(“暗模式”),
尾随:开关(
价值观:正确,
一旦更改:(isDarkmode)=>打印(isDarkmode),
),
),
文本(“单位”),
列表砖
(
标题:文本(SpeedNotation.milesPerHour.shortNotation),
尾随:开关(
值:_isMilesPerHour,
一旦更改:_onMilesPerHourToggled
),
),
列表砖
(
标题:文本(速度符号。每小时公里数。短符号),
尾随:开关(
值:_为公里/小时,
一旦改变:每小时公里
),
),
];
});
}
_onMilesPerHourToggled(bool isMilesPerHour){
设置状态(){
_isMilesPerHour=isMilesPerHour;
_Iskm/h=!isMilesPerHour;
});
globals.preferences.writeSettings();
}
_ON公里/小时切换(布尔值为公里/小时){
设置状态(){
_Iskm/h=Iskm/h;
_isMilesPerHour=!Iskm/h;
});
globals.preferences.writeSettings();
}
未来的getImageAsync()异步{
File image=wait ImagePicker.pickImage(源:ImageSource.gallery);
设置状态(){
_PickeImage=图像;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景色:主题。背景色,
主体:填充物(
填充:所有边缘设置(30.0),
子:列
(
mainAxisAlignment:mainAxisAlignment.center,
儿童:
[
扩大(
弹性:1,
子:堆栈
(
儿童:[
对齐(
对齐:alignment.topLeft,
子项:NavigatePopButton(),
),
排列
(
对齐:alignment.topCenter,
子:文本
(
“设置”,
样式:Theme.of(context).textTheme.title,
),
),
],
)
),
扩大
(
弹性:4,
子项:MediaQuery.removePadding
(
上下文:上下文,
removeTop:没错,
子项:ListView.separated
(
itemCount:settings.length,
separatorBuilder:(BuildContext上下文,int索引)=>分隔符(高度:1.0),
itemBuilder:(构建上下文,int索引)
{
返回设置[索引];
}
),
),
),
]
),
),
);
}
}
上下文在第一次生成后可用。因此,您不能像在initState()
中那样使用它。您可以使用WidgetsBinding.instance.addPostFrameCallback(({})代码>内部initState()
在那里您可以使用上下文
试试这个:
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
// Use context here
settings =
[
Text(
'Account',
style: Theme.of(context).textTheme.body1,
),
//...... Do all initializations that require context here
]
});
}
此外,在initState()
中不能setState()
<代码>设置状态()
用于生成重建。在initState()中,
是在第一次生成之前运行的,因此无需重建。您在initState()
中设置的所有内容都将在第一次构建中可用。上下文可用,但主题不可用。您可以自由地使用上下文,其他方式HI Remi!谢谢你的评论。你能帮我更新我的答案,这样就不会误导我吗?正如错误消息所说,主题可能会改变。所以你的列表不是一个典型的例子,当主题可以改变是黑暗模式