Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(881)

Side by Side Diff: sky/examples/widgets/styled_text.dart

Issue 1194693002: Adds StyledText (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Changes per review feedback, merge Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | sky/sdk/lib/widgets/basic.dart » ('j') | sky/sdk/lib/widgets/basic.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 import 'package:sky/painting/text_style.dart';
6 import 'package:sky/rendering/box.dart';
7 import 'package:sky/rendering/flex.dart';
8 import 'package:sky/rendering/sky_binding.dart';
9 import 'package:sky/theme/colors.dart';
10 import 'package:sky/theme/typography.dart';
11 import 'package:sky/widgets/basic.dart';
12 import 'package:sky/widgets/material.dart';
13 import 'package:sky/widgets/scaffold.dart';
14 import 'package:sky/widgets/tool_bar.dart';
15 import 'package:sky/widgets/widget.dart';
16
17
18 class StyledTextApp extends App {
19
20 StyledTextApp() {
21 toText = toStyledText;
22 nameLines = dialogText
23 .split('\n')
24 .map((String line) => line.split(':'))
25 .toList();
26 }
27
28 Function toText;
29
30 // From https://en.wikiquote.org/wiki/2001:_A_Space_Odyssey_(film)
31 final String dialogText = '''
32 Dave: Open the pod bay doors, please, HAL. Open the pod bay doors, please, HAL. Hello, HAL. Do you read me? Hello, HAL. Do you read me? Do you read me, HAL?
33 HAL: Affirmative, Dave. I read you.
34 Dave: Open the pod bay doors, HAL.
35 HAL: I'm sorry, Dave. I'm afraid I can't do that.
36 Dave: What's the problem?
37 HAL: I think you know what the problem is just as well as I do.
38 Dave: What are you talking about, HAL?
39 HAL: This mission is too important for me to allow you to jeopardize it.''';
40
41 // [["Dave", "Open the pod bay..."] ...]
42 List<List<String>> nameLines;
43
44 final TextStyle daveStyle = new TextStyle(color: Indigo[400]);
45 final TextStyle halStyle = new TextStyle(color: Red[400], fontFamily: "monospa ce");
46 final TextStyle boldStyle = new TextStyle(fontWeight: bold);
47
48 Component toStyledText(String name, String text) {
49 TextStyle lineStyle = (name == "Dave") ? daveStyle : halStyle;
50 return new StyledText(
51 key: text,
52 elements: [lineStyle, [boldStyle, name + ":"], text]
53 );
54 }
55
56 Component toPlainText(String name, String text) => new Text(name + ":" + text) ;
57
58 Component createSeparator() {
59 return new Container(
60 constraints: const BoxConstraints(minWidth: double.INFINITY, maxHeight: 0. 0),
61 margin: const EdgeDims.symmetric(vertical: 10.0, horizontal: 64.0),
62 decoration: const BoxDecoration(
63 border: const Border(
64 bottom: const BorderSide(color: const Color.fromARGB(24, 0, 0, 0))
65 )
66 )
67 );
68 }
69
70 void toggleToTextFunction(_) {
71 setState(() {
72 toText = (toText == toPlainText) ? toStyledText : toPlainText;
73 });
74 }
75
76 Widget build() {
77 List<Component> lines = nameLines
78 .map((nameAndText) => Function.apply(toText, nameAndText))
79 .toList();
80
81 List<Component> children = [];
82 for (Component line in lines) {
83 children.add(line);
84 if (line != lines.last) {
85 children.add(createSeparator());
86 }
87 }
88
89 Container body = new Container(
90 padding: new EdgeDims.symmetric(horizontal: 8.0),
91 child: new Flex(children,
92 direction: FlexDirection.vertical,
93 justifyContent: FlexJustifyContent.center,
94 alignItems: FlexAlignItems.flexStart
95 )
96 );
97
98 Listener interactiveBody = new Listener(
99 child: body,
100 onPointerDown: toggleToTextFunction
101 );
102
103 return new Scaffold(
104 body: new Material(child: interactiveBody),
105 toolbar: new ToolBar(
106 center: new Text('Hal and Dave', style: white.title),
107 backgroundColor: Blue[500]
108 )
109 );
110 }
111 }
112
113 void main() {
114 runApp(new StyledTextApp());
115 SkyBinding.instance.onFrame = () {
116 // uncomment this for debugging:
117 // SkyBinding.instance.debugDumpRenderTree();
118 };
119 }
OLDNEW
« no previous file with comments | « no previous file | sky/sdk/lib/widgets/basic.dart » ('j') | sky/sdk/lib/widgets/basic.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698