Flutter界面布局百分比控制插件percentify的使用

Flutter界面布局百分比控制插件percentify的使用

Percentify

Percentify 是一个用于在 Flutter 应用程序中以不同组件形式显示百分比的包。该包提供了多种组件,可以用来向用户展示数据。

使用说明

Rounded Circular Percentify

[@override](/user/override)
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: RoundedCircularPercentify(
        40, // 进度值
        backgroundColor: Colors.black,
        valueColor: Colors.blueAccent,
        strokeWidth: 15,
        valueStrokeWidth: 25,
        child: const SizedBox(
          width: 200,
          height: 200,
          child: Center(
              child: Text(
            "${40}%",
            style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
          )),
        ),
      ),
    ),
  );
}

Rect Circular Percentify

[@override](/user/override)
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: RectCircularPercentify(
        22, // 进度值
        backgroundColor: Colors.red,
        valueColor: Colors.blue,
        strokeWidth: 20,
        valueStrokeWidth: 35
      ),
    ),
  );
}

Rounded Linear Percentify

[@override](/user/override)
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: RoundedLinearPercentify(
        22, // 进度值
        backgroundColor: Colors.red,
        valueColor: Colors.blue,
        strokeWidth: 20,
        valueStrokeWidth: 35
      ),
    ),
  );
}

Rect Linear Percentify

[@override](/user/override)
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: RectLinearPercentify(
        22, // 进度值
        backgroundColor: Colors.red,
        valueColor: Colors.blue,
        strokeWidth: 20,
        valueStrokeWidth: 35
      ),
    ),
  );
}

更多关于Flutter界面布局百分比控制插件percentify的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter界面布局百分比控制插件percentify的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中,percentify 是一个用于百分比布局的插件。它允许你以百分比的形式设置控件的大小和位置,从而更灵活地进行界面布局。下面是如何使用 percentify 插件的详细说明。

1. 安装 percentify 插件

首先,你需要在 pubspec.yaml 文件中添加 percentify 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  percentify: ^0.0.1  # 请检查最新版本

然后运行 flutter pub get 来安装插件。

2. 导入 percentify 插件

在你的 Dart 文件中导入 percentify 插件:

import 'package:percentify/percentify.dart';

3. 使用 Percentify 进行布局

percentify 提供了几个用于百分比布局的组件,包括 PercentWidthPercentHeightPercentPositioned 等。

示例 1: 使用 PercentWidthPercentHeight

import 'package:flutter/material.dart';
import 'package:percentify/percentify.dart';

class PercentageLayoutExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Percentage Layout Example'),
      ),
      body: Center(
        child: PercentWidth(
          percent: 0.5, // 50% of parent width
          child: PercentHeight(
            percent: 0.3, // 30% of parent height
            child: Container(
              color: Colors.blue,
              child: Center(
                child: Text(
                  '50% Width\n30% Height',
                  style: TextStyle(color: Colors.white, fontSize: 20),
                  textAlign: TextAlign.center,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

在这个例子中,PercentWidth 使子控件的宽度为父控件宽度的 50%,PercentHeight 使子控件的高度为父控件高度的 30%。

示例 2: 使用 PercentPositioned

import 'package:flutter/material.dart';
import 'package:percentify/percentify.dart';

class PercentagePositionedExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Percentage Positioned Example'),
      ),
      body: Stack(
        children: [
          PercentPositioned(
            leftPercent: 0.1, // 10% from the left
            topPercent: 0.2, // 20% from the top
            child: Container(
              width: 100,
              height: 100,
              color: Colors.red,
              child: Center(
                child: Text(
                  '10% Left\n20% Top',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),
          PercentPositioned(
            rightPercent: 0.1, // 10% from the right
            bottomPercent: 0.2, // 20% from the bottom
            child: Container(
              width: 100,
              height: 100,
              color: Colors.green,
              child: Center(
                child: Text(
                  '10% Right\n20% Bottom',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
回到顶部