flutter - URI 的目标不存在:'package:flutter_gen/gen_l10n/gallery_localizations.dart'
问题描述
我现在在我的项目中使用颤振画廊,这是包参考:
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
但它显示:
Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/gallery_localizations.dart'.
我在以下位置添加了 lib pubspec.yaml
:
flutter_localizations:
sdk: flutter
intl: ^0.16.1
flutter_localized_locales: ^1.1.1
并补充说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
我错过了什么吗?仍然无法正常工作,我该怎么做才能使其正常工作?这是完整的代码:
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,
),
),
),
],
);
}
}
当我运行命令时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.
解决方案
我遵循了 Flutter 官方文档(https://flutter.dev/docs/development/accessibility-and-localization/internationalization),但遇到了与您相同的问题。我首先尝试了“颤振升级”。问题仍然存在。
之后,我尝试关闭我的IDE(Android Studio)并再次打开它,问题就解决了!
推荐阅读
- c# - 如何在 Unity3D 中更准确地编辑框碰撞器?
- truncate - 在 Clickhouse 中的集群上截断表
- angular - 为 Angular 子路由添加 scrollPositionRestoration
- android - 无法将 Ionic cordova 构建到 Android 应用程序
- python - 从 Tensorflow 1.x 到 Tensorflow 2.1.0
- java - Spring Cloud 断路器 - 如何控制打开的 http 状态电路
- javascript - 如何使用javascript将表的值插入另一个表
- python - 如何使用 Pandas 和 NumPy 修复“'系列'对象是可变的,因此它们不能被散列”
- javascript - 如何在 D3.js 中垂直包装 SVG 中的文本?
- github - 如何删除通过文件上传创建的 Github 中的特定提交历史?