styled_text.dart 3.84 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

7
typedef Widget _TextTransformer(String name, String text);
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// From https://en.wikiquote.org/wiki/2001:_A_Space_Odyssey_(film)
const String _kDialogText = '''
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?
HAL: Affirmative, Dave. I read you.
Dave: Open the pod bay doors, HAL.
HAL: I'm sorry, Dave. I'm afraid I can't do that.
Dave: What's the problem?
HAL: I think you know what the problem is just as well as I do.
Dave: What are you talking about, HAL?
HAL: This mission is too important for me to allow you to jeopardize it.''';

// [["Dave", "Open the pod bay..."] ...]
final List<List<String>> _kNameLines = _kDialogText
  .split('\n')
  .map((String line) => line.split(':'))
  .toList();

26
final TextStyle _kDaveStyle = new TextStyle(color: Colors.indigo.shade400, height: 1.8);
27
final TextStyle _kHalStyle = new TextStyle(color: Colors.red.shade400, fontFamily: 'monospace');
28 29
const TextStyle _kBold = TextStyle(fontWeight: FontWeight.bold);
const TextStyle _kUnderline = TextStyle(
30
  decoration: TextDecoration.underline,
31
  decorationColor: Color(0xFF000000),
32 33 34 35
  decorationStyle: TextDecorationStyle.wavy
);

Widget toStyledText(String name, String text) {
36
  final TextStyle lineStyle = (name == 'Dave') ? _kDaveStyle : _kHalStyle;
Adam Barth's avatar
Adam Barth committed
37
  return new RichText(
38
    key: new Key(text),
Adam Barth's avatar
Adam Barth committed
39 40 41 42 43 44 45 46 47 48
    text: new TextSpan(
      style: lineStyle,
      children: <TextSpan>[
        new TextSpan(
          style: _kBold,
          children: <TextSpan>[
            new TextSpan(
              style: _kUnderline,
              text: name
            ),
49
            const TextSpan(text: ':')
Adam Barth's avatar
Adam Barth committed
50 51 52 53 54
          ]
        ),
        new TextSpan(text: text)
      ]
    )
55 56 57
  );
}

58
Widget toPlainText(String name, String text) => new Text(name + ':' + text);
59

60
class SpeakerSeparator extends StatelessWidget {
61
  @override
62 63 64
  Widget build(BuildContext context) {
    return new Container(
      constraints: const BoxConstraints.expand(height: 0.0),
65
      margin: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 64.0),
66
      decoration: const BoxDecoration(
67 68
        border: Border(
          bottom: BorderSide(color: Color.fromARGB(24, 0, 0, 0))
69 70 71 72 73 74
        )
      )
    );
  }
}

75
class StyledTextDemo extends StatefulWidget {
76
  @override
77 78 79 80
  _StyledTextDemoState createState() => new _StyledTextDemoState();
}

class _StyledTextDemoState extends State<StyledTextDemo> {
81
  @override
82 83 84 85 86
  void initState() {
    super.initState();
    _toText = toStyledText;
  }

87
  _TextTransformer _toText;
88 89 90 91 92 93 94

  void _handleTap() {
    setState(() {
      _toText = (_toText == toPlainText) ? toStyledText : toPlainText;
    });
  }

95
  @override
96
  Widget build(BuildContext context) {
97
    final List<Widget> lines = _kNameLines
98
      .map<Widget>((List<String> nameAndText) => _toText(nameAndText[0], nameAndText[1]))
99 100
      .toList();

101
    final List<Widget> children = <Widget>[];
102 103 104 105 106 107 108 109 110
    for (Widget line in lines) {
      children.add(line);
      if (line != lines.last)
        children.add(new SpeakerSeparator());
    }

    return new GestureDetector(
      onTap: _handleTap,
      child: new Container(
111
        padding: const EdgeInsets.symmetric(horizontal: 8.0),
112 113
        child: new Column(
          children: children,
114 115
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start
116 117 118 119 120 121 122 123 124
        )
      )
    );
  }
}

void main() {
  runApp(new MaterialApp(
    theme: new ThemeData.light(),
125 126
    home: new Scaffold(
      appBar: new AppBar(
127
        title: const Text('Hal and Dave')
128 129
      ),
      body: new Material(
130
        color: Colors.grey.shade50,
131 132 133
        child: new StyledTextDemo()
      )
    )
134 135
  ));
}