如何重建带有参数更改的 statefullwidget 页面
How can I rebuild a statefullwidget page with a parameter changes
enter image description here[导入'package:flutter/material.dart';
导入 '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]
我想对 IconsDependsOnPage 说,当你的参数(currentId)改变时你必须重建,但我尝试了 didUpdateWidget 但没有用,你能告诉我我该怎么做或者你能给我一个 link 或关于此的文件,提前谢谢你。这是我的第一个问题,如果我问错或不完整,我很抱歉。
尝试
onPageChanged: (value){
print(value);
setState((){
currentId = value;
});
}
enter image description here[导入'package:flutter/material.dart'; 导入 '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]
我想对 IconsDependsOnPage 说,当你的参数(currentId)改变时你必须重建,但我尝试了 didUpdateWidget 但没有用,你能告诉我我该怎么做或者你能给我一个 link 或关于此的文件,提前谢谢你。这是我的第一个问题,如果我问错或不完整,我很抱歉。
尝试
onPageChanged: (value){
print(value);
setState((){
currentId = value;
});
}