styled_text.dart 3.62 KB
Newer Older
1 2 3 4
// 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.

5 6
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
7

Hixie's avatar
Hixie committed
8 9
typedef Widget TextTransformer(String name, String text);

10 11 12
class StyledTextApp extends StatefulComponent {
  StyledTextAppState createState() => new StyledTextAppState();
}
13

14
class StyledTextAppState extends State<StyledTextApp> {
15 16
  void initState() {
    super.initState();
17 18 19 20 21 22 23
    toText = toStyledText;
    nameLines = dialogText
      .split('\n')
      .map((String line) => line.split(':'))
      .toList();
  }

Hixie's avatar
Hixie committed
24
  TextTransformer toText;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

  // From https://en.wikiquote.org/wiki/2001:_A_Space_Odyssey_(film)
  final String dialogText = '''
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..."] ...]
  List<List<String>> nameLines;

40 41
  final TextStyle daveStyle = new TextStyle(color: Colors.indigo[400], height: 1.8);
  final TextStyle halStyle = new TextStyle(color: Colors.red[400], fontFamily: "monospace");
42
  final TextStyle boldStyle = const TextStyle(fontWeight: FontWeight.bold);
43
  final TextStyle underlineStyle = const TextStyle(
44
    decoration: TextDecoration.underline,
45 46 47 48
    decorationColor: const Color(0xFF000000),
    decorationStyle: TextDecorationStyle.wavy
  );

49
  Widget toStyledText(String name, String text) {
50 51
    TextStyle lineStyle = (name == "Dave") ? daveStyle : halStyle;
    return new StyledText(
52
      key: new Key(text),
53 54 55 56
      elements: [lineStyle, [boldStyle, [underlineStyle, name], ":"], text]
    );
  }

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

59
  Widget createSeparator() {
60
    return new Container(
61
      constraints: const BoxConstraints.expand(height: 0.0),
62 63 64 65 66 67 68 69 70
      margin: const EdgeDims.symmetric(vertical: 10.0, horizontal: 64.0),
      decoration: const BoxDecoration(
        border: const Border(
          bottom: const BorderSide(color: const Color.fromARGB(24, 0, 0, 0))
        )
      )
    );
  }

Adam Barth's avatar
Adam Barth committed
71
  void toggleToTextFunction(_) {
72 73 74 75 76
    setState(() {
      toText = (toText == toPlainText) ? toStyledText : toPlainText;
    });
  }

77 78
  Widget build(BuildContext context) {
    List<Widget> lines = nameLines
Hixie's avatar
Hixie committed
79
      .map((List<String> nameAndText) => Function.apply(toText, nameAndText))
80 81
      .toList();

Hixie's avatar
Hixie committed
82
    List<Widget> children = <Widget>[];
83
    for (Widget line in lines) {
84 85 86 87 88 89
      children.add(line);
      if (line != lines.last) {
        children.add(createSeparator());
      }
    }

90
    Widget body = new Container(
91
        padding: new EdgeDims.symmetric(horizontal: 8.0),
92 93
        child: new Column(
          children: children,
94 95 96 97 98 99 100 101 102 103 104 105 106 107
          justifyContent: FlexJustifyContent.center,
          alignItems: FlexAlignItems.start
        )
      );

    Listener interactiveBody = new Listener(
      child: body,
      onPointerDown: toggleToTextFunction
    );

    return new Theme(
      data: new ThemeData.light(),
      child: new Scaffold(
        body: new Material(
108
          color: Colors.grey[50],
109 110
          child: interactiveBody
        ),
Adam Barth's avatar
Adam Barth committed
111
        toolBar: new ToolBar(
112 113 114 115 116 117 118 119 120 121
          center: new Text('Hal and Dave')
        )
      )
    );
  }
}

void main() {
  runApp(new StyledTextApp());
}