1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Flutter] YouTubeのサムネイルを表示させたい

Last updated at Posted at 2022-10-06

YouTubeのサムネイルを載せたいな!と思ったら、
下記のパッケージを使うと簡単に実現できるので便利。
https://pub.dev/packages/youtube

インストール

pubspec.yamlに記載して、Pub get

dependencies:
  youtube: ^1.0.1

使い方

importで呼び出します

import 'package:youtube/youtube_thumbnail.dart';

1行だけでサムネイルを表示させることができます

Image.network(YoutubeThumbnail(youtubeId: 'oR7lofyBj7k').small())

🔖 youtubeId・・・https://www.youtube.com/watch?v=?????????? の ??????????部分を指定する
🔖 .small()・・・ サムネイルの表示サイズを指定(下記5つの中から選ぶ)。

サイズ 指定方法
1280x720 .hd()
640x480 .standard()
480x360 .hq()
320x180 .mq()
120x90 .defaults()

応用

サムネイルをタップした時にYouTubeリンクへ飛ばしたい場合は・・・

InkWell(
  onTap: () async {
    await launchUrlString("https://www.youtube.com/shorts/oR7lofyBj7k");
  },
  child: Image.network(YoutubeThumbnail(youtubeId: 'oR7lofyBj7k').small()),
)

デモ

環境
macOS Monterey 11.3.1
Flutter 3.3.1
Dart 2.18.0
youtube: 1.0.1

IMG_2270.JPG
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:youtube/youtube_thumbnail.dart';

class Music extends StatefulWidget {
  const Music({Key? key}) : super(key: key);
  @override
  State<Music> createState() => _MusicState();
}
class _MusicState extends State<Music> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            const Padding(
              padding: EdgeInsets.only(top: 50),
              child: Text(
                "Music",
                style: TextStyle(
                  decoration: TextDecoration.underline,
                  decorationStyle: TextDecorationStyle.double,
                  fontSize: 48,
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top:30, right: 10, left: 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  InkWell(
                    onTap: () async {
                      await launchUrlString("https://www.youtube.com/shorts/oR7lofyBj7k");
                    },
                    child: Image.network(YoutubeThumbnail(youtubeId: 'oR7lofyBj7k').small()),
                  ),
                  const Padding(
                    padding: EdgeInsets.only(left:10),
                    child: FittedBox(
                      fit: BoxFit.fitWidth,
                      child: Text(
                        "2020/3/某日\nコロナ初期の星野源さんの『うちで踊ろう』"
                            "\n私も影響されてキーボードでセッションに参加",
                        style: TextStyle(fontSize: 12),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?