demo.dart 7.26 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:async';

7
import 'package:flutter/cupertino.dart';
8
import 'package:flutter/material.dart';
9
import 'package:url_launcher/url_launcher.dart';
10

11
import 'demos.dart';
12
import 'example_code_parser.dart';
13
import 'syntax_highlighter.dart';
14

15
@immutable
16
class ComponentDemoTabData {
17
  const ComponentDemoTabData({
18
    this.demoWidget,
19
    this.exampleCodeTag,
20
    this.description,
21 22
    this.tabName,
    this.documentationUrl,
23 24
  });

25
  final Widget demoWidget;
26
  final String exampleCodeTag;
27 28
  final String description;
  final String tabName;
29
  final String documentationUrl;
30 31 32 33 34

  @override
  bool operator==(Object other) {
    if (other.runtimeType != runtimeType)
      return false;
35 36 37 38
    return other is ComponentDemoTabData
        && other.tabName == tabName
        && other.description == description
        && other.documentationUrl == documentationUrl;
39 40 41
  }

  @override
42
  int get hashCode => hashValues(tabName, description, documentationUrl);
43 44 45
}

class TabbedComponentDemoScaffold extends StatelessWidget {
46
  const TabbedComponentDemoScaffold({
47
    this.title,
48 49
    this.demos,
    this.actions,
50 51
    this.isScrollable = true,
    this.showExampleCodeAction = true,
52 53 54 55
  });

  final List<ComponentDemoTabData> demos;
  final String title;
56
  final List<Widget> actions;
57 58
  final bool isScrollable;
  final bool showExampleCodeAction;
59

60
  void _showExampleCode(BuildContext context) {
61
    final String tag = demos[DefaultTabController.of(context).index].exampleCodeTag;
62
    if (tag != null) {
63 64
      Navigator.push(context, MaterialPageRoute<FullScreenCodeDialog>(
        builder: (BuildContext context) => FullScreenCodeDialog(exampleCodeTag: tag)
65 66 67 68
      ));
    }
  }

69
  Future<void> _showApiDocumentation(BuildContext context) async {
70
    final String url = demos[DefaultTabController.of(context).index].documentationUrl;
71 72 73 74 75 76 77 78 79 80
    if (url == null)
      return;

    if (await canLaunch(url)) {
      await launch(url);
    } else {
      showDialog<void>(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
81
            title: const Text("Couldn't display URL:"),
82 83 84 85 86 87 88 89 90
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 16.0),
                child: Text(url),
              ),
            ],
          );
        },
      );
91 92 93
    }
  }

94 95
  @override
  Widget build(BuildContext context) {
96
    return DefaultTabController(
Hans Muller's avatar
Hans Muller committed
97
      length: demos.length,
98 99 100
      child: Scaffold(
        appBar: AppBar(
          title: Text(title),
101 102
          actions: <Widget>[
            ...?actions,
103 104 105 106 107 108 109 110
            Builder(
              builder: (BuildContext context) {
                return IconButton(
                  icon: const Icon(Icons.library_books, semanticLabel: 'Show documentation'),
                  onPressed: () => _showApiDocumentation(context),
                );
              },
            ),
111 112 113 114 115 116 117 118 119 120 121
            if (showExampleCodeAction)
              Builder(
                builder: (BuildContext context) {
                  return IconButton(
                    icon: const Icon(Icons.code),
                    tooltip: 'Show example code',
                    onPressed: () => _showExampleCode(context),
                  );
                },
              ),
          ],
122
          bottom: TabBar(
123
            isScrollable: isScrollable,
124
            tabs: demos.map<Widget>((ComponentDemoTabData data) => Tab(text: data.tabName)).toList(),
Hans Muller's avatar
Hans Muller committed
125
          ),
126
        ),
127
        body: TabBarView(
128
          children: demos.map<Widget>((ComponentDemoTabData demo) {
129
            return SafeArea(
130 131
              top: false,
              bottom: false,
132
              child: Column(
133
                children: <Widget>[
134
                  Padding(
135
                    padding: const EdgeInsets.all(16.0),
136
                    child: Text(demo.description,
137
                      style: Theme.of(context).textTheme.subtitle1,
138
                    ),
139
                  ),
140
                  Expanded(child: demo.demoWidget),
141 142
                ],
              ),
143
            );
Hans Muller's avatar
Hans Muller committed
144 145 146
          }).toList(),
        ),
      ),
147 148 149 150
    );
  }
}

