简体   繁体   English

在flutter中创建一个类似于github语言栏的栏

[英]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 https://api.flutter.dev/flutter/widgets/Expanded-class.html

You'd want to use a Flexible widget:你想使用一个Flexible小部件:

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:这是它的样子:

演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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