简体   繁体   中英

Create a bar like github languages bar in flutter

I need to implement something like this.

有多种颜色的酒吧

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?

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

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:

演示

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM