为什么是我的listview.在颤振中,构建器未更新setstate


class _ProjectDrawerState extends State<ProjectDrawer> {

@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;
bool showMore = false;
print('ls Length --- ${widget.ls.length}');
void toggleShowMore()
{
print('Inside toggleshowMore original value --- $showMore');
setState(() {
showMore = !showMore;
});
print('Inside toggleshowMOre updated value --- $showMore');
}
return Scaffold(
body: SafeArea(
child: Container(
margin: EdgeInsets.only(
left: 0.04 * width,
right: 0.04 * width,
top: 0.14 * height,
bottom: 0.1 * height),
decoration: BoxDecoration(
color: const Color(0xffefefef),
borderRadius: BorderRadius.circular(0.037 * width),
),
child:  SingleChildScrollView(
child: IntrinsicHeight(
child: ResponsiveRowColumnItem(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
height: /*showMore ? (widget.ls.length * height * 0.025) :*/ height * 0.25,
child: ListView.builder(
physics:
const AlwaysScrollableScrollPhysics(),
itemCount: showMore ? widget.ls.length : 4,
itemBuilder:
(BuildContext context, int index) {
print('ShowMore value inside ListView.builder  --- $showMore');
return SingleChildScrollView(
child: ListTile(
dense: true,
title: Row(
children: [
SizedBox(
width: width * 0.0255,
),
Image.asset(
'assets/logo.png',
width: 24,
height: 24),
Text(
'        ${widget.ls[index]}',
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontSize: 18,
letterSpacing: 0,
fontWeight:
FontWeight.normal,
height: 1),
),
],
),
),
);
},
),
),
MaterialButton(
onPressed: toggleShowMore,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: width * 0.22,
child: const Divider(
color: Color(0xffbdbdbd),
thickness: 1.5,
height: 0.05),
),
Text(
showMore ? 'See Less' : 'See more',
style: const TextStyle(
fontFamily: 'Roboto',
fontSize: 18,
fontWeight: FontWeight.w600,
color: Color(0xff000000),
),
),
Icon(
showMore ? Icons.arrow_drop_up_sharp : Icons.arrow_drop_down_sharp,
color: Colors.black,
size: 40,
),
SizedBox(
width: width * 0.2,
child: const Divider(
color: Color(0xffbdbdbd),
thickness: 1.5,
height: 0.05),
),
],
),
),

],
),
),
)),

),
));

}}

我增加了打印语句调试,在setState showMore更新为true。但是为什么setstate不在listview中更新showmore value为true呢?生成器和打印列表中的所有项目?默认情况下,它只打印4个元素,但在单击setstate之后,没有发生任何变化,更多的元素不会像预期的那样呈现。

将变量和函数移出构建方法

bool showMore = false;
print('ls Length --- ${widget.ls.length}');
void toggleShowMore()
{
print('Inside toggleshowMore original value --- $showMore');
setState(() {
showMore = !showMore;
});
print('Inside toggleshowMOre updated value --- $showMore');
}
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;
return Scaffold(
.....

调用setState意味着我们重新执行这个方法:Widget build(BuildContext context)

如果你把变量放在里面,那么值将被重置为你声明的初始值

相关内容

最新更新