Fix RangeSlider throws a null-check error after clearSemantics is called (#141965)

fixes [Null-check operator on RangeSlider's _startSemanticsNode](https://github.com/flutter/flutter/issues/141953)
This commit is contained in:
Taha Tesser 2024-01-22 20:21:31 +02:00 committed by GitHub
parent cf8d20f70f
commit 5aa6cb857d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 2 deletions

View File

@ -1628,10 +1628,10 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
}
/// Describe the semantics of the start thumb.
SemanticsNode? _startSemanticsNode = SemanticsNode();
SemanticsNode? _startSemanticsNode;
/// Describe the semantics of the end thumb.
SemanticsNode? _endSemanticsNode = SemanticsNode();
SemanticsNode? _endSemanticsNode;
// Create the semantics configuration for a single value.
SemanticsConfiguration _createSemanticsConfiguration(
@ -1697,6 +1697,10 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
width: kMinInteractiveDimension,
height: kMinInteractiveDimension,
);
_startSemanticsNode ??= SemanticsNode();
_endSemanticsNode ??= SemanticsNode();
switch (textDirection) {
case TextDirection.ltr:
_startSemanticsNode!.rect = leftRect;

View File

@ -9,6 +9,8 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/src/physics/utils.dart' show nearEqual;
import 'package:flutter_test/flutter_test.dart';
import '../widgets/semantics_tester.dart';
void main() {
// Regression test for https://github.com/flutter/flutter/issues/105833
testWidgets('Drag gesture uses provided gesture settings', (WidgetTester tester) async {
@ -2610,4 +2612,36 @@ void main() {
// No exception should be thrown.
expect(tester.takeException(), null);
});
// This is a regression test for https://github.com/flutter/flutter/issues/141953.
testWidgets('Semantic nodes do not throw an error after clearSemantics', (WidgetTester tester) async {
SemanticsTester semantics = SemanticsTester(tester);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Material(
child: RangeSlider(
values: const RangeValues(40, 80),
max: 100,
onChanged: (RangeValues newValue) { },
),
),
),
);
// Dispose the semantics to trigger clearSemantics.
semantics.dispose();
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
// Initialize the semantics again.
semantics = SemanticsTester(tester);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
semantics.dispose();
}, semanticsEnabled: false);
}