Flutter - Progress Bar from the Rows background color

Viewed 44

I've got row with some text on the left and right side of it. center is expanded so both texts are located at left, right corners.

Row( children: [  Text('left Text'),
 Expanded(child: Text('')),
Text('right Text'
),
]),

Now I I wrap this Row in a Container with color then the whole row will get background color.

Container( color: Colors.green,
child:MY_ROW()
)

What I want now is to put some background color not on the entire row but controllable part of it, to make a progress bar. is that possible ? Also if this progress bar could be animated that would be a plus :)

1 Answers

Here is the code

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double _percent = 0.0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MyCustomProgressBar(percent: _percent),
      floatingActionButton: FloatingActionButton(
        onPressed: () => setState(() => _percent < 100 ? _percent += 10 : null),
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

class MyCustomProgressBar extends StatefulWidget {
  final double percent;
  const MyCustomProgressBar({
    Key? key,
    required this.percent,
  }) : super(key: key);

  @override
  State<MyCustomProgressBar> createState() => _MyCustomProgressBar();
}

class _MyCustomProgressBar extends State<MyCustomProgressBar> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 70,
      child: Stack(
        //direction: Axis.horizontal,
        children: [
          AnimatedContainer(
            width: MediaQuery.of(context).size.width / 100 * widget.percent,
            color: Colors.green,
            duration: const Duration(milliseconds: 500),
          ),
          Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(widget.percent.toString()),
              const Text('Right text'),
            ],
          ),
        ],
      ),
    );
  }
}

Related