2019-11-27 15:04:02 -08:00
|
|
|
// Copyright 2014 The Flutter Authors. All rights reserved.
|
2016-04-20 09:29:01 -07:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
import 'dart:async';
|
2016-04-27 12:23:02 -07:00
|
|
|
|
2016-04-20 09:29:01 -07:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
|
|
|
|
|
const String _kStartTag = '// START ';
|
|
|
|
|
const String _kEndTag = '// END';
|
|
|
|
|
|
|
|
|
|
Map<String, String> _exampleCode;
|
|
|
|
|
|
|
|
|
|
Future<String> getExampleCode(String tag, AssetBundle bundle) async {
|
|
|
|
|
if (_exampleCode == null)
|
|
|
|
|
await _parseExampleCode(bundle);
|
|
|
|
|
return _exampleCode[tag];
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 22:03:06 +02:00
|
|
|
Future<void> _parseExampleCode(AssetBundle bundle) async {
|
2016-10-03 12:00:01 -07:00
|
|
|
final String code = await bundle.loadString('lib/gallery/example_code.dart') ??
|
|
|
|
|
'// lib/gallery/example_code.dart not found\n';
|
2016-04-20 09:29:01 -07:00
|
|
|
_exampleCode = <String, String>{};
|
|
|
|
|
|
|
|
|
|
final List<String> lines = code.split('\n');
|
|
|
|
|
|
|
|
|
|
List<String> codeBlock;
|
|
|
|
|
String codeTag;
|
|
|
|
|
|
2020-01-07 16:32:04 +01:00
|
|
|
for (final String line in lines) {
|
2016-04-20 09:29:01 -07:00
|
|
|
if (codeBlock == null) {
|
|
|
|
|
// Outside a block.
|
|
|
|
|
if (line.startsWith(_kStartTag)) {
|
|
|
|
|
// Starting a new code block.
|
|
|
|
|
codeBlock = <String>[];
|
2018-02-28 20:39:48 +00:00
|
|
|
codeTag = line.substring(_kStartTag.length).trim();
|
2016-04-20 09:29:01 -07:00
|
|
|
} else {
|
|
|
|
|
// Just skipping the line.
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Inside a block.
|
|
|
|
|
if (line.startsWith(_kEndTag)) {
|
|
|
|
|
// Add the block.
|
|
|
|
|
_exampleCode[codeTag] = codeBlock.join('\n');
|
|
|
|
|
codeBlock = null;
|
|
|
|
|
codeTag = null;
|
|
|
|
|
} else {
|
|
|
|
|
// Add to the current block
|
2018-02-28 20:39:48 +00:00
|
|
|
// trimRight() to remove any \r on Windows
|
|
|
|
|
// without removing any useful indentation
|
|
|
|
|
codeBlock.add(line.trimRight());
|
2016-04-20 09:29:01 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|