blob: 38c143ad3ecd8280c97bccc068efcfbd7eccd808 [file] [log] [blame]
Ian Hickson449f4a62019-11-27 15:04:02 -08001// Copyright 2014 The Flutter Authors. All rights reserved.
Adam Barth948ae152016-02-13 00:52:56 -08002// 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),
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010032 decorationStyle: TextDecorationStyle.wavy,
Adam Barth948ae152016-02-13 00:52:56 -080033);
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,
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010047 text: name,
Adam Barthfb4dbf42016-02-24 13:48:56 -080048 ),
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010049 const TextSpan(text: ':'),
50 ],
Adam Barthfb4dbf42016-02-24 13:48:56 -080051 ),
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010052 TextSpan(text: text),
53 ],
54 ),
Adam Barth948ae152016-02-13 00:52:56 -080055 );
56}
57
Alexandre Ardhuin34059ee2021-06-01 20:14:06 +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 {
Michael Goderbauer91bcf422022-03-29 12:53:08 -070061 const SpeakerSeparator({super.key});
Michael Goderbauer0f568292021-03-02 10:14:02 -080062
Hixie797e27e2016-03-14 13:31:43 -070063 @override
Adam Barth948ae152016-02-13 00:52:56 -080064 Widget build(BuildContext context) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020065 return Container(
Adam Barth948ae152016-02-13 00:52:56 -080066 constraints: const BoxConstraints.expand(height: 0.0),
Adam Barthe71bd772016-03-12 11:43:18 -080067 margin: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 64.0),
Adam Barth948ae152016-02-13 00:52:56 -080068 decoration: const BoxDecoration(
Alexandre Ardhuineda03e22018-08-02 12:02:32 +020069 border: Border(
Alexandre Ardhuin7f318612021-04-28 22:54:49 +020070 bottom: BorderSide(color: Color.fromARGB(24, 0, 0, 0)),
71 ),
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010072 ),
Adam Barth948ae152016-02-13 00:52:56 -080073 );
74 }
75}
76
Adam Barth95fc5ae2016-03-12 12:13:16 -080077class StyledTextDemo extends StatefulWidget {
Michael Goderbauer91bcf422022-03-29 12:53:08 -070078 const StyledTextDemo({super.key});
Michael Goderbauer0f568292021-03-02 10:14:02 -080079
Hixie797e27e2016-03-14 13:31:43 -070080 @override
Michael Goderbauerb8a24562021-05-10 16:26:16 -070081 State<StyledTextDemo> createState() => _StyledTextDemoState();
Adam Barth948ae152016-02-13 00:52:56 -080082}
83
84class _StyledTextDemoState extends State<StyledTextDemo> {
Michael Goderbauerdd2ea7c2020-10-22 13:33:07 -070085 _TextTransformer _toText = toStyledText;
Adam Barth948ae152016-02-13 00:52:56 -080086
87 void _handleTap() {
88 setState(() {
89 _toText = (_toText == toPlainText) ? toStyledText : toPlainText;
90 });
91 }
92
Hixie797e27e2016-03-14 13:31:43 -070093 @override
Adam Barth948ae152016-02-13 00:52:56 -080094 Widget build(BuildContext context) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020095 return GestureDetector(
Adam Barth948ae152016-02-13 00:52:56 -080096 onTap: _handleTap,
Alexandre Ardhuind927c932018-09-12 08:29:29 +020097 child: Container(
Alexandre Ardhuine9a775b2017-02-21 23:54:29 +010098 padding: const EdgeInsets.symmetric(horizontal: 8.0),
Alexandre Ardhuind927c932018-09-12 08:29:29 +020099 child: Column(
Adam Barthd5b2e2a2016-03-12 18:28:42 -0800100 mainAxisAlignment: MainAxisAlignment.center,
Alexandre Ardhuin387f8852019-03-01 08:17:55 +0100101 crossAxisAlignment: CrossAxisAlignment.start,
Alexandre Ardhuindf4bf452019-09-17 16:23:44 +0200102 children: _kNameLines
103 .map<Widget>((List<String> nameAndText) => _toText(nameAndText[0], nameAndText[1]))
104 .expand((Widget line) => <Widget>[
105 line,
Michael Goderbauer0f568292021-03-02 10:14:02 -0800106 const SpeakerSeparator(),
Alexandre Ardhuindf4bf452019-09-17 16:23:44 +0200107 ])
108 .toList()..removeLast(),
Alexandre Ardhuin387f8852019-03-01 08:17:55 +0100109 ),
110 ),
Adam Barth948ae152016-02-13 00:52:56 -0800111 );
112 }
113}
114
115void main() {
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200116 runApp(MaterialApp(
117 theme: ThemeData.light(),
118 home: Scaffold(
119 appBar: AppBar(
Alexandre Ardhuin387f8852019-03-01 08:17:55 +0100120 title: const Text('Hal and Dave'),
Ian Hickson1b9476c2016-04-20 09:33:28 -0700121 ),
Alexandre Ardhuind927c932018-09-12 08:29:29 +0200122 body: Material(
Alexandre Ardhuin578ca0a2017-03-21 23:14:55 +0100123 color: Colors.grey.shade50,
Michael Goderbauer0f568292021-03-02 10:14:02 -0800124 child: const StyledTextDemo(),
Alexandre Ardhuin387f8852019-03-01 08:17:55 +0100125 ),
126 ),
Adam Barth948ae152016-02-13 00:52:56 -0800127 ));
128}