tree: f178ad7699e81be200b029534405b496bf968e6b [path history] [tgz]
  1. freeform.tmpl
  2. freeform_no_null_safety.tmpl
  4. stateful_widget.tmpl
  5. stateful_widget_cupertino.tmpl
  6. stateful_widget_cupertino_no_null_safety.tmpl
  7. stateful_widget_cupertino_page_scaffold.tmpl
  8. stateful_widget_cupertino_page_scaffold_no_null_safety.tmpl
  9. stateful_widget_cupertino_ticker.tmpl
  10. stateful_widget_cupertino_ticker_no_null_safety.tmpl
  11. stateful_widget_material.tmpl
  12. stateful_widget_material_no_null_safety.tmpl
  13. stateful_widget_material_ticker.tmpl
  14. stateful_widget_material_ticker_no_null_safety.tmpl
  15. stateful_widget_no_null_safety.tmpl
  16. stateful_widget_restoration.tmpl
  17. stateful_widget_restoration_no_null_safety.tmpl
  18. stateful_widget_scaffold.tmpl
  19. stateful_widget_scaffold_center.tmpl
  20. stateful_widget_scaffold_center_freeform_state.tmpl
  21. stateful_widget_scaffold_center_freeform_state_no_null_safety.tmpl
  22. stateful_widget_scaffold_center_no_null_safety.tmpl
  23. stateful_widget_scaffold_no_null_safety.tmpl
  24. stateful_widget_ticker.tmpl
  25. stateful_widget_ticker_no_null_safety.tmpl
  26. stateless_widget.tmpl
  27. stateless_widget_cupertino.tmpl
  28. stateless_widget_cupertino_no_null_safety.tmpl
  29. stateless_widget_cupertino_page_scaffold.tmpl
  30. stateless_widget_cupertino_page_scaffold_no_null_safety.tmpl
  31. stateless_widget_material.tmpl
  32. stateless_widget_material_no_null_safety.tmpl
  33. stateless_widget_no_null_safety.tmpl
  34. stateless_widget_scaffold.tmpl
  35. stateless_widget_scaffold_center.tmpl
  36. stateless_widget_scaffold_center_no_null_safety.tmpl
  37. stateless_widget_scaffold_no_null_safety.tmpl

Creating Code Snippets

In general, creating application snippets can be accomplished with the following syntax inside of the dartdoc comment for a Flutter class/variable/enum/etc.:

/// {@tool snippet --template=stateful_widget}
/// Any text outside of the code blocks will be accumulated and placed at the
/// top of the snippet box as a description. Don't try and say "see the code
/// above" or "see the code below", since the location of the description may
/// change in the future. You can use dartdoc [Linking] in the description, and
/// __Markdown__ too.
/// ```dart preamble
/// class Foo extends StatelessWidget {
///   const Foo({this.value = ''});
///   String value;
///   @override
///   Widget build(BuildContext context) {
///     return Text(value);
///   }
/// }
/// ```
/// This will get tacked on to the end of the description above, and shown above
/// the snippet.  These two code blocks will be separated by `///...` in the
/// short version of the snippet code sample.
/// ```dart
/// String myValue = 'Foo';
/// @override
/// Widget build(BuildContext) {
///   return const Foo(myValue);
/// }
/// ```
/// {@end-tool}

This will result in the template having the section that‘s inside “```dart” interpolated into the template’s stateful widget's state object body.

For other sections of the template, the interpolation occurs by appending the string that comes after code- into the code block. For example, the stateful_widget template contains {{code-imports}}. To interpolate code into {{code-imports}}, you would have to do add the following:

/// ```dart imports
/// import 'package:flutter/rendering.dart';
/// ```

All code within a code block in a snippet needs to be able to be run through dartfmt without errors, so it needs to be valid code (This shouldn't be an additional burden, since all code will also be compiled to be sure it compiles).

Available Templates

The templates available for use as an argument to the snippets tool are as follows:

  • freeform : This is a simple template for which you provide everything. It has no code of its own, just the sections for imports, main, and preamble. You must provide the main section to have a main().

WidgetsApp Templates

These templates create a WidgetsApp that encloses the snippet widget. These templates import the widgets library.

  • stateful_widget : The default code block will be placed as the body of the State object of a StatefulWidget subclass. Because the default code block is placed as the body of a stateful widget, you will need to implement the build() method and any state variables. It also has a preamble in addition to the default code block, which will be placed at the top level of the Dart file, so bare method calls are not allowed in the preamble. The default code block is placed as the body of a stateful widget, so you will need to implement the build() method, and any state variables. It also has an imports section to import additional packages. Please only import things that are part of Flutter or part of default dependencies for a flutter create project.

  • stateful_widget_ticker : Identical to the stateful_widget template, with the addition of the TickerProviderStateMixin class, enabling easy generation of animated samples.

  • stateful_widget_restoration : Similar to the stateful_widget template, but the widget also imports RestorationMixin and has a restorationId field which it uses to implement the restorationId getter on the State.

  • stateless_widget : Identical to the stateful_widget template, except that the default code block is inserted as a method (which should be the build method) in a StatelessWidget. The @override before the build method is added by the template so must be omitted from the sample code.

MaterialApp Templates

These templates follow the same conventions as the WidgetsApp templates above but use a MaterialApp instead. These templates import the material library.

CupertinoApp Templates

These templates follow the same conventions as the WidgetsApp templates above but use a CupertinoApp instead. These templates import the Cupertino library.