Home > database >  LinearProgressIndicator animation Flutter
LinearProgressIndicator animation Flutter

Time:11-27

I'm trying to create a page with a page slider carousel and a progress bar. When the carousel moves to another page I want that the progress bar updates from a value to another with an animation. I tried LinearProgressIndicator but I don't know how to set animation from the old value to new one. This is what I have

LinearProgressIndicator(
  minHeight: 2,
  value: currentPageIndex/(pages.length - 1)
)

currentPageIndex is updated with setState() method externally.

Is there any way to do it? Thanks in advance.

CodePudding user response:

You should use setState(){} when page is swiped and currentPageIndex is updated.

CodePudding user response:

You can try using this approach

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

/// This is the private State class that goes with MyStatefulWidget.
/// AnimationControllers can be created with `vsync: this` because of TickerProviderStateMixin.
class _MyStatefulWidgetState extends State<MyStatefulWidget>
    with TickerProviderStateMixin {
  late AnimationController controller;
  
 late Animation _animation; 
  int count = 0;
  @override
  void initState() {
    controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 5),
    )..addListener(() {
        if (_animation.value == count/5) {
          controller.stop(canceled: false);
        }
        setState(() {});
      });
    // ignore: avoid_single_cascade_in_expression_statements
    controller.addStatusListener((AnimationStatus status){
       
    });
   _animation = Tween<double>(
  begin: 0,
  end: 1,
).animate(controller);
    //controller.repe
    //controller.forward();
    super.initState();
  }
 
  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            const Text(
              'Linear progress indicator with a fixed color',
              style: TextStyle(fontSize: 20),
            ),
            LinearProgressIndicator(
              value: _animation.value,
              semanticsLabel: 'Linear progress indicator',
            ),
            ElevatedButton(
              child: const Text(
              'count',
              style: TextStyle(fontSize: 20),
            ),
              onPressed: (){
                setState(() {
                  if (count > 5) return;
                  count   ;
                 
                  controller.forward(from:count/5);
                    
                  
                });
              }
            )
          ],
        ),
      ),
    );
  }
}

Run code from here

  • Related