Skip to content

Commit

Permalink
Fix RangeSlider throws an exception in a ListView (#135667)
Browse files Browse the repository at this point in the history
fixes [[RangeSlider] [Flutter 3.10] LateInitializationError: Field '_startThumbCenter@280317193' has not been initialized.](flutter/flutter#126648)

### Code sample (Run it on iOS)

<details>
<summary>expand to view the code sample</summary> 

```dart
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @OverRide
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Example(),
    );
  }
}

class Example extends StatelessWidget {
  const Example({super.key});

  @OverRide
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: <Widget>[
          const SizedBox(
            height: 1000,
            child: Placeholder(),
          ),
          RangeSlider(
            values: const RangeValues(0.25, 0.75),
            onChanged: (value) {},
          ),
        ],
      ),
    );
  }
}
```

</details>
  • Loading branch information
TahaTesser authored Sep 28, 2023
1 parent 449c927 commit d134345
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/material/range_slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,8 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
late TapGestureRecognizer _tap;
bool _active = false;
late RangeValues _newValues;
late Offset _startThumbCenter;
late Offset _endThumbCenter;
Offset _startThumbCenter = Offset.zero;
Offset _endThumbCenter = Offset.zero;
Rect? overlayStartRect;
Rect? overlayEndRect;

Expand Down
32 changes: 31 additions & 1 deletion packages/flutter/test/material/range_slider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2541,7 +2541,7 @@ void main() {
);
});

testWidgetsWithLeakTracking('RangeSlider onChangeStart and onChangeEnd fire once', (WidgetTester tester) async {
testWidgetsWithLeakTracking('RangeSlider onChangeStart and onChangeEnd fire once', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/128433

int startFired = 0;
Expand Down Expand Up @@ -2581,4 +2581,34 @@ void main() {
expect(startFired, equals(1));
expect(endFired, equals(1));
});

testWidgetsWithLeakTracking('RangeSlider in a ListView does not throw an exception', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/126648

await tester.pumpWidget(
MaterialApp(
home: Directionality(
textDirection: TextDirection.ltr,
child: Material(
child: ListView(
children: <Widget>[
const SizedBox(
height: 600,
child: Placeholder(),
),
RangeSlider(
values: const RangeValues(40, 80),
max: 100,
onChanged: (RangeValues newValue) { },
),
],
),
),
),
),
);

// No exception should be thrown.
expect(tester.takeException(), null);
});
}

0 comments on commit d134345

Please sign in to comment.