151
class FullScreenCodeDialog extends StatefulWidget {
152
  const FullScreenCodeDialog({ this.exampleCodeTag });
153 154 155 156

  final String exampleCodeTag;

  @override
157
  FullScreenCodeDialogState createState() => FullScreenCodeDialogState();
158 159 160
}

class FullScreenCodeDialogState extends State<FullScreenCodeDialog> {
161

162 163 164
  String _exampleCode;

  @override
165
  void didChangeDependencies() {
166
    getExampleCode(widget.exampleCodeTag, DefaultAssetBundle.of(context)).then<void>((String code) {
167 168
      if (mounted) {
        setState(() {
169
          _exampleCode = code ?? 'Example code not found';
170 171
        });
      }
172
    });
173
    super.didChangeDependencies();
174
  }
175 176 177

  @override
  Widget build(BuildContext context) {
178 179 180 181
    final SyntaxHighlighterStyle style = Theme.of(context).brightness == Brightness.dark
      ? SyntaxHighlighterStyle.darkThemeStyle()
      : SyntaxHighlighterStyle.lightThemeStyle();

182 183
    Widget body;
    if (_exampleCode == null) {
184
      body = const Center(
185
        child: CircularProgressIndicator(),
186 187
      );
    } else {
188 189
      body = SingleChildScrollView(
        child: Padding(
190
          padding: const EdgeInsets.all(16.0),
191 192
          child: RichText(
            text: TextSpan(
193
              style: const TextStyle(fontFamily: 'monospace', fontSize: 10.0),
194
              children: <TextSpan>[
195 196 197 198 199
                DartSyntaxHighlighter(style).format(_exampleCode),
              ],
            ),
          ),
        ),
200 201 202
      );
    }

203 204 205
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
206 207 208 209
          icon: const Icon(
            Icons.clear,
            semanticLabel: 'Close',
          ),
210
          onPressed: () { Navigator.pop(context); },
211
        ),
212
        title: const Text('Example code'),
213
      ),
214
      body: body,
215 216 217
    );
  }
}
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

class MaterialDemoDocumentationButton extends StatelessWidget {
  MaterialDemoDocumentationButton(String routeName, { Key key })
    : documentationUrl = kDemoDocumentationUrl[routeName],
      assert(
        kDemoDocumentationUrl[routeName] != null,
        'A documentation URL was not specified for demo route $routeName in kAllGalleryDemos',
      ),
      super(key: key);

  final String documentationUrl;

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: const Icon(Icons.library_books),
234
      tooltip: 'API documentation',
235
      onPressed: () => launch(documentationUrl, forceWebView: true),
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    );
  }
}

class CupertinoDemoDocumentationButton extends StatelessWidget {
  CupertinoDemoDocumentationButton(String routeName, { Key key })
    : documentationUrl = kDemoDocumentationUrl[routeName],
      assert(
        kDemoDocumentationUrl[routeName] != null,
        'A documentation URL was not specified for demo route $routeName in kAllGalleryDemos',
      ),
      super(key: key);

  final String documentationUrl;

  @override
  Widget build(BuildContext context) {
    return CupertinoButton(
      padding: EdgeInsets.zero,
255 256 257 258
      child: Semantics(
        label: 'API documentation',
        child: const Icon(CupertinoIcons.book),
      ),
259
      onPressed: () => launch(documentationUrl, forceWebView: true),
260 261 262
    );
  }
}