Flutter图形表示插件graphic_representation的使用

发布于 1周前 作者 wuwangju 来自 Flutter

Flutter图形表示插件graphic_representation的使用

Easy graphic representation 此包将允许您使用Flutter非常快速地进行图形表示。

安装

在您的pubspec.yaml文件的dependencies部分添加以下行:

dependencies:
    graphic_representation: ^1.1.2

使用

导入此类:

import 'package:graphic_representation/graphic_representation.dart';

示例

类DiscreteGraphic

构建一个StatelessWidget:包含定义大小的Container并包含图形。 该图形可以包含不同的元素:

  • 如果定义了colorPoint,则包含点。
  • 如果定义了colorLine,则包含线。
  • 如果定义了colorBox,则包含垂直条形图。
DiscreteGraphic(
    size: Size(MediaQuery.of(context).size.width,
        MediaQuery.of(context).size.height * 0.35),
    nums: [1, 2, 5, 3, 7, 13, 7],
    listGradX: [
        "Lun",
        "Mar",
        "Mer",
        "Jeu",
        "Ven",
        "Sam",
        "Dim",
    ],
    colorAxes: Colors.black,
    colorLine: Colors.blue,
    strokeLine : 2.0,
    colorPoint: Colors.blue,
    radiusPoint: 3.0,
    nbGradY: 9,
    minY: 0,
    maxY: 16,
)

类FunctionGraphic

构建一个StatelessWidget:包含定义大小的Container并包含图形。 该图形表示与属性functions和functionsXt/functionsYt关联的函数。对于参数化函数,您可以使用functionsXt和functionsYt列表。 您可以在同一图形上表示多个函数。

FunctionGraphic(
    size: Size(MediaQuery.of(context).size.width,
        MediaQuery.of(context).size.height * 0.5),
    functions: [(x) => exp(-x * x/8), (x) => sin(x) / x, (x) => sin(x/2)],
    functionsXt: [],
    functionsYt: [],
    colorAxes: Colors.black,
    colors: [Colors.purple, Colors.green, Colors.red],
    nbGradX: 11,
    minX: -10,
    maxX: 10,
    minY: -1,
    maxY: 1,
    strokeLine: 3.0,
)
FunctionGraphic(
    size: Size(MediaQuery.of(context).size.width,
        MediaQuery.of(context).size.height * 0.35),
    functions: [],
    functionsXt: [(t) => sin(2*t)],
    functionsYt: [(t) => sin(3*t)],
    colorAxes: Colors.black,
    colorsParam: [Colors.purple],
    nbGradX: 11,
    minT: -pi,
    maxT: pi,
    minX: -1,
    maxX: 1,
    minY: -1,
    maxY: 1,
    strokeLine: 3.0,
)

类CircularGraphic

构建一个StatelessWidget:包含定义大小的Container并包含圆形图形。 该图形表示数据列表nums。

CircularGraphic(
    context: context,
    nums: [204, 180, 243, 231, 378, 798],
    titles: [
        "Lundi",
        "Mardi",
        "Mercredi",
        "Jeudi",
        "Vendredi",
        "Samedi"
    ],
    colors: [
        Colors.blue,
        Colors.purple,
        Colors.yellow,
        Colors.green,
        Colors.red,
        Colors.brown
    ],
    showPourcentage: true,
    colorsInfo: Colors.white,
)

类EccEcdGraphic

构建一个StatelessWidget:包含定义大小的Container并包含ECC-ECD图形。 该图形表示数据列表numsX和numsY。

EccEcdGraphic(
    size: Size(MediaQuery.of(context).size.width,
        MediaQuery.of(context).size.height * 0.6),
    numsX: [0, 30, 15, 40, 50, 70, 130],
    numsY: [0, 23, 117, 27, 18, 39, 350],
    pourcentageMode: true,
    nbGradX: 14,
    nbGradY: 21,
    showECC: true,
    showECD: true,
    colorECC: Colors.purple,
    colorECD: Colors.brown,
    strokeLine: 2.0,
    showMedian: true,
    colorMedian: Colors.black,
)

类TableValues

构建一个StatelessWidget:包含定义大小的Container并包含表格值。 如果提供了f函数,将显示带有计算值的表格。

