OLD | NEW |
| (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 'dart:sky' as sky; | |
6 | |
7 import '../widgets/basic.dart'; | |
8 import 'editable_string.dart'; | |
9 import 'editable_text.dart'; | |
10 import 'keyboard.dart'; | |
11 | |
12 typedef void ValueChanged(value); | |
13 | |
14 class Input extends Component { | |
15 | |
16 Input({Object key, | |
17 this.placeholder, | |
18 this.onChanged, | |
19 this.focused}) | |
20 : super(key: key, stateful: true) { | |
21 _editableValue = new EditableString( | |
22 text: _value, | |
23 onUpdated: _handleTextUpdated | |
24 ); | |
25 } | |
26 | |
27 // static final Style _style = new Style(''' | |
28 // transform: translateX(0); | |
29 // margin: 8px; | |
30 // padding: 8px; | |
31 // border-bottom: 1px solid ${Grey[200]}; | |
32 // align-self: center; | |
33 // height: 1.2em; | |
34 // white-space: pre; | |
35 // overflow: hidden;''' | |
36 // ); | |
37 | |
38 // static final Style _placeholderStyle = new Style(''' | |
39 // top: 8px; | |
40 // left: 8px; | |
41 // position: absolute; | |
42 // ${typography.black.caption};''' | |
43 // ); | |
44 | |
45 // static final String _focusedInlineStyle = ''' | |
46 // padding: 7px; | |
47 // border-bottom: 2px solid ${Blue[500]};'''; | |
48 | |
49 String placeholder; | |
50 ValueChanged onChanged; | |
51 bool focused = false; | |
52 | |
53 void syncFields(Input source) { | |
54 placeholder = source.placeholder; | |
55 onChanged = source.onChanged; | |
56 focused = source.focused; | |
57 } | |
58 | |
59 String _value = ''; | |
60 bool _isAttachedToKeyboard = false; | |
61 EditableString _editableValue; | |
62 | |
63 void _handleTextUpdated() { | |
64 scheduleBuild(); | |
65 if (_value != _editableValue.text) { | |
66 _value = _editableValue.text; | |
67 if (onChanged != null) | |
68 onChanged(_value); | |
69 } | |
70 } | |
71 | |
72 UINode build() { | |
73 if (focused && !_isAttachedToKeyboard) { | |
74 keyboard.show(_editableValue.stub); | |
75 _isAttachedToKeyboard = true; | |
76 } | |
77 | |
78 List<UINode> children = []; | |
79 | |
80 if (placeholder != null && _value.isEmpty) { | |
81 children.add(new Container( | |
82 // style: _placeholderStyle, | |
83 child: new Text(placeholder) | |
84 )); | |
85 } | |
86 | |
87 children.add(new EditableText(value: _editableValue, focused: focused)); | |
88 | |
89 return new EventListenerNode( | |
90 // style: _style, | |
91 // inlineStyle: focused ? _focusedInlineStyle : null, | |
92 new Stack(children), | |
93 onPointerDown: (sky.Event e) => keyboard.showByRequest() | |
94 ); | |
95 } | |
96 | |
97 void didUnmount() { | |
98 if (_isAttachedToKeyboard) | |
99 keyboard.hide(); | |
100 super.didUnmount(); | |
101 } | |
102 | |
103 } | |
OLD | NEW |