如何使用参数更改重建状态fulwidget页面

发布于 2025-01-24 12:56:56 字数 2500 浏览 0 评论 0原文

在此处输入图像描述 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2025-01-31 12:56:56

尝试

onPageChanged: (value){
  print(value);
  setState((){
    currentId = value;
});
}

Try

onPageChanged: (value){
  print(value);
  setState((){
    currentId = value;
});
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文