blob: 633d55e722089b951a1919b9637d30efccffa12f [file] [log] [blame]
Adam Barth948ae152016-02-13 00:52:56 -08001// 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
5import 'package:flutter/material.dart';
6
Alexandre Ardhuina07d3712018-09-14 21:06:19 +02007typedef _TextTransformer = Widget Function(String name, String text);
Adam Barth948ae152016-02-13 00:52:56 -08008
9// From https://en.wikiquote.org/wiki/2001:_A_Space_Odyssey_(film)
10const String _kDialogText = '''
11Dave: 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?
12HAL: Affirmative, Dave. I read you.
13Dave: Open the pod bay doors, HAL.
14HAL: I'm sorry, Dave. I'm afraid I can't do that.
15Dave: What's the problem?
16HAL: I think you know what the problem is just as well as I do.
17Dave: What are you talking about, HAL?
18HAL: This mission is too important for me to allow you to jeopardize it.''';
19
20// [["Dave", "Open the pod bay..."] ...]
21final List<List<String>> _kNameLines = _kDialogText
22 .split('\n')
Alexandre Ardhuinf62afdc2018-10-01 21:29:08 +020023 .map<List<String>>((String line) => line.split(':'))
Adam Barth948ae152016-02-13 00:52:56 -080024 .toList();
25
Alexandre Ardhuind927c932018-09-12 08:29:29 +020026final TextStyle _kDaveStyle = TextStyle(color: Colors.indigo.shade400, height: 1.8);
27final TextStyle _kHalStyle = TextStyle(color: Colors.red.shade400, fontFamily: 'monospace');
Alexandre Ardhuineda03e22018-08-02 12:02:32 +020028const TextStyle _kBold = TextStyle(fontWeight: FontWeight.bold);
29const TextStyle _kUnderline = TextStyle(
Adam Barth948ae152016-02-13 00:52:56 -080030 decoration: TextDecoration.underline,
Alexandre Ardhuineda03e22018-08-02 12:02:32 +020031 decorationColor: Color(0xFF000000),
Adam Barth948ae152016-02-13 00:52:56 -080032 decorationStyle: TextDecorationStyle.wavy
33);
34
35Widget toStyledText(String name, String text) {
Alexandre Ardhuin1fce14a2017-10-22 18:11:36 +020036 final TextStyle lineStyle = (name == 'Dave') ? _kDaveStyle : _kHalStyle;
Alexandre Ardhuind927c932018-09-12 08:29:29 +020037 return RichText(
38 key: Key(text),
39 text: TextSpan(
Adam Barthfb4dbf42016-02-24 13:48:56 -080040 style: lineStyle,
41 children: <TextSpan>[
Alexandre Ardhuind927c932018-09-12 08:29:29 +020042 TextSpan(
Adam Barthfb4dbf42016-02-24 13:48:56 -080043 style: _kBold,
44 children: <TextSpan>[
Alexandre Ardhuind927c932018-09-12 08:29:29 +020045 TextSpan(
Adam Barthfb4dbf42016-02-24 13:48:56 -080046 style: _kUnderline,
47 text: name
48 ),
Alexandre Ardhuine9a775b2017-02-21 23:54:29 +010049 const TextSpan(text: ':')
Adam Barthfb4dbf42016-02-24 13:48:56 -080050 ]
51 ),
Alexandre Ardhuind927c932018-09-12 08:29:29 +020052 TextSpan(text: text)
Adam Barthfb4dbf42016-02-24 13:48:56 -080053 ]
54 )
Adam Barth948ae152016-02-13 00:52:56 -080055 );
56}
57
Alexandre Ardhuind927c932018-09-12 08:29:29 +020058Widget toPlainText(String name, String text) => Text(name + ':' + text);
Adam Barth948ae152016-02-13 00:52:56 -080059
Adam Barth95fc5ae2016-03-12 12:13:16 -080060class SpeakerSeparator extends StatelessWidget {
Hixie797e27e2016-03-14 13:31:43 -070061 @override
Adam Barth948ae152016-02-13 00:52:56 -080062 Widget build(BuildContext context) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020063 return Container(
Adam Barth948ae152016-02-13 00:52:56 -080064 constraints: const BoxConstraints.expand(height: 0.0),
Adam Barthe71bd772016-03-12 11:43:18 -080065 margin: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 64.0),
Adam Barth948ae152016-02-13 00:52:56 -080066 decoration: const BoxDecoration(
Alexandre Ardhuineda03e22018-08-02 12:02:32 +020067 border: Border(
68 bottom: BorderSide(color: Color.fromARGB(24, 0, 0, 0))
Adam Barth948ae152016-02-13 00:52:56 -080069 )
70 )
71 );
72 }
73}
74
Adam Barth95fc5ae2016-03-12 12:13:16 -080075class StyledTextDemo extends StatefulWidget {
Hixie797e27e2016-03-14 13:31:43 -070076 @override
Alexandre Ardhuind927c932018-09-12 08:29:29 +020077 _StyledTextDemoState createState() => _StyledTextDemoState();
Adam Barth948ae152016-02-13 00:52:56 -080078}
79
80class _StyledTextDemoState extends State<StyledTextDemo> {
Hixie797e27e2016-03-14 13:31:43 -070081 @override
Adam Barth948ae152016-02-13 00:52:56 -080082 void initState() {
83 super.initState();
84 _toText = toStyledText;
85 }
86
Ian Hickson63aa1392017-01-23 01:04:31 -080087 _TextTransformer _toText;
Adam Barth948ae152016-02-13 00:52:56 -080088
89 void _handleTap() {
90 setState(() {
91 _toText = (_toText == toPlainText) ? toStyledText : toPlainText;
92 });
93 }
94
Hixie797e27e2016-03-14 13:31:43 -070095 @override
Adam Barth948ae152016-02-13 00:52:56 -080096 Widget build(BuildContext context) {
Chris Bracken4c8c4202017-03-03 18:04:27 -080097 final List<Widget> lines = _kNameLines
Ian Hickson63aa1392017-01-23 01:04:31 -080098 .map<Widget>((List<String> nameAndText) => _toText(nameAndText[0], nameAndText[1]))
Adam Barth948ae152016-02-13 00:52:56 -080099 .toList();
100
Chris Bracken4c8c4202017-03-03 18:04:27 -0800101 final List<Widget> children = <Widget>[];
Adam Barth948ae152016-02-13 00:52:56 -0800102 for (Widget line in lines) {
103 children.add(line);
104 if (line != lines.last)
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200105 children.add(SpeakerSeparator());
Adam Barth948ae152016-02-13 00:52:56 -0800106 }
107
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200108 return GestureDetector(
Adam Barth948ae152016-02-13 00:52:56 -0800109 onTap: _handleTap,
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200110 child: Container(
Alexandre Ardhuine9a775b2017-02-21 23:54:29 +0100111 padding: const EdgeInsets.symmetric(horizontal: 8.0),
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200112 child: Column(
Adam Barth948ae152016-02-13 00:52:56 -0800113 children: children,
Adam Barthd5b2e2a2016-03-12 18:28:42 -0800114 mainAxisAlignment: MainAxisAlignment.center,
115 crossAxisAlignment: CrossAxisAlignment.start
Adam Barth948ae152016-02-13 00:52:56 -0800116 )
117 )
118 );
119 }
120}
121
122void main() {
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200123 runApp(MaterialApp(
124 theme: ThemeData.light(),
125 home: Scaffold(
126 appBar: AppBar(
Ian Hickson3eb87832017-04-07 12:24:32 -0700127 title: const Text('Hal and Dave')
Ian Hickson1b9476c2016-04-20 09:33:28 -0700128 ),
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200129 body: Material(
Alexandre Ardhuin578ca0a2017-03-21 23:14:55 +0100130 color: Colors.grey.shade50,
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200131 child: StyledTextDemo()
Ian Hickson1b9476c2016-04-20 09:33:28 -0700132 )
133 )
Adam Barth948ae152016-02-13 00:52:56 -0800134 ));
135}