Flutter电视频道展示插件tv_channel_widget的使用

Flutter电视频道展示插件tv_channel_widget的使用


ChannelWidget 📺

ChannelWidget 是一个用于显示电视频道及其相应节目的优雅且易于使用的包。受流行的流媒体服务如 Amazon Prime 和 Jio Tv 的启发,此包完全用 Dart 开发,并支持所有平台。凭借其可定制性和布局选项,ChannelWidget 可以让您完全控制应用的外观和感觉。

没有自动检测时间

有自动检测时间

特性 🚀

  • 显示电视频道及其相应节目。
  • 使用 ChannelBuilder 和 ShowBuilder 回调自定义外观。
  • 选项显示时间在小部件上方。
  • 选项移动到当前日期和时间。
  • 可定制的布局选项,如头部宽度、项目高度、垂直填充和计时器行高度。
  • 选项禁用水平滚动。

安装 🔧

在您的 pubspec.yaml 文件中添加 tv_channel_widget 作为依赖项:

dependencies:
  tv_channel_widget: '^0.0.1'

然后在终端中运行 flutter packages get 以便获取该包。

导入 Channel Widget:

import 'package:tv_channel_widget/tv_channel_widget.dart';

使用 🎬

创建包含 TvChannel 类的列表

首先,创建一个 TvChannel 对象及其相应 ShowItem 的列表:

List<TvChannel> showsList = [
  TvChannel(
    channelName: 'Discovery+',
    showItems: [
      ShowItem(
        showName: 'Gold Rush',
        showStartTime: DateTime(2023, 1, 6, 00, 0),
        showEndTime: DateTime(2023, 1, 6, 1, 0)),
      ShowItem(
        showName: 'Food Factory',
        showStartTime: DateTime(2023, 1, 6, 1, 0),
        showEndTime: DateTime(2023, 1, 6, 1, 30)),
      // 其他节目...
    ]
  ),
  // 其他频道...
];

创建 ChannelWidget

然后,使用 ChannelWidget 来显示频道和节目列表:

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

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

class _MyHomePageState extends State<MyHomePage> {
  List<TvChannel> showsList = [];

  [@override](/user/override)
  void initState() {
    super.initState();

    showsList = [
      // 初始化频道列表...
    ];
    showsList.shuffle(); // 随机打乱列表
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: const Text('Channel Widget'),
          backgroundColor: const Color(0xFFFF4500).withOpacity(0.8)),
      body: ChannelWidget(
        channelShows: showsList,
        moveToCurrentTime: true,
        itemHeight: 80,
        showTime: true,
        verticalPadding: 2,
        channelBuilder: (context, index) {
          final item = showsList[index];
          return Container(
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5),
                color: const Color(0xFFFF4500).withOpacity(0.8)),
            child: Center(
                child: Text(
              item.channelName,
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 18,
                  fontWeight: FontWeight.w400),
            )),
          );
        },
        showsBuilder: (context, show) {
          final channel = show;
          return Padding(
            padding: const EdgeInsets.only(left: 5),
            child: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5),
                color: Colors.black12,
              ),
              padding: const EdgeInsets.all(8.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(channel.showName,
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: const TextStyle(
                          fontSize: 20, fontWeight: FontWeight.w500)),
                  Text(
                      "${channel.showStartTime.difference(channel.showEndTime).inMinutes.abs()} Min",
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: const TextStyle(
                          fontSize: 14, fontWeight: FontWeight.w400)),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}

更多关于Flutter电视频道展示插件tv_channel_widget的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter电视频道展示插件tv_channel_widget的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中使用tv_channel_widget插件来展示电视频道列表的代码示例。这个示例假设你已经安装了tv_channel_widget插件,并且已经配置好了Flutter开发环境。

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

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

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

接下来,在你的Flutter项目中创建一个新的Dart文件(例如channel_list.dart),并编写以下代码来展示电视频道列表:

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

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

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

class ChannelListScreen extends StatelessWidget {
  final List<Channel> channels = [
    Channel(
      id: '1',
      title: 'CNN',
      imageUrl: 'https://example.com/cnn_logo.png',  // 替换为实际图片URL
      description: 'Breaking News, Latest News and Current News from CNN.com.',
    ),
    Channel(
      id: '2',
      title: 'BBC',
      imageUrl: 'https://example.com/bbc_logo.png',  // 替换为实际图片URL
      description: 'BBC News provides trusted World and UK news as well as local and regional perspectives.',
    ),
    // 添加更多频道...
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('TV Channels'),
      ),
      body: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 10,
          mainAxisSpacing: 10,
        ),
        itemCount: channels.length,
        itemBuilder: (context, index) {
          return ChannelCard(
            channel: channels[index],
          );
        },
      ),
    );
  }
}

class Channel {
  final String id;
  final String title;
  final String imageUrl;
  final String description;

  Channel({
    required this.id,
    required this.title,
    required this.imageUrl,
    required this.description,
  });
}

class ChannelCard extends StatelessWidget {
  final Channel channel;

  ChannelCard({required this.channel});

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.network(
            channel.imageUrl,
            fit: BoxFit.cover,
            height: 100,
            width: 150,
          ),
          Text(
            channel.title,
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          ),
          Text(
            channel.description,
            style: TextStyle(fontSize: 14, color: Colors.grey),
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
          ),
        ],
      ),
    );
  }
}

// 注意:tv_channel_widget插件可能提供了更高级的自定义和交互功能,
// 这只是一个简单的示例,展示了如何使用GridView和自定义Card来展示频道列表。
// 如果tv_channel_widget有特定的API或组件,你应该参考其文档进行更深入的集成。

在这个示例中,我们创建了一个简单的Flutter应用,它使用GridView来展示一个电视频道列表。每个频道都使用一个自定义的ChannelCard小部件来显示频道的图片、标题和描述。

请注意,tv_channel_widget插件可能提供了更高级的自定义和交互功能,比如滚动、选择频道、播放预览等。你应该参考tv_channel_widget的官方文档来了解如何充分利用这些功能。如果插件提供了特定的组件或API,你可能需要替换或调整上面的代码来适应这些功能。

回到顶部