【问题标题】:Create a bar like github languages bar in flutter在颤振中创建一个类似 github 语言栏的栏
【发布时间】:2020-09-28 08:56:32
【问题描述】:

我需要实现这样的东西。

它基本上是一个有多种颜色的条,每种颜色都有一个长度。此外,可能会在每种颜色上添加一个文本。

如何在 Flutter 中实现这一点?

【问题讨论】:

    标签: flutter dart flutter-layout flutter-animation


    【解决方案1】:

    您想使用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,
                      ),
                    ),
                  ],
                ),
              ],
            )),
          ),
        );
      }
    }
    

    看起来是这样的:

    【讨论】:

      【解决方案2】:

      简单的方法是使用支持水平堆叠条形图的图表库。

      更难的方法是使用行和扩展小部件创建您自己的小部件。 像这样的:

              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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-02
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多