카테고리 없음

[Flutter] 오픈소스 라이선스 목록 보여주기

ThLee 2024. 3. 11. 13:58
728x90

결과물 

 

방법 및 코드 

1. pubspec.yaml에 라이브러리를 추가한다

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_oss_licenses: ^2.0.1

 

2. 터미널에 아래 명령어를 입력한다

1. flutter pub get
2. flutter pub run flutter_oss_licenses:generate.dart

 

명령어가 정상적으로 실행이 되면 lib > oss_licenses.dart 파일이 생긴다

 

구조는 요런식으로 되어있다

 

3. oss_licenses.dart 에 있는 Package 들을 보여줄 oss_licenses_page.dart 파일을 생성하고 코드를 짠다

class OssLicensesPage extends StatelessWidget {
  static Future<List<String>> loadLicenses() async {
    final ossKeys = List<String>.from(ossLicenses);
    return ossKeys..sort();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Align(
            alignment: Alignment.centerLeft,
            child:  CustomTextJalnanGothic(
              text: "오픈소스 라이선스",
              size:16,
            ),
          ),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              for (var i = 0; i < ossLicenses.length; i++)
                ListTile(
                  title: Text(ossLicenses[i].name),
                  // subtitle: ossLicenses[i].description != null ? Text(ossLicenses[i].description!) : null,
                  trailing: Icon(Icons.chevron_right),
                  onTap: (){
                    // 클릭하면 해당 오픈소스 라이선스 페이지로 이동
                    Navigator.of(context).push(MaterialPageRoute(builder: (context) =>
                        MiscOssLicenseSingle(
                            name: ossLicenses[i].name ?? '',
                            version: ossLicenses[i].version ?? '',
                            description: ossLicenses[i].description ?? '',
                            licenseText: ossLicenses[i].license ?? '',
                            homepage: ossLicenses[i].homepage ?? ''
                        )
                    )
                    );

                  },
                  // onTap: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => MiscOssLicenseSingle(name: ossLicenses[i].name, json: ossLicenses[i])))
                )
            ],
          ),
        )
    );
  }
}

class MiscOssLicenseSingle extends StatelessWidget {
  final String name;
  final String version;
  final String description;
  final String licenseText;
  final String homepage;


  MiscOssLicenseSingle({
    required this.name,
    required this.version,
    required this.description,
    required this.licenseText,
    required this.homepage,
  });

  String _bodyText() {
    return licenseText.split('\n').map((line) {
      if (line.startsWith('//')) line = line.substring(2);
      line = line.trim();
      return line;
    }).join('\n');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Align(
          alignment: Alignment.centerLeft,
          child:  CustomTextJalnanGothic(
            text: "오픈소스 라이선스",
            size:16,
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            ListTile(
              title: Text(name),
              subtitle: Text('version : $version'),
            ),
            if (description != null)
              Padding(
                  padding: const EdgeInsets.only(top: 12.0, left: 12.0, right: 12.0),
                  child: Text(description)),
            const Divider(),
            Padding(
              padding: const EdgeInsets.only(top: 12.0, left: 12.0, right: 12.0),
              child: Text(
                  _bodyText(),
                  style: Theme.of(context).textTheme.bodyText2
              ),
            ),
            const Divider(),
            ListTile(
              title: Text('Homepage'),
                subtitle: Text(homepage),
                onTap: () async {
                  if (await canLaunch(homepage)) {
                    await launch(homepage);
                  } else {
                    throw 'Could not launch $homepage';
                  }
                }
            ),
          ],
        ),
      ),
    );
  }
}

 

 

직접만든 위젯도 껴있고, 복붙해도 안돌아갈 수 있다

 

간략하게 설명을 덧붙이자면 

 

OssLicensesPage Class에서 아래와 같이 목록들을 보여준다. 

 

하나씩 클릭하게 되면, MiscOssLicenseSingle Class에서 자세한 정보를 보여주게 된다 

그리고 제일 밑에 홈페이지 링크도 있다 


 

참고 블로그 : https://ksrapp.tistory.com/26

 

 

728x90