Flutter片尾字幕展示插件end_credits的使用

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

Flutter片尾字幕展示插件end_credits的使用

end_credits 插件可以帮助你在 Flutter 应用中展示简单的片尾字幕。以下是该插件的详细说明和使用方法。

插件信息

pub end_credits codecov License: MIT style: very good analysis

展示在你的 Flutter 应用中的一个简单的片尾字幕屏幕。

开始使用

要开始使用 end_credits 插件,你需要在你的 Flutter 项目中添加它到 pubspec.yaml 文件中:

dependencies:
  end_credits: ^2.0.0

然后运行 flutter pub get 来安装依赖。

接下来,你可以通过以下方式使用 EndCredits 小部件:

EndCredits(
    sections,
    backgroundColor: Colors.black,
    curve: Curves.linear,
    delay: Duration(seconds: 1),
    speedFactor: normalSpeedFactor,
);

这里你需要传递一个数组 sections,每个元素是一个 Section 对象:

Section(title: 'Cast', roles: [
    Role(name: 'Role 1', crew: [Responsable('John Doe')])
])

你还可以自定义各个部分的文字样式,例如:

  • Section
  • Crew
  • Role

该插件提供了三种预定义的速度因子,但你也可以传递一个自定义的 double 值:

  • 慢速
  • 正常速度
  • 快速

完整示例

以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 end_credits 插件:

import 'package:end_credits/end_credits.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,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: EndCredits([
        Section(
          title: 'Cast',
          roles: [
            Role(name: 'Role 1', crew: [Responsable('John Doe')]),
            Role(name: 'Role 2', crew: [Responsable('John Doe')]),
            Role(name: 'Role 3', crew: [Responsable('John Doe')]),
            Role(name: 'Role 4', crew: [Responsable('John Doe')]),
            Role(name: 'Role 5', crew: [Responsable('John Doe')]),
          ],
        ),
        Section(
          title: 'Producers',
          roles: [
            Role(name: 'Executive producer', crew: [
              Responsable('John Doe'),
              Responsable('John Doe'),
              Responsable('John Doe')
            ]),
            Role(name: 'Producer', crew: [
              Responsable('John Doe'),
              Responsable('John Doe'),
              Responsable('John Doe')
            ])
          ],
        ),
        Section(
          title: 'Other',
          roles: [
            Role(name: 'Role', crew: [
              Responsable('John Doe'),
              Responsable('John Doe'),
              Responsable('John Doe'),
              Responsable('John Doe')
            ])
          ],
        )
      ]),
    );
  }
}

更多关于Flutter片尾字幕展示插件end_credits的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter片尾字幕展示插件end_credits的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用end_credits插件来展示片尾字幕的一个示例代码案例。end_credits插件可以帮助你轻松地在应用中实现片尾字幕的滚动展示。

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

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

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

接下来,你可以在你的Flutter项目中创建一个简单的页面来展示片尾字幕。以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter End Credits Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: EndCreditsDemoPage(),
    );
  }
}

class EndCreditsDemoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('End Credits Demo'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            _showEndCredits(context);
          },
          child: Text('Show End Credits'),
        ),
      ),
    );
  }

  void _showEndCredits(BuildContext context) {
    // 定义片尾字幕的列表
    List<String> credits = [
      "Director: John Doe",
      "Producer: Jane Smith",
      "Screenplay: Alice Johnson",
      "Cinematography: Bob Brown",
      "Editing: Charlie White",
      // ... 更多字幕
    ];

    // 使用EndCredits.show方法展示片尾字幕
    EndCredits.show(
      context: context,
      credits: credits,
      // 可选参数:设置字体大小、滚动速度、背景颜色等
      textStyle: TextStyle(fontSize: 20, color: Colors.white),
      speed: 30, // 滚动速度,数值越大速度越快
      backgroundColor: Colors.black.withOpacity(0.8),
      padding: EdgeInsets.all(16),
      onDismissed: () {
        // 字幕展示完毕后执行的回调
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('End Credits Finished')),
        );
      },
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个按钮。点击按钮时,会调用_showEndCredits方法来展示片尾字幕。EndCredits.show方法接受多个参数,包括上下文、字幕列表、文本样式、滚动速度、背景颜色和填充等。

你可以根据实际需求调整这些参数,以达到最佳的展示效果。

注意:请确保你使用的是最新版本的end_credits插件,因为插件的API可能会随着版本的更新而有所变化。你可以在pub.dev网站上查看最新的文档和示例。

回到顶部