TableValues(
    size: Size(MediaQuery.of(context).size.width,
        MediaQuery.of(context).size.height * 0.2),
    functionName: 'x² + 1/x',
    f: (x) => x*x + 1/x,
    numsX: [-2, -1, 0, 1, 2],
    fontSize: 12,
)

类TableSign

构建一个StatelessWidget:包含定义大小的Container并包含表格符号。 显示一个表格符号。

TableSign(
    size: Size(MediaQuery.of(context).size.width,
        MediaQuery.of(context).size.height * 0.3),
    rowsLabels: [
        ["x", "-∞", "-1", "0,5", "1", "+∞"],
        ["x - 0,5", "-", "-/0", "+", "+"],
        ["x - 1", "-", "-", "-/0", "+"],
        ["x + 1", "-/0", "+", "+", "+"],
        ["f(x)", "-/NAN", "+/0", "-/NAN", "+"],
    ],
    fontSize: 18.0,
)

类TableVariation

构建一个StatelessWidget:包含定义大小的Container并包含变化表。 显示一个变化表。

TableVariation(
    size: Size(MediaQuery.of(context).size.width,
        MediaQuery.of(context).size.height * 0.3),
    rowsLabels: [
        ["x", "-∞", "-1", "1", "3", "+∞"],
        ["f '(x)", "+/0", "-/NAN", "-/0","+"],
        ["f(x)", "-∞/P0", "-5/P2", "-∞/P0/+∞/P2/NAN","3/P0","+∞/P2"]
    ],
    fontSize: 18.0,
    strokeWidth: 1.5,
)

示例Demo

以下是完整的示例Demo:

import 'package:example/circular_graphic_page.dart';
import 'package:example/discrete_graphic_page.dart';
import 'package:example/ecc_ecd_graphic_page.dart';
import 'package:example/function_graphic_page.dart';
import 'package:example/parametrized_graphic_page.dart';
import 'package:example/tablesigne_page.dart';
import 'package:example/tablevalues_page.dart';
import 'package:example/tablevariation_page.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: "/",
      routes: <String, WidgetBuilder>{
        "/": (BuildContext context) =>
            MyHomePage(title: 'Graphic_Representation : examples'),
        "discrete_graphic_page": (BuildContext context) =>
            DiscreteGraphicPage(),
        "function_graphic_page": (BuildContext context) =>
            FunctionGraphicPage(),
        "circular_graphic_page": (BuildContext context) =>
            CircularGraphicPage(),
        "ecc_ecd_graphic_page": (BuildContext context) => EccEcdGraphicPage(),
        "parametrized_graphic_page": (BuildContext context) =>
            ParametrizedGraphicPage(),
        "tablevalue_page": (BuildContext context) =>
            TableValuePage(),
        "tablesign_page": (BuildContext context) =>
            TableSignPage(),
        "tablevariation_page": (BuildContext context) =>
            TableVariationPage(),
      },
      debugShowCheckedModeBanner: false,
    );
  }
}

ElevatedButton myButton(BuildContext context, String title, String pushName) {
  return ElevatedButton(
      onPressed: () {
        Navigator.pushNamed(context, pushName);
      },
      style: ElevatedButton.styleFrom(
        primary: Colors.blueAccent,
      ),
      child: Padding(
          padding: EdgeInsets.all(5.0),
          child: Text(
            title,
            textScaleFactor: 1.8,
          )));
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          myButton(context, "DiscreteGraphic", "discrete_graphic_page"),
          myButton(context, "FunctionGraphic1", "function_graphic_page"),
          myButton(context, "FunctionGraphic2", "parametrized_graphic_page"),
          myButton(context, "TableValue", "tablevalue_page"),
          myButton(context, "TableSign", "tablesign_page"),
          myButton(context, "TableVariation", "tablevariation_page"),
          myButton(context, "CircularGraphic", "circular_graphic_page"),
          myButton(context, "EccEcdGraphic", "ecc_ecd_graphic_page")
        ],
      )),
    );
  }
}

