step-progress-indicator icon indicating copy to clipboard operation
step-progress-indicator copied to clipboard

Dynamic height with scrollable for custom length of steps

Open pratikbutani opened this issue 3 years ago • 2 comments

I am using verticle StepProgressIndicator and I want to give dynamic height to it because I am generating steps based on the length of API.

Is there any way to give height dynamically with a scrollable list of indicators?

pratikbutani avatar Jul 06 '21 07:07 pratikbutani

any solution?

kevin4dhd avatar Jul 29 '21 07:07 kevin4dhd

Hi @pratikbutani (cc @kevin4dhd)

The height of each step with StepProgressIndicator is computed based on the parent container size. Therefore, if you want the size of the each step to change based on a dynamic value, you should change the height of the container.

Below an example:

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: const [
            Box(height: 100),
            Box(height: 200),
            Box(height: 300),
            Box(height: 400),
            Box(height: 500),
          ],
        ),
      ),
    );
  }
}

class Box extends StatelessWidget {
  final double height;
  const Box({
    Key? key,
    required this.height,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: Colors.black,
          width: 2,
        ),
      ),
      width: 100,
      height: height,
      child: const Indicator(),
    );
  }
}

class Indicator extends StatelessWidget {
  const Indicator({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const StepProgressIndicator(
      totalSteps: 10,
      currentStep: 5,
      size: 20,
      direction: Axis.vertical,
    );
  }
}
Screenshot 2022-01-02 at 15 31 49

The principle is the same if your parent container is scrollable:

class ScrollContainerSize extends StatelessWidget {
  const ScrollContainerSize({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SizedBox(
          height: 300,
          child: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: const [
                Box(height: 500),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class Box extends StatelessWidget {
  final double height;
  const Box({
    Key? key,
    required this.height,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: Colors.black,
          width: 2,
        ),
      ),
      width: 100,
      height: height,
      child: const Indicator(),
    );
  }
}

class Indicator extends StatelessWidget {
  const Indicator({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const StepProgressIndicator(
      totalSteps: 10,
      currentStep: 5,
      size: 20,
      direction: Axis.vertical,
    );
  }
}
Screenshot 2022-01-02 at 15 37 19

SandroMaglione avatar Jan 02 '22 14:01 SandroMaglione