Skip to content

Commit 9347698

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 8395f1c commit 9347698

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;
@@ -373,18 +374,14 @@ InlineSpan inlineCode(InlineCodeNode node) {
373374
// ]);
374375
}
375376

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

382-
const _kCodeBlockStyle = TextStyle(
381+
final _kCodeBlockStyle = kMonospaceTextStyle.merge(const TextStyle(
383382
backgroundColor: Color.fromRGBO(255, 255, 255, 1),
384383
fontSize: 0.825 * kBaseFontSize,
385-
fontFamily: "Source Code Pro", // TODO supply font
386-
fontFamilyFallback: ["monospace"],
387-
);
384+
));
388385

389386
// const _kInlineCodeLeftBracket = '⸤';
390387
// const _kInlineCodeRightBracket = '⸣';
@@ -641,8 +638,4 @@ InlineSpan _errorUnimplemented(UnimplementedNode node) {
641638

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

644-
const errorCodeStyle = TextStyle(
645-
color: Colors.red,
646-
fontFamily: "Source Code Pro", // TODO supply font
647-
fontFamilyFallback: ["monospace"],
648-
);
641+
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)