From 04e0fcb00e2d3eb7c0838a9c35166fdca5ad70ee Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Tue, 5 Jun 2018 10:55:11 -0700 Subject: [PATCH] Fixed a typo in the render paragraph locale setter (#18189) --- .../flutter/lib/src/rendering/paragraph.dart | 19 ++++++++++++++----- packages/flutter/lib/src/widgets/basic.dart | 16 +++++++++++++--- .../test/rendering/paragraph_test.dart | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/rendering/paragraph.dart b/packages/flutter/lib/src/rendering/paragraph.dart index 189bf8a30d..7f813a68a3 100644 --- a/packages/flutter/lib/src/rendering/paragraph.dart +++ b/packages/flutter/lib/src/rendering/paragraph.dart @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui' as ui show Gradient, Shader, TextBox, Locale; +import 'dart:ui' as ui show Gradient, Shader, TextBox; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; @@ -44,7 +44,7 @@ class RenderParagraph extends RenderBox { TextOverflow overflow = TextOverflow.clip, double textScaleFactor = 1.0, int maxLines, - ui.Locale locale, + Locale locale, }) : assert(text != null), assert(text.debugAssertIsValid()), assert(textAlign != null), @@ -176,11 +176,19 @@ class RenderParagraph extends RenderBox { markNeedsLayout(); } - ui.Locale get locale => _textPainter.locale; - set locale(ui.Locale value) { + /// Used by this paragraph's internal [TextPainter] to select a locale-specific + /// font. + /// + /// In some cases the same Unicode character may be rendered differently depending + /// on the locale. For example the '骨' character is rendered differently in + /// the Chinese and Japanese locales. In these cases the [locale] may be used + /// to select a locale-specific font. + Locale get locale => _textPainter.locale; + /// The value may be null. + set locale(Locale value) { if (_textPainter.locale == value) return; - _textPainter.locale = locale; + _textPainter.locale = value; _overflowShader = null; markNeedsLayout(); } @@ -286,6 +294,7 @@ class RenderParagraph extends RenderBox { text: new TextSpan(style: _textPainter.text.style, text: '\u2026'), textDirection: textDirection, textScaleFactor: textScaleFactor, + locale: locale, )..layout(); if (didOverflowWidth) { double fadeEnd, fadeStart; diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index cea1d5dded..be5c27eeb0 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -235,7 +235,7 @@ class ShaderMask extends SingleChildRenderObjectWidget { /// it can customize the shader to the size and location of the child. /// /// Typically this will use a [LinearGradient], [RadialGradient], or - /// [SweepGradient] to create the [dart:ui.Shader], though the + /// [SweepGradient] to create the [dart:ui.Shader], though the /// [dart:ui.ImageShader] class could also be used. final ShaderCallback shaderCallback; @@ -4289,6 +4289,7 @@ class RichText extends LeafRenderObjectWidget { this.overflow = TextOverflow.clip, this.textScaleFactor = 1.0, this.maxLines, + this.locale, }) : assert(text != null), assert(textAlign != null), assert(softWrap != null), @@ -4341,6 +4342,15 @@ class RichText extends LeafRenderObjectWidget { /// edge of the box. final int maxLines; + /// Used to select a font when the same Unicode character can + /// be rendered differently, depending on the locale. + /// + /// It's rarely necessary to set this property. By default its value + /// is inherited from the enclosing app with `Localizations.localeOf(context)`. + /// + /// See [RenderParagraph.locale] for more information. + final Locale locale; + @override RenderParagraph createRenderObject(BuildContext context) { assert(textDirection != null || debugCheckHasDirectionality(context)); @@ -4351,7 +4361,7 @@ class RichText extends LeafRenderObjectWidget { overflow: overflow, textScaleFactor: textScaleFactor, maxLines: maxLines, - locale: Localizations.localeOf(context, nullOk: true), + locale: locale ?? Localizations.localeOf(context, nullOk: true), ); } @@ -4366,7 +4376,7 @@ class RichText extends LeafRenderObjectWidget { ..overflow = overflow ..textScaleFactor = textScaleFactor ..maxLines = maxLines - ..locale = Localizations.localeOf(context, nullOk: true); + ..locale = locale ?? Localizations.localeOf(context, nullOk: true); } @override diff --git a/packages/flutter/test/rendering/paragraph_test.dart b/packages/flutter/test/rendering/paragraph_test.dart index 15810bd55f..ca8bd3f0ef 100644 --- a/packages/flutter/test/rendering/paragraph_test.dart +++ b/packages/flutter/test/rendering/paragraph_test.dart @@ -310,4 +310,19 @@ void main() { ), ); }); + + test('locale setter', () { + // Regression test for https://github.com/flutter/flutter/issues/18175 + + final RenderParagraph paragraph = new RenderParagraph( + const TextSpan(text: _kText), + locale: const Locale('zh', 'HK'), + textDirection: TextDirection.ltr, + ); + expect(paragraph.locale, const Locale('zh', 'HK')); + + paragraph.locale = const Locale('ja', 'JP'); + expect(paragraph.locale, const Locale('ja', 'JP')); + }); + }