Flutter短视频录制按钮插件shorts_video_record_button的使用

Flutter短视频录制按钮插件shorts_video_record_button的使用

我们的插件提供了一个时尚且现代化的视频录制按钮,其设计风格与流行的社交媒体平台(如Instagram)上的按钮非常相似。

示例

示例

示例2

使用说明

RecordButton(  
  showLabel: true,  // 是否显示标签
  labelColor: Colors.black12,  // 标签颜色
  trackColor: Colors.grey.shade300,  // 进度条颜色
  fillColor: Colors.deepOrange,  // 填充颜色
  buttonColor: Colors.transparent,  // 按钮颜色
  gradients: const [  // 渐变颜色列表
    Color(0xff405de6),  
    Color(0xff5851db),  
    Color(0xff833ab4),  
    Color(0xffc13584),  
    Color(0xffe1306c),  
    Color(0xfffd1d1d),  
  ],  
  onPlay: () {  // 开始录制时的回调函数
    // Do whatever you want after play
  },  
  onStop: (int value) {  // 停止录制时的回调函数
    // Do whatever you want after stop
  },  
  onComplete: (int value) {  // 录制完成时的回调函数
    // Do whatever you want after complete
  },  
  seconds: 30,  // 录制时间限制
);

完整示例

import 'dart:developer';

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

void main() {
  runApp(const ShortsRecordButton());
}

class ShortsRecordButton extends StatelessWidget {
  const ShortsRecordButton({super.key});

  // This widget is the root of your application.
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: SizedBox(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          child: RecordButton(
            showLabel: true,
            labelColor: Colors.black12,
            trackColor: Colors.grey.shade300,
            fillColor: Colors.deepOrange,
            buttonColor: Colors.transparent,
            gradients: const [
              Color(0xff405de6),
              Color(0xff5851db),
              Color(0xff833ab4),
              Color(0xffc13584),
              Color(0xffe1306c),
              Color(0xfffd1d1d),
            ],
            onPlay: () {
              // 开始录制时的回调函数
              log("ON PLAY PRESSED=====");
            },
            onStop: (int value) {
              // 停止录制时的回调函数
              log("ON STOP PRESSED=====");
            },
            onComplete: (int value) {
              // 录制完成时的回调函数
              log("ON COMPLETED===== $value");
            },
            seconds: 3,
          ),
        ),
      ),
    );
  }
}

const backgroundGradient = LinearGradient(
  begin: Alignment.topCenter,
  end: Alignment.bottomCenter,
  colors: [
    Color(0xFFF1D4F5),
    Color(0xFFF9F4FA),
  ],
);

class ShortsRecordSample extends StatelessWidget {
  const ShortsRecordSample({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          body: Container(
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            decoration: const BoxDecoration(gradient: backgroundGradient),
            child: Stack(
              children: [
                Positioned(
                  bottom: MediaQuery.of(context).size.height * 0.05,
                  child: SizedBox(
                    // height: MediaQuery.of(context).size.height * 0.2,
                    width: MediaQuery.of(context).size.width,
                    child: Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            IconButton(
                              icon: const Icon(
                                Icons.camera,
                                color: Colors.black,
                                weight: 30,
                                size: 30,
                              ),
                              onPressed: () {
                                // Handle the button press
                                // toggleCameraDirection();
                              },
                            ),
                            RecordButton(
                                height: 70,
                                width: 70,
                                seconds: 10,
                                showLabel: true,
                                labelColor: Colors.yellow,
                                trackColor: Colors.grey.shade300,
                                // fillColor: Colors.deepOrange,
                                gradients: const [Colors.deepPurple, Colors.pink, Colors.red],
                                onPlay: () {
                                },
                                onStop: (value) {
                                },
                                onComplete: (value) {
                                }),
                            IconButton(
                              icon: const Icon(
                                Icons.cameraswitch,
                                color: Colors.black,
                                weight: 30,
                                size: 30,
                              ),
                              onPressed: () {
                                // toggleCameraDirection();
                              },
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter短视频录制按钮插件shorts_video_record_button的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter短视频录制按钮插件shorts_video_record_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中集成和使用shorts_video_record_button插件的详细代码示例。shorts_video_record_button是一个用于短视频录制的按钮插件,可以方便地集成到你的Flutter应用中。

步骤1:添加依赖

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

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

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

步骤2:导入插件

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

import 'package:shorts_video_record_button/shorts_video_record_button.dart';

步骤3:使用插件

下面是一个完整的示例,展示了如何在Flutter应用中使用shorts_video_record_button插件:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Shorts Video Record Button Example'),
        ),
        body: Center(
          child: VideoRecordButtonExample(),
        ),
      ),
    );
  }
}

class VideoRecordButtonExample extends StatefulWidget {
  @override
  _VideoRecordButtonExampleState createState() => _VideoRecordButtonExampleState();
}

class _VideoRecordButtonExampleState extends State<VideoRecordButtonExample> {
  late ShortsVideoRecordController controller;

  @override
  void initState() {
    super.initState();
    controller = ShortsVideoRecordController(
      maxDuration: Duration(seconds: 10),  // 最大录制时长
      onResult: (result) {
        // 录制完成后的回调
        if (result != null && result.path != null) {
          print("录制完成,视频路径: ${result.path}");
        }
      },
      onError: (error) {
        // 录制出错时的回调
        print("录制出错: $error");
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return ShortsVideoRecordButton(
      controller: controller,
      buttonSize: 100,
      iconColor: Colors.white,
      backgroundColor: Colors.red,
      rippleColor: Colors.grey.withAlpha(128),
    );
  }

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

解释

  1. 依赖添加:在pubspec.yaml文件中添加shorts_video_record_button依赖。
  2. 导入插件:在Dart文件中导入shorts_video_record_button
  3. 创建控制器:在initState方法中初始化ShortsVideoRecordController,并设置最大录制时长、录制完成后的回调和录制出错时的回调。
  4. 使用按钮:在build方法中使用ShortsVideoRecordButton,并传入控制器和其他样式参数(如按钮大小、图标颜色、背景颜色和波纹颜色)。
  5. 释放资源:在dispose方法中释放控制器资源。

这样,你就可以在你的Flutter应用中集成并使用shorts_video_record_button插件进行短视频录制了。记得根据实际需求调整参数和回调逻辑。

回到顶部