如何使用参数更改重建状态fulwidget页面
在此处输入图像描述 [import'package'package:flutter/yaters.dart'; 导入'软件包:flutter_application_product/core/fit_container.dart';
class ProductImages extends StatefulWidget {
const ProductImages({Key? key, required this.images}) : super(key: key);
final List<String> images;
@override
State<ProductImages> createState() => _ProductImagesState();
}
class _ProductImagesState extends State<ProductImages> {
int currentId = 0;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.bottomCenter,
children: \[
PageView.builder(
onPageChanged: (value) {
print(value);
currentId = value;
},
scrollDirection: Axis.horizontal,
itemCount: widget.images.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: FitContainer(imagePath: widget.images\[index\]),
);
},
),
IconsDependsOnPage(widget: widget, currentId: currentId),
\],
);
}
}
class IconsDependsOnPage extends StatefulWidget {
const IconsDependsOnPage({
Key? key,
required this.widget,
required this.currentId,
}) : super(key: key);
final ProductImages widget;
final int currentId;
@override
State<IconsDependsOnPage> createState() => _IconsDependsOnPageState();
}
class _IconsDependsOnPageState extends State<IconsDependsOnPage> {
@override
Widget build(BuildContext context) {
print(widget.currentId);
print("selamlar");
return SizedBox(
height: 50,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: widget.widget.images.length,
itemBuilder: (BuildContext context, int index) {
return Icon(Icons.circle,
color: widget.currentId == index ? Colors.green : Colors.black);
}),
);
}
}][1]
我想对IconSdependsonPage说,您必须在您的参数(CurrentID)更改时重建,但是我尝试使用DIDUPDATEWIDGET,但不起作用,您能告诉我我该怎么做,或者您能为我提供有关此的链接或文档,谢谢你提前。这是我的第一个问题,很抱歉,如果我错误或不完全问这个问题。
enter image description here [import 'package:flutter/material.dart';
import 'package:flutter_application_product/core/fit_container.dart';
class ProductImages extends StatefulWidget {
const ProductImages({Key? key, required this.images}) : super(key: key);
final List<String> images;
@override
State<ProductImages> createState() => _ProductImagesState();
}
class _ProductImagesState extends State<ProductImages> {
int currentId = 0;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.bottomCenter,
children: \[
PageView.builder(
onPageChanged: (value) {
print(value);
currentId = value;
},
scrollDirection: Axis.horizontal,
itemCount: widget.images.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: FitContainer(imagePath: widget.images\[index\]),
);
},
),
IconsDependsOnPage(widget: widget, currentId: currentId),
\],
);
}
}
class IconsDependsOnPage extends StatefulWidget {
const IconsDependsOnPage({
Key? key,
required this.widget,
required this.currentId,
}) : super(key: key);
final ProductImages widget;
final int currentId;
@override
State<IconsDependsOnPage> createState() => _IconsDependsOnPageState();
}
class _IconsDependsOnPageState extends State<IconsDependsOnPage> {
@override
Widget build(BuildContext context) {
print(widget.currentId);
print("selamlar");
return SizedBox(
height: 50,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: widget.widget.images.length,
itemBuilder: (BuildContext context, int index) {
return Icon(Icons.circle,
color: widget.currentId == index ? Colors.green : Colors.black);
}),
);
}
}][1]
I want to say to IconsDependsOnPage, you must rebuild when your parameter(currentId) changes, but I tried with didUpdateWidget but don't works, can you tell me how can I do or can you provide me a link or documents about this,thank you in advance. This is my first question, I'm sorry if I asked the question incorrectly or incompletely.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
Try