Flutter动画效果插件riverflow的使用

Flutter动画效果插件riverflow的使用

Riverflow 是一个用于 Dart 开发者的库。它可以帮助开发者从网页中提取结构化数据。以下是 Riverflow 的使用方法。

使用方法

1. 创建模板

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:riverflow/riverflow.dart';

// 创建第一个阶段
final stage1 = DomStage(
  inputMapping: {
    'package_html': '/_body',
  },
  outputFields: {
    'package_html': FieldSelector(
      extractors: [
        HtmlIncludeExtractor(
          selector: 'div[class="packages"] div[class="packages-item"]',
        )
      ],
      isFlatten: true,
      collectAs: SingleFieldCollector(CollectTypes.ARRAY, OutputTypes.HTML_ELEMENT, null),
    ),
  },
  excludeMapping: {
    '/_body',
  },
);

// 创建第二个阶段
final stage2 = DomStage(
  inputMapping: {
    'package_name': '/package_html',
    'description': '/package_html',
    'likes': '/package_html',
    'health': '/package_html',
    'popularity': '/package_html',
  },
  outputFields: {
    'package_name': FieldSelector(
      extractors: [
        HtmlExtractor(selector: 'h3[class="packages-title"]', collectors: [r'${text()}']),
        StringTrimExtractor(),
      ],
    ),
    'description': FieldSelector(
      extractors: [
        HtmlExtractor(selector: 'p[class="packages-description"]', collectors: [r'${text()}'])
      ],
    ),
    'likes': FieldSelector(
      extractors: [
        HtmlExtractor(
            selector: 'div[class*="packages-score-like"] *[class="packages-score-value-number"]',
            collectors: [r'${text()}'])
      ],
      collectAs: SingleFieldCollector(CollectTypes.FIRST, OutputTypes.INT, 0),
    ),
    'health': FieldSelector(
      extractors: [
        HtmlExtractor(
            selector: 'div[class*="packages-score-health"] *[class="packages-score-value-number"]',
            collectors: [r'${text()}']),
      ],
      collectAs: SingleFieldCollector(CollectTypes.FIRST, OutputTypes.INT, 0),
    ),
    'popularity': FieldSelector(
      extractors: [
        HtmlExtractor(
            selector: 'div[class*="packages-score-popularity"] *[class="packages-score-value-number"]',
            collectors: [r'${text()}']),
      ],
    ),
  },
  excludeMapping: {
    '/package_html',
  },
);

// 将阶段组合成模板
final template = Template([stage1, stage2]);

2. 创建 WebFlow

final flow = WebFlow(template);

3. 下载网页并构建文档

void main() async {
  final response = await http.get(Uri.parse('https://pub.dev/flutter/favorites'));
  final html = response.body;

  // 将 HTML 字符串转换为文档对象
  var document = html.asDocument('https://pub.dev').documentElement!;
}

4. 启动流程并获取输出结果

var records = flow.start([document]);

records.forEach((record) {
  print(jsonEncode(record.toJson()));
  print('--------------------------\n');
});

输出结果应该类似于:

[
  {
    "package_name": "shared_preferences",
    "description": "Flutter plugin for reading and writing simple key-value pairs. Wraps NSUserDefaults on iOS and SharedPreferences on Android.",
    "likes": 1233,
    "health": 110,
    "popularity": "100"
  },
  {
    "package_name": "url_launcher",
    "description": "Flutter plugin for launching a URL on Android and iOS. Supports web, phone, SMS, and email schemes.",
    "likes": 858,
    "health": 110,
    "popularity": "100"
  },
  {
    "package_name": "flutter_bloc",
    "description": "Flutter Widgets that make it easy to implement the BLoC (Business Logic Component) design pattern. Built to be used with the bloc state management package.",
    "likes": 821,
    "health": 110,
    "popularity": "99"
  },
  {
    "package_name": "bloc",
    "description": "A predictable state management library that helps implement the BLoC (Business Logic Component) design pattern.",
    "likes": 440,
    "health": 110,
    "popularity": "99"
  },
  {
    "package_name": "location",
    "description": "A Flutter plugin to easily handle realtime location in iOS and Android. Provides settings for optimizing performance or battery.",
    "likes": 333,
    "health": 110,
    "popularity": "99"
  },
  {
    "package_name": "flutter_slidable",
    "description": "A Flutter implementation of slidable list item with directional slide actions that can be dismissed.",
    "likes": 623,
    "health": 110,
    "popularity": "98"
  },
  {
    "package_name": "built_value",
    "description": "Value types with builders, Dart classes as enums, and serialization. This library is the runtime dependency.\n",
    "likes": 127,
    "health": 110,
    "popularity": "99"
  },
  {
    "package_name": "convex_bottom_bar",
    "description": "A Flutter package which implements a ConvexAppBar to show a convex tab in the bottom bar. Theming supported.",
    "likes": 250,
    "health": 110,
    "popularity": "92"
  },
  {
    "package_name": "provider",
    "description": "A wrapper around InheritedWidget to make them easier to use and more reusable.",
    "likes": 1736,
    "health": 105,
    "popularity": "100"
  },
  {
    "package_name": "rxdart",
    "description": "RxDart is an implementation of the popular reactiveX api for asynchronous programming, leveraging the native Dart Streams api.\n",
    "likes": 535,
    "health": 105,
    "popularity": "100"
  }
]

更多关于Flutter动画效果插件riverflow的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter动画效果插件riverflow的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中,riverflow 是一个用于创建流畅动画效果的插件。它允许开发者通过简单的配置实现复杂的动画,非常适合用于展示数据流动、滚动视图等场景。下面是一个使用 riverflow 插件创建动画效果的代码示例。

首先,确保你已经在 pubspec.yaml 文件中添加了 riverflow 依赖:

dependencies:
  flutter:
    sdk: flutter
  riverflow: ^最新版本号  # 请替换为实际的最新版本号

然后,运行 flutter pub get 来获取依赖。

接下来,你可以在你的 Flutter 应用中使用 Riverflow 小部件来创建动画效果。以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Riverflow Animation Example'),
        ),
        body: Center(
          child: RiverflowExample(),
        ),
      ),
    );
  }
}

class RiverflowExample extends StatefulWidget {
  @override
  _RiverflowExampleState createState() => _RiverflowExampleState();
}

class _RiverflowExampleState extends State<RiverflowExample> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 5),
      vsync: this,
    )..repeat(reverse: true);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Riverflow(
      controller: _controller,
      items: [
        RiverflowItem(
          child: Container(
            width: 100,
            height: 100,
            color: Colors.red,
            child: Center(child: Text('1')),
          ),
          animationValue: 0.0, // 起始位置,可以是0.0到1.0之间的任意值
        ),
        RiverflowItem(
          child: Container(
            width: 100,
            height: 100,
            color: Colors.blue,
            child: Center(child: Text('2')),
          ),
          animationValue: 0.2, // 起始位置
        ),
        RiverflowItem(
          child: Container(
            width: 100,
            height: 100,
            color: Colors.green,
            child: Center(child: Text('3')),
          ),
          animationValue: 0.4, // 起始位置
        ),
        RiverflowItem(
          child: Container(
            width: 100,
            height: 100,
            color: Colors.yellow,
            child: Center(child: Text('4')),
          ),
          animationValue: 0.6, // 起始位置
        ),
        RiverflowItem(
          child: Container(
            width: 100,
            height: 100,
            color: Colors.purple,
            child: Center(child: Text('5')),
          ),
          animationValue: 0.8, // 起始位置
        ),
      ],
      itemSpacing: 10, // 项目之间的间距
      alignment: RiverflowAlignment.center, // 对齐方式
      scrollDirection: Axis.horizontal, // 滚动方向
    );
  }
}

在这个示例中,我们创建了一个包含五个不同颜色的方块(RiverflowItem)的 Riverflow 动画。每个方块都有一个起始的 animationValue,这决定了它们在动画开始时的位置。AnimationController 控制动画的持续时间和循环方式。

你可以根据需要调整 itemSpacingalignmentscrollDirection 等属性来定制动画效果。

这个示例提供了一个基础框架,你可以在此基础上进一步扩展和自定义动画效果。

回到顶部