更多关于Flutter图形表示插件graphic_representation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter图形表示插件graphic_representation的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,关于graphic_representation这个Flutter插件(请注意,这是一个假设的插件名称,因为Flutter生态系统中并没有一个广泛知名且直接命名为graphic_representation的官方或主流插件),我可以提供一个假设性的代码案例来展示如何在Flutter应用中使用图形表示插件。

假设graphic_representation插件提供了一些基本功能来绘制图形,比如线条、圆形和矩形,并且它有一个类似GraphicCanvas的组件来承载这些图形。以下是一个简化的代码示例,展示了如何使用这样的插件:

import 'package:flutter/material.dart';
import 'package:graphic_representation/graphic_representation.dart'; // 假设的导入路径

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Graphic Representation Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Graphic Representation Demo'),
        ),
        body: Center(
          child: GraphicCanvas(
            children: <GraphicElement>[
              // 绘制一个矩形
              GraphicRect(
                color: Colors.red,
                width: 100,
                height: 50,
                topLeft: Offset(50, 100),
              ),
              // 绘制一个圆形
              GraphicCircle(
                color: Colors.blue,
                radius: 30,
                center: Offset(200, 200),
              ),
              // 绘制一条线
              GraphicLine(
                color: Colors.green,
                strokeWidth: 2,
                from: Offset(50, 300),
                to: Offset(350, 300),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

// 假设的GraphicElement抽象类,用于定义图形的基本属性
abstract class GraphicElement {
  Color get color;
}

// 假设的GraphicRect类,用于表示矩形
class GraphicRect extends StatelessWidget implements GraphicElement {
  final Color color;
  final double width;
  final double height;
  final Offset topLeft;

  GraphicRect({required this.color, required this.width, required this.height, required this.topLeft});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.circular(8),
      ),
      width: width,
      height: height,
      // 使用Positioned或者Transform来精确定位
      // 这里简化处理,仅作为示例
      margin: EdgeInsets.only(left: topLeft.dx, top: topLeft.dy),
    );
  }

  @override
  Color get color => this.color;
}

// 假设的GraphicCircle类,用于表示圆形
class GraphicCircle extends StatelessWidget implements GraphicElement {
  final Color color;
  final double radius;
  final Offset center;

  GraphicCircle({required this.color, required this.radius, required this.center});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        color: color,
      ),
      width: radius * 2,
      height: radius * 2,
      // 使用Positioned或者Transform来精确定位
      // 这里简化处理,仅作为示例
      margin: EdgeInsets.only(left: center.dx - radius, top: center.dy - radius),
    );
  }

  @override
  Color get color => this.color;
}

// 假设的GraphicLine类,用于表示线条
class GraphicLine extends StatelessWidget implements GraphicElement {
  final Color color;
  final double strokeWidth;
  final Offset from;
  final Offset to;

  GraphicLine({required this.color, required this.strokeWidth, required this.from, required this.to});

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: LinePainter(
        color: color,
        strokeWidth: strokeWidth,
        from: from,
        to: to,
      ),
    );
  }

  @override
  Color get color => this.color;
}

// 自定义Painter类,用于绘制线条
class LinePainter extends CustomPainter {
  final Color color;
  final double strokeWidth;
  final Offset from;
  final Offset to;

  LinePainter({required this.color, required this.strokeWidth, required this.from, required this.to});

  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..color = color
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;

    canvas.drawLine(from, to, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

// 假设的GraphicCanvas组件,用于承载图形元素
class GraphicCanvas extends StatelessWidget {
  final List<GraphicElement> children;

  GraphicCanvas({required this.children});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: children.map((element) => element).toList(),
    );
  }
}

注意

  1. 上述代码中的GraphicRectGraphicCircleGraphicLine以及GraphicCanvas都是假设性的实现,用于展示如何组织图形表示的逻辑。
  2. 在实际使用中,graphic_representation插件可能会有自己的一套API和组件,你需要参考该插件的官方文档来使用。
  3. CustomPaintCustomPainter是Flutter中用于自定义绘制的强大工具,这里用于绘制线条。
  4. 由于graphic_representation是一个假设的插件名称,因此你需要替换为实际的插件名称和API来使用。

希望这个示例能帮助你理解如何在Flutter中使用图形表示插件的基本思路。

回到顶部