Flutter中如何为showLicensePage添加自定义许可证?
手动添加自定义许可证到Flutter的License页面
要把无包依赖的外部代码许可证加入默认License页面,直接用LicenseRegistry.addLicense()就能实现,具体操作如下:
核心实现逻辑
addLicense()方法要求返回一个Stream<LicenseEntry>,你需要把自定义许可证文本包装成这个类型返回。可以在打开License页面的代码前临时注册,也可以在App初始化时全局注册(推荐后者,确保所有入口打开License页面都能看到)。
贴合你代码的修改示例
替换你代码里的注释部分,完整代码如下:
onPressed: () { // 注册自定义许可证 LicenseRegistry.addLicense(() async* { yield LicenseEntry( // 填写自定义标识,比如外部代码的名称 packages: ['第三方工具代码'], // 粘贴完整的许可证文本 license: '''MIT License Copyright (c) [年份] [作者/组织名] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.''', ); }); showLicensePage( context: context, applicationName: 'name', applicationVersion: 'version', applicationLegalese: 'copyright', applicationIcon: Padding( padding: const EdgeInsets.all(8), child: Image.asset("assets/icon.png", width: 48, height: 48), ), ); }
额外提示
- 若要全局生效,把
LicenseRegistry.addLicense()的代码移到main()函数里runApp()之前,这样无论从哪个入口打开License页面,都会显示这个自定义许可证。 packages参数可填任意字符串,用来标识许可证对应的内容,方便用户识别。- 许可证文本要保证完整准确,直接复制官方原版内容即可。
内容的提问来源于stack exchange,提问作者Francesco - FL
相关产品推荐
相关产品推荐

