Home > Software engineering >  How can I rebuild a statefullwidget page with a parameter changes
How can I rebuild a statefullwidget page with a parameter changes

Time:04-28

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.

CodePudding user response:

Try

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