• Home
Name
Date
Size
#Lines
LOC

..--

assets/12-May-2024-

config/12-May-2024-402315

lib/12-May-2024-519467

test/12-May-2024-199170

README.mdD12-May-20244.6 KiB136105

pubspec.yamlD12-May-20248.1 KiB10289

README.md

1# Dartdoc Generation
2
3The Flutter API documentation contains code blocks that help provide
4context or a good starting point when learning to use any of Flutter's APIs.
5
6To generate these code blocks, Flutter uses dartdoc tools to turn documentation
7in the source code into API documentation, as seen on https://api.flutter.dev/.
8
9## Table of Contents
10
11- [Types of code blocks](#types-of-code-blocks)
12  - [Sample tool](#sample-tool)
13  - [Snippet tool](#snippet-tool)
14- [Skeletons](#skeletons)
15- [Test Doc Generation Workflow](#test-doc-generation-workflow)
16## Types of code blocks
17
18### Sample Tool
19
20![Code sample image](assets/code_sample.png)
21
22The code `sample` tool generates a block containing a description and example
23code. Here is an example of the code `sample` tool in use:
24
25```dart
26/// {@tool sample}
27///
28/// If the avatar is to have an image, the image should be specified in the
29/// [backgroundImage] property:
30///
31/// ```dart
32/// CircleAvatar(
33///   backgroundImage: NetworkImage(userAvatarUrl),
34/// )
35/// ```
36/// {@end-tool}
37```
38
39This will generate sample code that can be copied to the clipboard and added
40to existing applications.
41
42This uses the skeleton for [sample](config/skeletons/sample.html) snippets.
43
44### Snippet Tool
45
46![Code snippet image](assets/code_snippet.png)
47
48The code `snippet` tool can expand sample code into full Flutter applications.
49These sample applications can be directly copied and used to demonstrate the
50API's functionality in a sample application:
51
52```dart
53/// {@tool snippet --template=stateless_widget_material}
54/// This example shows how to make a simple [FloatingActionButton] in a
55/// [Scaffold], with a pink [backgroundColor] and a thumbs up [Icon].
56///
57/// ```dart
58/// Widget build(BuildContext context) {
59///   return Scaffold(
60///     appBar: AppBar(
61///       title: Text('Floating Action Button Sample'),
62///     ),
63///     body: Center(
64///       child: Text('Press the button below!')
65///     ),
66///     floatingActionButton: FloatingActionButton(
67///       onPressed: () {
68///         // Add your onPressed code here!
69///       },
70///       child: Icon(Icons.thumb_up),
71///       backgroundColor: Colors.pink,
72///     ),
73///   );
74/// }
75/// ```
76/// {@end-tool}
77```
78
79This uses the skeleton for [application](config/skeletons/application.html)
80snippets.
81
82Code `snippets` also allow for quick Flutter app generation using the following command:
83
84```bash
85flutter create --sample=[directory.File.sampleNumber] [name_of_project_directory]
86```
87
88#### Templates
89
90In order to support showing an entire app when you click on the right tab of
91the code snippet UI, we have to be able to insert the `snippet` into the template
92and instantiate the right parts.
93
94To do this, there is a [config/templates](config/templates) directory that
95contains a list of templates. These templates represent an entire app that the
96`snippet` can be placed into, basically a replacement for `lib/main.dart` in a
97flutter app package.
98
99For more information about how to create, use, or update templates, see
100[config/templates/README.md](config/templates/README.md).
101
102## Skeletons
103
104A skeleton (in relation to this tool) is an HTML template into which the Dart
105code blocks and descriptions are interpolated.
106
107There is currently one skeleton for
108[application](config/skeletons/application.html) `snippets` and one for
109[sample](config/skeletons/sample.html) `snippets`, but there could be more.
110
111Skeletons use mustache notation (e.g. `{{code}}`) to mark where components will
112be interpolated into the template. It doesn't actually use the mustache
113package, since these are simple string substitutions, but it uses the same
114syntax.
115
116The code block generation tools process the source input and emit HTML for output,
117which dartdoc places back into the documentation. Any options given to the
118 `{@tool ...}` directive are passed on verbatim to the tool.
119
120The `snippets` tool renders these examples through a combination of markdown
121and HTML using the `{@inject-html}` dartdoc directive.
122
123## Test Doc Generation Workflow
124
125If you are making changes to an existing code block or are creating a new code
126block, follow these steps to generate a local copy of the API docs and verify
127that your code blocks are showing up correctly:
128
1291. Make an update to a code block or create a new code block.
1302. From the root directory, run `./dev/bots/docs.sh`. This should start
131generating a local copy of the API documentation.
1323. Once complete, check `./dev/docs/doc` to check your API documentation. The
133search bar will not work locally, so open `./dev/docs/doc/index.html` to
134navigate through the documentation, or search `./dev/docs/doc/flutter` for your
135page of interest.
136