Skip to content

Commit 95948d7

Browse files
committed
text: Factor out kMonospaceTextStyle; handle iOS quirk about 'monospace'
To encapsulate handling the odd bug where Apple's font collection doesn't respond to 'monospace'; see implementation comment.
1 parent f84aa30 commit 95948d7

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

lib/widgets/content.dart

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import '../model/content.dart';
77
import '../model/store.dart';
88
import 'store.dart';
99
import 'lightbox.dart';
10+
import 'text.dart';
1011

1112
/// The font size for message content in a plain unstyled paragraph.
1213
const double kBaseFontSize = 14;
@@ -371,18 +372,14 @@ InlineSpan inlineCode(InlineCodeNode node) {
371372
// ]);
372373
}
373374

374-
const _kInlineCodeStyle = TextStyle(
375+
final _kInlineCodeStyle = kMonospaceTextStyle.merge(const TextStyle(
375376
backgroundColor: Color(0xffeeeeee),
376-
fontSize: 0.825 * kBaseFontSize,
377-
fontFamily: "Source Code Pro", // TODO supply font
378-
fontFamilyFallback: ["monospace"]);
377+
fontSize: 0.825 * kBaseFontSize));
379378

380-
const _kCodeBlockStyle = TextStyle(
379+
final _kCodeBlockStyle = kMonospaceTextStyle.merge(const TextStyle(
381380
backgroundColor: Color.fromRGBO(255, 255, 255, 1),
382381
fontSize: 0.825 * kBaseFontSize,
383-
fontFamily: "Source Code Pro", // TODO supply font
384-
fontFamilyFallback: ["monospace"],
385-
);
382+
));
386383

387384
// const _kInlineCodeLeftBracket = '⸤';
388385
// const _kInlineCodeRightBracket = '⸣';
@@ -639,8 +636,4 @@ InlineSpan _errorUnimplemented(UnimplementedNode node) {
639636

640637
const errorStyle = TextStyle(fontWeight: FontWeight.bold, color: Colors.red);
641638

642-
const errorCodeStyle = TextStyle(
643-
color: Colors.red,
644-
fontFamily: "Source Code Pro", // TODO supply font
645-
fontFamilyFallback: ["monospace"],
646-
);
639+
final errorCodeStyle = kMonospaceTextStyle.merge(const TextStyle(color: Colors.red));

lib/widgets/text.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'dart:io';
2+
import 'package:flutter/widgets.dart';
3+
4+
/// A mergeable [TextStyle] with 'Source Code Pro' and platform-aware fallbacks.
5+
///
6+
/// Example:
7+
///
8+
/// ```dart
9+
/// kMonospaceTextStyle.merge(const TextStyle(color: Colors.red))
10+
/// ```
11+
final TextStyle kMonospaceTextStyle = TextStyle(
12+
fontFamily: 'Source Code Pro', // TODO supply font
13+
14+
// Oddly, iOS doesn't handle 'monospace':
15+
// https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/flutter.3A.20monospace.20font.20fallback/near/1570622
16+
fontFamilyFallback: Platform.isIOS ? ['Menlo', 'Courier'] : ['monospace'],
17+
18+
inherit: true,
19+
);

0 commit comments

Comments
 (0)