gpt4 book ai didi

java - 即使未在构建方法中声明变量,setState 也不会更新 UI

转载 作者:行者123 更新时间:2023-12-02 01:26:16 28 4
gpt4 key购买 nike

即使未在构建方法中声明变量,setState 函数也不会更新列表中的文本。

我正在尝试更改 bottomNavigationBar 的第一个和第二个选项卡中的文本。

代码如下:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
const MyApp({super.key});

static const String _title = 'Flutter Code Sample';

@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}

class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key});

@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}


class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);

int _volume = 0;

void changeVolume(){
_volume += 1;
}

late final List<Widget> _widgetOptions = <Widget>[
Row(
children: <Widget>[
IconButton(
icon: const Icon(Icons.volume_up),
tooltip: 'Increase volume by 1',
onPressed: (){
setState(() {
changeVolume();
});
},
),
Expanded(
child: Text('Volume : $_volume'),
),
Expanded(
child: FittedBox(
child: FlutterLogo(),
),
),
],
),
Text('Volume : $_volume'),
Text(
'Index 2: School',
style: optionStyle,
),
];

void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}

@override
Widget build(BuildContext context) {


return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Business',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'School',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}

大多数时候这个问题出现在这个网站上是因为有人在构建方法中声明了变量,但我在这里没有这样做,所以我不确定为什么文本没有更新。

最佳答案

setState 将重新运行 build 方法,因此如果您在 build 方法之外声明 _widgetOptions,它的内容不会改变。 _selectedIndex 上的 setState 会起作用,因为它只选择 _widgetOptions 中的一项。

因此删除late final 并将_widgetOptions 的声明移动到构建方法中:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
const MyApp({super.key});

static const String _title = 'Flutter Code Sample';

@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}

class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key});

@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);

int _volume = 0;

void changeVolume() {
_volume += 1;
}

void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}

@override
Widget build(BuildContext context) {
List<Widget> _widgetOptions = <Widget>[
Row(
children: <Widget>[
IconButton(
icon: const Icon(Icons.volume_up),
tooltip: 'Increase volume by 1',
onPressed: () {
setState(() {
changeVolume();
});
},
),
Expanded(
child: Text('Volume : $_volume'),
),
Expanded(
child: FittedBox(
child: FlutterLogo(),
),
),
],
),
Text('Volume : $_volume'),
Text(
'Index 2: School',
style: optionStyle,
),
];

return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Business',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'School',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}

当然你可以优化它,但上面的代码在 DartPad 中按预期工作。

(顺便说一下,late 标记了一个成员变量,它在声明时没有赋值给它,但是你要确保它在你的代码的任何部分访问它之前有一个值。所以事实上,当您为变量分配一个非空值时,它没有任何实际意义。)

关于java - 即使未在构建方法中声明变量,setState 也不会更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74552788/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com