我现在在我的项目中使用 flutter gallary,这是包参考:
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
Run Code Online (Sandbox Code Playgroud)
但它显示:
Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/gallery_localizations.dart'.
Run Code Online (Sandbox Code Playgroud)
我在pubspec.yaml:
flutter_localizations:
sdk: flutter
intl: ^0.16.1
flutter_localized_locales: ^1.1.1
Run Code Online (Sandbox Code Playgroud)
并补充说l10n.yaml:
template-arb-file: intl_en.arb
output-localization-file: gallery_localizations.dart
output-class: GalleryLocalizations
preferred-supported-locales:
- en
use-deferred-loading: false
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?仍然无法正常工作,我该怎么做才能使其正常工作?这是完整的代码:
import 'package:flutter/material.dart';
import 'package:animations/animations.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
enum BottomNavigationDemoType {
withLabels,
withoutLabels,
}
class BottomNavigationDemo extends StatefulWidget {
const BottomNavigationDemo({Key key, @required this.type}) : super(key: key);
final BottomNavigationDemoType type;
@override
_BottomNavigationDemoState createState() => _BottomNavigationDemoState();
}
class _BottomNavigationDemoState extends State<BottomNavigationDemo> {
int _currentIndex = 0;
String _title(BuildContext context) {
switch (widget.type) {
case BottomNavigationDemoType.withLabels:
return GalleryLocalizations.of(context)
.demoBottomNavigationPersistentLabels;
case BottomNavigationDemoType.withoutLabels:
return GalleryLocalizations.of(context)
.demoBottomNavigationSelectedLabel;
}
return '';
}
@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
final textTheme = Theme.of(context).textTheme;
var bottomNavigationBarItems = <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: const Icon(Icons.add_comment),
label: GalleryLocalizations.of(context).bottomNavigationCommentsTab,
),
BottomNavigationBarItem(
icon: const Icon(Icons.calendar_today),
label: GalleryLocalizations.of(context).bottomNavigationCalendarTab,
),
BottomNavigationBarItem(
icon: const Icon(Icons.account_circle),
label: GalleryLocalizations.of(context).bottomNavigationAccountTab,
),
BottomNavigationBarItem(
icon: const Icon(Icons.alarm_on),
label: GalleryLocalizations.of(context).bottomNavigationAlarmTab,
),
BottomNavigationBarItem(
icon: const Icon(Icons.camera_enhance),
label: GalleryLocalizations.of(context).bottomNavigationCameraTab,
),
];
if (widget.type == BottomNavigationDemoType.withLabels) {
bottomNavigationBarItems = bottomNavigationBarItems.sublist(
0, bottomNavigationBarItems.length - 2);
_currentIndex =
_currentIndex.clamp(0, bottomNavigationBarItems.length - 1).toInt();
}
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text(_title(context)),
),
body: Center(
child: PageTransitionSwitcher(
child: _NavigationDestinationView(
// Adding [UniqueKey] to make sure the widget rebuilds when transitioning.
key: UniqueKey(),
item: bottomNavigationBarItems[_currentIndex],
),
transitionBuilder: (child, animation, secondaryAnimation) {
return FadeThroughTransition(
child: child,
animation: animation,
secondaryAnimation: secondaryAnimation,
);
},
),
),
bottomNavigationBar: BottomNavigationBar(
showUnselectedLabels:
widget.type == BottomNavigationDemoType.withLabels,
items: bottomNavigationBarItems,
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
selectedFontSize: textTheme.caption.fontSize,
unselectedFontSize: textTheme.caption.fontSize,
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
selectedItemColor: colorScheme.onPrimary,
unselectedItemColor: colorScheme.onPrimary.withOpacity(0.38),
backgroundColor: colorScheme.primary,
),
);
}
}
class _NavigationDestinationView extends StatelessWidget {
_NavigationDestinationView({Key key, this.item}) : super(key: key);
final BottomNavigationBarItem item;
@override
Widget build(BuildContext context) {
return Stack(
children: [
ExcludeSemantics(
child: Center(
child: Padding(
padding: const EdgeInsets.all(16),
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.asset(
'assets/demos/bottom_navigation_background.png',
package: 'flutter_gallery_assets',
),
),
),
),
),
Center(
child: IconTheme(
data: const IconThemeData(
color: Colors.white,
size: 80,
),
child: Semantics(
label: GalleryLocalizations.of(context)
.bottomNavigationContentPlaceholder(
item.label,
),
child: item.icon,
),
),
),
],
);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行命令时flutter clean && flutter run ,显示结果:
[dolphin@MiWiFi-R4CM-srv]~/AndroidStudioProjects/Cruise% flutter clean && flutter run
Attempted to generate localizations code without having the flutter: generate flag turned on.
Check pubspec.yaml and ensure that flutter: generate: true has been added and rebuild the project. Otherwise, the localizations source code will not be
importable.
Generating synthetic localizations package has failed.
Run Code Online (Sandbox Code Playgroud)
Sha*_*nem 71
除了@Sleepingisimportant 答案之外,您可以重新启动“Dart Analysis Server”,问题将得到解决。
这个按钮位于 Android Studio 的 Dart Analysis 选项卡中,我猜这意味着它也在 Intelij 上。
Moh*_*eda 25
我只是在添加 l10n.yaml 后解决了这个问题,然后执行了以下操作:
扑干净
扑扑酒吧得到
小智 19
如果您正在使用包,flutter_gen则需要将其从 pubscpec.yaml 中删除以解决冲突。
尝试跑flutter update-packages进去~/flutter/packages/flutter。
flutter update-packages\nRun Code Online (Sandbox Code Playgroud)\n或者使用以下flutter upgrade命令更新 Flutter SDK:
flutter upgrade\nRun Code Online (Sandbox Code Playgroud)\n此命令获取当前 Flutter 通道上可用的最新版本的 Flutter SDK\xe2\x80\x99s。
\n有关如何升级 Flutter SDK 或切换 Flutter 通道的更多信息:https://flutter.dev/docs/development/tools/sdk/upgrading
\n这将修复你的import \'package:flutter_gen/gen_l10n/gallery_localizations.dart\';问题。
小智 8
我遵循了 Flutter 官方文档 ( https://flutter.dev/docs/development/accessibility-and-localization/internationalization ),但遇到了和你一样的问题。我首先尝试了“颤振升级”。问题仍然存在。
之后,我尝试关闭我的IDE(Android studio)并再次打开它,问题就解决了!
在 pubspec.yaml 的底部,您应该将generate 设置为 true...
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
generate: true
Run Code Online (Sandbox Code Playgroud)
我通过将这两行添加到 pubspec.yaml 文件来使导入工作:
cupertino_icons: ^0.1.3
flutter_gallery: ^2.4.0+20400
Run Code Online (Sandbox Code Playgroud)
第一行实际上是替换原始的 cupertino-icons 依赖项(版本 1.0.0),而 flutter gallery 依赖项需要不同的版本(0.1.3)。
然后用“flutter pub get”更新导入
该网站列出了所有可能的 flutter_gallery 导入。
| 归档时间: |
|
| 查看次数: |
6101 次 |
| 最近记录: |