From fb9ff62cf494141ec7302a457bd3a7a42dfa9069 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Fri, 29 Jan 2021 14:26:06 -0800 Subject: [PATCH] InputDecorator negative baseline error message (#74837) --- .../lib/src/material/input_decorator.dart | 21 ++++++++-- .../test/material/input_decorator_test.dart | 39 +++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index 159b2cd488..1d0a3e85b5 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -935,9 +935,20 @@ class _RenderDecoration extends RenderBox { // baseline from the alphabetic baseline. The ideographic baseline is for // use post-layout and is derived from the alphabetic baseline combined with // the font metrics. - final double? baseline = box.getDistanceToBaseline(TextBaseline.alphabetic); - assert(baseline != null && baseline >= 0.0); - return baseline!; + final double baseline = box.getDistanceToBaseline(TextBaseline.alphabetic)!; + + assert(() { + if (baseline >= 0) + return true; + throw FlutterError.fromParts([ + ErrorSummary("One of InputDecorator's children reported a negative baseline offset."), + ErrorDescription( + '${box.runtimeType}, of size ${box.size}, reported a negative ' + 'alphabetic baseline of $baseline.', + ), + ]); + }()); + return baseline; } // Returns a value used by performLayout to position all of the renderers. @@ -1767,7 +1778,9 @@ class _AffixText extends StatelessWidget { /// [InputDecorator] can be used to create widgets that look and behave like a /// [TextField] but support other kinds of input. /// -/// Requires one of its ancestors to be a [Material] widget. +/// Requires one of its ancestors to be a [Material] widget. The [child] widget, +/// as well as the decorative widgets specified in [decoration], must have +/// non-negative baselines. /// /// See also: /// diff --git a/packages/flutter/test/material/input_decorator_test.dart b/packages/flutter/test/material/input_decorator_test.dart index a64540183d..11ad7b6194 100644 --- a/packages/flutter/test/material/input_decorator_test.dart +++ b/packages/flutter/test/material/input_decorator_test.dart @@ -4,6 +4,7 @@ import 'dart:async'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -4420,4 +4421,42 @@ void main() { final double intrinsicHeight = tester.getSize(find.byKey(intrinsicHeightKey)).height; expect(intrinsicHeight, equals(height)); }); + + testWidgets('error message for negative baseline', (WidgetTester tester) async { + FlutterErrorDetails? errorDetails; + final FlutterExceptionHandler? oldHandler = FlutterError.onError; + FlutterError.onError = (FlutterErrorDetails details) { + errorDetails ??= details; + }; + try { + await tester.pumpWidget( + MaterialApp( + home: Center( + child: Directionality( + textDirection: TextDirection.ltr, + child: InputDecorator( + decoration: const InputDecoration(), + child: Stack( + children: const [ + SizedBox(height: 0), + Positioned( + bottom: 5, + child: Text('ok'), + ), + ], + ), + ), + ), + ), + ), + null, + EnginePhase.layout, + ); + } finally { + FlutterError.onError = oldHandler; + } + + expect(errorDetails?.toString(), contains("InputDecorator's children reported a negative baseline")); + expect(errorDetails?.toString(), contains('RenderStack')); + }); }