[Flutter] pub.dev package License 표시

sidcode·2024년 6월 8일

Flutter 패키지의 라이센스를 표시하는 방법에는 몇 가지가 있습니다. 여기서는 Flutter 앱 내에서 패키지 라이센스를 표시하는 방법을 소개합니다. 일반적으로 앱 내에서 라이센스를 표시하는 가장 좋은 방법은 "About" 페이지에 포함시키는 것입니다.

Step 1: package_info_plus 패키지 설치
먼저 package_info_plus 패키지를 설치합니다. 이 패키지를 사용하여 앱의 패키지 정보를 가져올 수 있습니다.

pubspec.yaml 파일에 다음을 추가합니다:

dependencies:
  flutter:
    sdk: flutter
  package_info_plus: ^1.0.6 

그런 다음 터미널에서 다음 명령을 실행하여 패키지를 설치합니다:

flutter pub get

Step 2: showLicensePage 사용
Flutter는 기본적으로 라이센스를 표시할 수 있는 showLicensePage 메서드를 제공합니다. 이 메서드는 앱 내에서 사용된 모든 패키지의 라이센스를 표시하는 화면을 제공합니다.

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

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

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter License Demo',
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () async {
            PackageInfo packageInfo = await PackageInfo.fromPlatform();
            showAboutDialog(
              context: context,
              applicationName: packageInfo.appName,
              applicationVersion: packageInfo.version,
              applicationLegalese: '© 2024 Your Company',
              children: [
                GestureDetector(
                  onTap: () {
                    showLicensePage(
                      context: context,
                      applicationName: packageInfo.appName,
                      applicationVersion: packageInfo.version,
                    );
                  },
                  child: Text(
                    'View Licenses',
                    style: TextStyle(color: Colors.blue),
                  ),
                ),
              ],
            );
          },
          child: Text('Show Licenses'),
        ),
      ),
    );
  }
}

Step 3: showLicensePage 커스터마이징
showLicensePage는 기본적으로 앱 내의 모든 패키지의 라이센스를 표시합니다. 이 메서드를 호출하면 LicensePage 위젯이 나타나며, 이 위젯을 커스터마이징할 수도 있습니다.

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

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

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter License Demo',
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () async {
            PackageInfo packageInfo = await PackageInfo.fromPlatform();
            showAboutDialog(
              context: context,
              applicationName: packageInfo.appName,
              applicationVersion: packageInfo.version,
              applicationLegalese: '© 2024 Your Company',
              children: [
                GestureDetector(
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => CustomLicensePage(
                          appName: packageInfo.appName,
                          appVersion: packageInfo.version,
                        ),
                      ),
                    );
                  },
                  child: Text(
                    'View Licenses',
                    style: TextStyle(color: Colors.blue),
                  ),
                ),
              ],
            );
          },
          child: Text('Show Licenses'),
        ),
      ),
    );
  }
}

class CustomLicensePage extends StatelessWidget {
  final String appName;
  final String appVersion;

  const CustomLicensePage({
    Key? key,
    required this.appName,
    required this.appVersion,
  }) : super(key: key);

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Licenses'),
      ),
      body: LicensePage(
        applicationName: appName,
        applicationVersion: appVersion,
        applicationLegalese: '© 2024 Your Company',
      ),
    );
  }
}

showAboutDialog는 앱의 정보와 함께 라이센스를 표시할 수 있는 링크를 제공하며,
showLicensePage는 실제 라이센스 내용을 표시하는 페이지를 제공합니다.

profile
I will do, what i want!!

0개의 댓글