Create a bar like github languages bar in flutter

78 views Asked by At

I need to implement something like this.

bar with multiple colors

It is basically a bar with multiple color and each color has a length. Additionally a text might be added on each color.

How can I implement this in flutter?

2

There are 2 answers

0
FloatingRock On BEST ANSWER

You'd want to use a Flexible widget:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: SafeArea(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Row(
              children: [
                Flexible(
                  flex: 3, // 30%
                  child: Container(
                    color: Colors.green,
                    height: 20,
                  ),
                ),
                Flexible(
                  flex: 2, // 20%
                  child: Container(
                    color: Colors.yellow,
                    height: 20,
                  ),
                ),
                Flexible(
                  flex: 5, // 50%
                  child: Container(
                    color: Colors.cyan,
                    height: 20,
                  ),
                ),
              ],
            ),
          ],
        )),
      ),
    );
  }
}

Here's what that looks like:

Demo

0
Er1 On

The easy way is to use a chart library which supports horizontal stacked bar charts.

The somewhat harder way is to create your own widget with rows and Expanded widgets. Something like this:

        Row(
          children: <Widget>[
            Expanded(
              flex: 2,
              child: Container(
                color: Colors.amber,
                height: 100,
              ),
            ),
            Expanded(
              flex: 2,
              child: Container(
                color: Colors.red,
                height: 100,
              ),
            ),
            Expanded(
              flex: 1,
              child: Container(
                color: Colors.green,
                height: 100,
              ),
            ),
          ],
        ),

https://api.flutter.dev/flutter/widgets/Expanded-class.html