官术网_书友最值得收藏!

Adding metadata

At the time of writing, some of the fullscreen APIs are marked as experimental in the Dart API documentation, and this is made clear in the documentation as well. By the time you read this, the fullscreen APIs should hopefully be stabilized and fully operational.

This is the normal process for new web browser features, which are eventually made stable. The adding of experimental APIs is likely to be a continuous process—it seems very unlikely that web browsers will ever be 100 percent complete! The onFullscreenChange Dart API documentation reads:

Experimental
ElementStream<Event> get onFullscreenChange
Stream of fullscreenchange events handled by this Element.

The source code for this part of the dart:html package has several annotations including the @experimental marker:

  /// Stream of `fullscreenchange` events handled by this [Element].
  @DomName('Element.onwebkitfullscreenchange')
  @DocsEditable()
  // https://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html
  @Experimental()
  ElementStream<Event> get onFullscreenChange => fullscreenChangeEvent.forElement(this);

Dart has a very flexible annotation system, and new systems can be created to add metadata to most of the components in Dart, such as classes, functions, and libraries. The pattern, as you probably already spotted, is used to start them with a @ sign followed by a call to the const constructor or a reference to a compile-time constant variable.

Creating a custom annotation

Let's create our own Temporary annotation that will mark classes as temporary to the design of the application and will allow a label to record which planned build the temporary classes are scheduled to be removed:

import 'dart:mirrors';

class Temporary {
  final String removalBuildLabel;
  const Temporary(this.removalBuildLabel);

  String toString() => removalBuildLabel;
}

@Temporary('Build1')
class TempWidgetTxt {}

@Temporary('Build2')
class TempWidgetWithGFX {}

void main() {
  ClassMirror classMirror1 = reflectClass(TempWidgetTxt);
  ClassMirror classMirror2 = reflectClass(TempWidgetWithGFX);

  List<InstanceMirror> metadata1 = classMirror1.metadata;
  print(metadata1.first.reflectee);

  List<InstanceMirror> metadata2 = classMirror2.metadata;
  print(metadata2.first.reflectee);
}

The Temporary constructor allows a label to be set. This field must be final (a single-assignment variable or field) if this class is to be used as an annotation. The main function calls reflectClass to obtain the details of the classes and then accesses this metadata and stores it in a list. The toString method allows the metadata to be the output via print:

Build1
Build2

Annotations are used extensively in the core Dart APIs, and in packages, they are used as the basis of frameworks, for example, web applications. They are not an obscure feature of the core SDK.

Translating the user interface text

My first ever professional software release did not involve any creation of code. It was a language update release for an existing package, and it was just as satisfying to mail out the CD-ROM (this was a while ago!) as something I had coded from scratch. Thankfully, it made me famous in Norway!

The translation of a software application is critical for its acceptance in some markets. This is even more true in the world of web applications. Dart has a package called intl that is used to help bring your applications to a global audience.

The presenter interface needs to be updated so that it displays the editor in French and Spanish. This will involve extracting the strings that we wish to use, obtaining translations, and integrating them back into the project.

The language will be selectable at runtime by user selection. The current OS interface language may not be the language that the user wishes to use the application in.

主站蜘蛛池模板: 蒙城县| 蕲春县| 淮南市| 通榆县| 两当县| 柳林县| 教育| 汉源县| 浦北县| 苍溪县| 汝阳县| 会昌县| 农安县| 岳普湖县| 荥经县| 政和县| 西城区| 重庆市| 蓬莱市| 色达县| 山东省| 永年县| 潮安县| 元朗区| 桐柏县| 澳门| 安仁县| 贵南县| 搜索| 连江县| 左云县| 松江区| 文登市| 湘潭县| 香港 | 康平县| 博兴县| 启东市| 车险| 铜陵市| 揭阳市|