slider_test.dart 46.6 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:ui';

7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/material.dart';
9
import 'package:flutter/rendering.dart';
10 11 12
import 'package:flutter/scheduler.dart';
import 'package:flutter_test/flutter_test.dart';

13
import '../rendering/mock_canvas.dart';
14
import '../widgets/semantics_tester.dart';
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29
// A thumb shape that also logs its repaint center.
class LoggingThumbShape extends SliderComponentShape {
  LoggingThumbShape(this.log);

  final List<Offset> log;

  @override
  Size getPreferredSize(bool isEnabled, bool isDiscrete) {
    return const Size(10.0, 10.0);
  }

  @override
  void paint(
    PaintingContext context,
30
    Offset thumbCenter, {
31 32
    Animation<double> activationAnimation,
    Animation<double> enableAnimation,
33
    bool isDiscrete,
34
    TextPainter labelPainter,
35
    RenderBox parentBox,
36 37 38
    SliderThemeData sliderTheme,
    TextDirection textDirection,
    double value,
39
  }) {
40
    log.add(thumbCenter);
41
    final Paint thumbPaint = Paint()..color = Colors.red;
42 43 44 45
    context.canvas.drawCircle(thumbCenter, 5.0, thumbPaint);
  }
}

46
void main() {
47
  testWidgets('Slider can move when tapped (LTR)', (WidgetTester tester) async {
48
    final Key sliderKey = UniqueKey();
49
    double value = 0.0;
50 51
    double startValue;
    double endValue;
52

53
    await tester.pumpWidget(
54
      Directionality(
55
        textDirection: TextDirection.ltr,
56
        child: StatefulBuilder(
57
          builder: (BuildContext context, StateSetter setState) {
58 59 60 61 62
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
                child: Center(
                  child: Slider(
63 64 65 66 67 68 69
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() {
                        value = newValue;
                      });
                    },
70 71 72 73 74 75
                    onChangeStart: (double value) {
                      startValue = value;
                    },
                    onChangeEnd: (double value) {
                      endValue = value;
                    },
76
                  ),
77
                ),
78
              ),
79 80 81
            );
          },
        ),
82
      ),
83
    );
84

85
    expect(value, equals(0.0));
86
    await tester.tap(find.byKey(sliderKey));
87
    expect(value, equals(0.5));
88 89 90 91
    expect(startValue, equals(0.0));
    expect(endValue, equals(0.5));
    startValue = null;
    endValue = null;
92
    await tester.pump(); // No animation should start.
93
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
94 95 96 97 98 99 100

    final Offset topLeft = tester.getTopLeft(find.byKey(sliderKey));
    final Offset bottomRight = tester.getBottomRight(find.byKey(sliderKey));

    final Offset target = topLeft + (bottomRight - topLeft) / 4.0;
    await tester.tapAt(target);
    expect(value, closeTo(0.25, 0.05));
101 102
    expect(startValue, equals(0.5));
    expect(endValue, closeTo(0.25, 0.05));
103 104
    await tester.pump(); // No animation should start.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
105 106
  });

107
  testWidgets('Slider can move when tapped (RTL)', (WidgetTester tester) async {
108
    final Key sliderKey = UniqueKey();
109
    double value = 0.0;
110

111
    await tester.pumpWidget(
112
      Directionality(
113
        textDirection: TextDirection.rtl,
114
        child: StatefulBuilder(
115
          builder: (BuildContext context, StateSetter setState) {
116 117 118 119 120
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
                child: Center(
                  child: Slider(
121 122 123 124 125 126 127 128
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() {
                        value = newValue;
                      });
                    },
                  ),
129
                ),
130
              ),
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
            );
          },
        ),
      ),
    );

    expect(value, equals(0.0));
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.5));
    await tester.pump(); // No animation should start.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));

    final Offset topLeft = tester.getTopLeft(find.byKey(sliderKey));
    final Offset bottomRight = tester.getBottomRight(find.byKey(sliderKey));

    final Offset target = topLeft + (bottomRight - topLeft) / 4.0;
    await tester.tapAt(target);
    expect(value, closeTo(0.75, 0.05));
    await tester.pump(); // No animation should start.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
  });

153
  testWidgets("Slider doesn't send duplicate change events if tapped on the same value", (WidgetTester tester) async {
154
    final Key sliderKey = UniqueKey();
155
    double value = 0.0;
156 157
    double startValue;
    double endValue;
158
    int updates = 0;
159 160
    int startValueUpdates = 0;
    int endValueUpdates = 0;
161 162

    await tester.pumpWidget(
163
      Directionality(
164
        textDirection: TextDirection.ltr,
165
        child: StatefulBuilder(
166
          builder: (BuildContext context, StateSetter setState) {
167 168 169 170 171
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
                child: Center(
                  child: Slider(
172 173 174 175
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() {
176
                        updates++;
177 178 179
                        value = newValue;
                      });
                    },
180 181 182 183 184 185 186 187
                    onChangeStart: (double value) {
                      startValueUpdates++;
                      startValue = value;
                    },
                    onChangeEnd: (double value) {
                      endValueUpdates++;
                      endValue = value;
                    },
188 189 190 191 192 193
                  ),
                ),
              ),
            );
          },
        ),
194
      ),
195
    );
196

197 198 199
    expect(value, equals(0.0));
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.5));
200 201
    expect(startValue, equals(0.0));
    expect(endValue, equals(0.5));
202 203 204 205 206
    await tester.pump();
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.5));
    await tester.pump();
    expect(updates, equals(1));
207 208
    expect(startValueUpdates, equals(2));
    expect(endValueUpdates, equals(2));
209 210
  });

211
  testWidgets('Value indicator shows for a bit after being tapped', (WidgetTester tester) async {
212
    final Key sliderKey = UniqueKey();
213 214 215
    double value = 0.0;

    await tester.pumpWidget(
216
      Directionality(
217
        textDirection: TextDirection.ltr,
218
        child: StatefulBuilder(
219
          builder: (BuildContext context, StateSetter setState) {
220 221 222 223 224
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
                child: Center(
                  child: Slider(
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                    key: sliderKey,
                    value: value,
                    divisions: 4,
                    onChanged: (double newValue) {
                      setState(() {
                        value = newValue;
                      });
                    },
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(value, equals(0.0));
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.5));
    await tester.pump(const Duration(milliseconds: 100));
    // Starts with the position animation and value indicator
    expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
    await tester.pump(const Duration(milliseconds: 100));
    // Value indicator is longer than position.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(1));
    await tester.pump(const Duration(milliseconds: 100));
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
    await tester.pump(const Duration(milliseconds: 100));
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
    await tester.pump(const Duration(milliseconds: 100));
    // Shown for long enough, value indicator is animated closed.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(1));
    await tester.pump(const Duration(milliseconds: 101));
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
  });

  testWidgets('Discrete Slider repaints and animates when dragged', (WidgetTester tester) async {
263
    final Key sliderKey = UniqueKey();
264 265
    double value = 0.0;
    final List<Offset> log = <Offset>[];
266
    final LoggingThumbShape loggingThumb = LoggingThumbShape(log);
267
    await tester.pumpWidget(
268
      Directionality(
269
        textDirection: TextDirection.ltr,
270
        child: StatefulBuilder(
271 272
          builder: (BuildContext context, StateSetter setState) {
            final SliderThemeData sliderTheme = SliderTheme.of(context).copyWith(thumbShape: loggingThumb);
273 274 275 276 277
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
                child: Center(
                  child: SliderTheme(
278
                    data: sliderTheme,
279
                    child: Slider(
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
                      key: sliderKey,
                      value: value,
                      divisions: 4,
                      onChanged: (double newValue) {
                        setState(() {
                          value = newValue;
                        });
                      },
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );

    final List<Offset> expectedLog = <Offset>[
      const Offset(16.0, 300.0),
      const Offset(16.0, 300.0),
      const Offset(400.0, 300.0),
    ];
    final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byKey(sliderKey)));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));
    expect(value, equals(0.5));
    expect(log.length, 3);
    expect(log, orderedEquals(expectedLog));
    await gesture.moveBy(const Offset(-500.0, 0.0));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 10));
    expect(value, equals(0.0));
    expect(log.length, 5);
314 315 316 317 318 319 320
    expect(log.last.dx, closeTo(386.3, 0.1));
    // With no more gesture or value changes, the thumb position should still
    // be redrawn in the animated position.
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 10));
    expect(value, equals(0.0));
    expect(log.length, 7);
321
    expect(log.last.dx, closeTo(343.3, 0.1));
322 323 324 325 326 327 328 329 330 331
    // Final position.
    await tester.pump(const Duration(milliseconds: 80));
    expectedLog.add(const Offset(16.0, 300.0));
    expect(value, equals(0.0));
    expect(log.length, 8);
    expect(log.last.dx, closeTo(16.0, 0.1));
    await gesture.up();
  });

  testWidgets("Slider doesn't send duplicate change events if tapped on the same value", (WidgetTester tester) async {
332
    final Key sliderKey = UniqueKey();
333 334 335 336
    double value = 0.0;
    int updates = 0;

    await tester.pumpWidget(
337
      Directionality(
338
        textDirection: TextDirection.ltr,
339
        child: StatefulBuilder(
340
          builder: (BuildContext context, StateSetter setState) {
341 342 343 344 345
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
                child: Center(
                  child: Slider(
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() {
                        updates++;
                        value = newValue;
                      });
                    },
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(value, equals(0.0));
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.5));
    await tester.pump();
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.5));
    await tester.pump();
    expect(updates, equals(1));
  });

  testWidgets('discrete Slider repaints when dragged', (WidgetTester tester) async {
374
    final Key sliderKey = UniqueKey();
375 376
    double value = 0.0;
    final List<Offset> log = <Offset>[];
377
    final LoggingThumbShape loggingThumb = LoggingThumbShape(log);
378
    await tester.pumpWidget(
379
      Directionality(
380
        textDirection: TextDirection.ltr,
381
        child: StatefulBuilder(
382 383
          builder: (BuildContext context, StateSetter setState) {
            final SliderThemeData sliderTheme = SliderTheme.of(context).copyWith(thumbShape: loggingThumb);
384 385 386 387 388
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
                child: Center(
                  child: SliderTheme(
389
                    data: sliderTheme,
390
                    child: Slider(
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
                      key: sliderKey,
                      value: value,
                      divisions: 4,
                      onChanged: (double newValue) {
                        setState(() {
                          value = newValue;
                        });
                      },
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );

    final List<Offset> expectedLog = <Offset>[
      const Offset(16.0, 300.0),
      const Offset(16.0, 300.0),
      const Offset(400.0, 300.0),
    ];
    final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byKey(sliderKey)));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));
    expect(value, equals(0.5));
    expect(log.length, 3);
    expect(log, orderedEquals(expectedLog));
    await gesture.moveBy(const Offset(-500.0, 0.0));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 10));
    expect(value, equals(0.0));
    expect(log.length, 5);
    expect(log.last.dx, closeTo(386.3, 0.1));
426 427 428 429 430 431
    // With no more gesture or value changes, the thumb position should still
    // be redrawn in the animated position.
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 10));
    expect(value, equals(0.0));
    expect(log.length, 7);
432
    expect(log.last.dx, closeTo(343.3, 0.1));
433 434 435 436 437 438 439 440 441 442
    // Final position.
    await tester.pump(const Duration(milliseconds: 80));
    expectedLog.add(const Offset(16.0, 300.0));
    expect(value, equals(0.0));
    expect(log.length, 8);
    expect(log.last.dx, closeTo(16.0, 0.1));
    await gesture.up();
  });

  testWidgets('Slider take on discrete values', (WidgetTester tester) async {
443
    final Key sliderKey = UniqueKey();
444 445 446
    double value = 0.0;

    await tester.pumpWidget(
447
      Directionality(
448
        textDirection: TextDirection.ltr,
449
        child: StatefulBuilder(
450
          builder: (BuildContext context, StateSetter setState) {
451 452 453 454 455
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
                child: Center(
                  child: SizedBox(
456
                    width: 144.0 + 2 * 16.0, // _kPreferredTotalWidth
457
                    child: Slider(
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
                      key: sliderKey,
                      min: 0.0,
                      max: 100.0,
                      divisions: 10,
                      value: value,
                      onChanged: (double newValue) {
                        setState(() {
                          value = newValue;
                        });
                      },
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );

478
    expect(value, equals(0.0));
479
    await tester.tap(find.byKey(sliderKey));
480
    expect(value, equals(50.0));
481
    await tester.drag(find.byKey(sliderKey), const Offset(5.0, 0.0));
482
    expect(value, equals(50.0));
483
    await tester.drag(find.byKey(sliderKey), const Offset(40.0, 0.0));
484
    expect(value, equals(80.0));
485

486
    await tester.pump(); // Starts animation.
487
    expect(SchedulerBinding.instance.transientCallbackCount, greaterThan(0));
488 489 490 491
    await tester.pump(const Duration(milliseconds: 200));
    await tester.pump(const Duration(milliseconds: 200));
    await tester.pump(const Duration(milliseconds: 200));
    await tester.pump(const Duration(milliseconds: 200));
492 493
    // Animation complete.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
494
  });
495

496
  testWidgets('Slider can be given zero values', (WidgetTester tester) async {
497
    final List<double> log = <double>[];
498
    await tester.pumpWidget(Directionality(
499
      textDirection: TextDirection.ltr,
500 501 502 503
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
        child: Material(
          child: Slider(
504 505 506 507 508 509 510
            value: 0.0,
            min: 0.0,
            max: 1.0,
            onChanged: (double newValue) {
              log.add(newValue);
            },
          ),
511
        ),
512 513 514 515 516 517 518
      ),
    ));

    await tester.tap(find.byType(Slider));
    expect(log, <double>[0.5]);
    log.clear();

519
    await tester.pumpWidget(Directionality(
520
      textDirection: TextDirection.ltr,
521 522 523 524
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
        child: Material(
          child: Slider(
525 526 527 528 529 530 531
            value: 0.0,
            min: 0.0,
            max: 0.0,
            onChanged: (double newValue) {
              log.add(newValue);
            },
          ),
532
        ),
533 534 535 536 537 538 539
      ),
    ));

    await tester.tap(find.byType(Slider));
    expect(log, <double>[]);
    log.clear();
  });
540

541
  testWidgets('Slider uses the right theme colors for the right components', (WidgetTester tester) async {
542 543
    const Color customColor1 = Color(0xcafefeed);
    const Color customColor2 = Color(0xdeadbeef);
544
    final ThemeData theme = ThemeData(
545 546 547 548 549 550 551 552 553
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme;
    double value = 0.45;
    Widget buildApp({
      Color activeColor,
      Color inactiveColor,
      int divisions,
554
      bool enabled = true,
555 556 557 558 559 560
    }) {
      final ValueChanged<double> onChanged = !enabled
          ? null
          : (double d) {
              value = d;
            };
561
      return Directionality(
562
        textDirection: TextDirection.ltr,
563 564 565 566 567
        child: MediaQuery(
          data: MediaQueryData.fromWindow(window),
          child: Material(
            child: Center(
              child: Theme(
568
                data: theme,
569
                child: Slider(
570 571 572 573 574 575 576
                  value: value,
                  label: '$value',
                  divisions: divisions,
                  activeColor: activeColor,
                  inactiveColor: inactiveColor,
                  onChanged: onChanged,
                ),
577
              ),
578 579 580 581 582 583
            ),
          ),
        ),
      );
    }

584
    await tester.pumpWidget(buildApp());
585

586
    final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(Slider));
587

588
    // Check default theme for enabled widget.
589
    expect(sliderBox, paints..rect(color: sliderTheme.activeTrackColor)..rect(color: sliderTheme.inactiveTrackColor));
590 591
    expect(sliderBox, paints..circle(color: sliderTheme.thumbColor));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
592 593
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor)));
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor)));
594 595 596 597 598
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.activeTickMarkColor)));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.inactiveTickMarkColor)));

    // Test setting only the activeColor.
    await tester.pumpWidget(buildApp(activeColor: customColor1));
599
    expect(sliderBox, paints..rect(color: customColor1)..rect(color: sliderTheme.inactiveTrackColor));
600 601 602
    expect(sliderBox, paints..circle(color: customColor1));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor)));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
603 604
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor)));
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor)));
605 606 607

    // Test setting only the inactiveColor.
    await tester.pumpWidget(buildApp(inactiveColor: customColor1));
608
    expect(sliderBox, paints..rect(color: sliderTheme.activeTrackColor)..rect(color: customColor1));
609 610
    expect(sliderBox, paints..circle(color: sliderTheme.thumbColor));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
611 612
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor)));
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor)));
613 614 615 616 617 618 619

    // Test setting both activeColor and inactiveColor.
    await tester.pumpWidget(buildApp(activeColor: customColor1, inactiveColor: customColor2));
    expect(sliderBox, paints..rect(color: customColor1)..rect(color: customColor2));
    expect(sliderBox, paints..circle(color: customColor1));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor)));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
620 621
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor)));
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor)));
622 623 624

    // Test colors for discrete slider.
    await tester.pumpWidget(buildApp(divisions: 3));
625
    expect(sliderBox, paints..rect(color: sliderTheme.activeTrackColor)..rect(color: sliderTheme.inactiveTrackColor));
626 627 628 629 630 631 632 633 634
    expect(
        sliderBox,
        paints
          ..circle(color: sliderTheme.activeTickMarkColor)
          ..circle(color: sliderTheme.activeTickMarkColor)
          ..circle(color: sliderTheme.inactiveTickMarkColor)
          ..circle(color: sliderTheme.inactiveTickMarkColor)
          ..circle(color: sliderTheme.thumbColor));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
635 636
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor)));
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor)));
637 638

    // Test colors for discrete slider with inactiveColor and activeColor set.
639 640 641 642 643
    await tester.pumpWidget(buildApp(
      activeColor: customColor1,
      inactiveColor: customColor2,
      divisions: 3,
    ));
644 645 646 647 648 649 650 651 652 653 654
    expect(sliderBox, paints..rect(color: customColor1)..rect(color: customColor2));
    expect(
        sliderBox,
        paints
          ..circle(color: customColor2)
          ..circle(color: customColor2)
          ..circle(color: customColor1)
          ..circle(color: customColor1)
          ..circle(color: customColor1));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor)));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
655 656
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor)));
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor)));
657 658 659 660 661
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.activeTickMarkColor)));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.inactiveTickMarkColor)));

    // Test default theme for disabled widget.
    await tester.pumpWidget(buildApp(enabled: false));
662
    await tester.pumpAndSettle();
663 664 665
    expect(
        sliderBox,
        paints
666 667
          ..rect(color: sliderTheme.disabledActiveTrackColor)
          ..rect(color: sliderTheme.disabledInactiveTrackColor));
668 669
    expect(sliderBox, paints..circle(color: sliderTheme.disabledThumbColor));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor)));
670 671
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.activeTrackColor)));
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.inactiveTrackColor)));
672 673

    // Test setting the activeColor and inactiveColor for disabled widget.
674
    await tester.pumpWidget(buildApp(activeColor: customColor1, inactiveColor: customColor2, enabled: false));
675 676 677
    expect(
        sliderBox,
        paints
678 679
          ..rect(color: sliderTheme.disabledActiveTrackColor)
          ..rect(color: sliderTheme.disabledInactiveTrackColor));
680 681
    expect(sliderBox, paints..circle(color: sliderTheme.disabledThumbColor));
    expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor)));
682 683
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.activeTrackColor)));
    expect(sliderBox, isNot(paints..rect(color: sliderTheme.inactiveTrackColor)));
684 685 686 687 688

    // Test that the default value indicator has the right colors.
    await tester.pumpWidget(buildApp(divisions: 3));
    Offset center = tester.getCenter(find.byType(Slider));
    TestGesture gesture = await tester.startGesture(center);
689
    // Wait for value indicator animation to finish.
690
    await tester.pumpAndSettle();
691 692 693 694
    expect(value, equals(2.0 / 3.0));
    expect(
      sliderBox,
      paints
695 696
        ..rect(color: sliderTheme.activeTrackColor)
        ..rect(color: sliderTheme.inactiveTrackColor)
697 698 699 700 701 702 703 704 705
        ..circle(color: sliderTheme.overlayColor)
        ..circle(color: sliderTheme.activeTickMarkColor)
        ..circle(color: sliderTheme.activeTickMarkColor)
        ..circle(color: sliderTheme.inactiveTickMarkColor)
        ..circle(color: sliderTheme.inactiveTickMarkColor)
        ..path(color: sliderTheme.valueIndicatorColor)
        ..circle(color: sliderTheme.thumbColor),
    );
    await gesture.up();
706
    // Wait for value indicator animation to finish.
707
    await tester.pumpAndSettle();
708 709 710 711 712 713 714 715 716

    // Testing the custom colors are used for the indicator.
    await tester.pumpWidget(buildApp(
      divisions: 3,
      activeColor: customColor1,
      inactiveColor: customColor2,
    ));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
717
    // Wait for value indicator animation to finish.
718
    await tester.pumpAndSettle();
719 720 721 722 723 724 725 726 727 728 729 730 731 732
    expect(value, equals(2.0 / 3.0));
    expect(
      sliderBox,
      paints
        ..rect(color: customColor1)
        ..rect(color: customColor2)
        ..circle(color: customColor1.withAlpha(0x29))
        ..circle(color: customColor2)
        ..circle(color: customColor2)
        ..circle(color: customColor1)
        ..path(color: customColor1)
        ..circle(color: customColor1),
    );
    await gesture.up();
733
  });
734

735
  testWidgets('Slider can tap in vertical scroller', (WidgetTester tester) async {
736
    double value = 0.0;
737
    await tester.pumpWidget(Directionality(
738
      textDirection: TextDirection.ltr,
739 740 741 742
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
        child: Material(
          child: ListView(
743
            children: <Widget>[
744
              Slider(
745 746 747 748 749
                value: value,
                onChanged: (double newValue) {
                  value = newValue;
                },
              ),
750
              Container(
751 752 753 754
                height: 2000.0,
              ),
            ],
          ),
755 756 757 758 759 760 761 762 763 764
        ),
      ),
    ));

    await tester.tap(find.byType(Slider));
    expect(value, equals(0.5));
  });

  testWidgets('Slider drags immediately (LTR)', (WidgetTester tester) async {
    double value = 0.0;
765
    await tester.pumpWidget(Directionality(
766
      textDirection: TextDirection.ltr,
767 768 769 770 771
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
        child: Material(
          child: Center(
            child: Slider(
772 773 774 775 776
              value: value,
              onChanged: (double newValue) {
                value = newValue;
              },
            ),
777
          ),
778
        ),
779 780 781
      ),
    ));

782 783 784
    final Offset center = tester.getCenter(find.byType(Slider));
    final TestGesture gesture = await tester.startGesture(center);

785
    expect(value, equals(0.5));
786 787 788 789 790 791

    await gesture.moveBy(const Offset(1.0, 0.0));

    expect(value, greaterThan(0.5));

    await gesture.up();
792 793
  });

794
  testWidgets('Slider drags immediately (RTL)', (WidgetTester tester) async {
795
    double value = 0.0;
796
    await tester.pumpWidget(Directionality(
797
      textDirection: TextDirection.rtl,
798 799 800 801 802
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
        child: Material(
          child: Center(
            child: Slider(
803 804 805 806 807
              value: value,
              onChanged: (double newValue) {
                value = newValue;
              },
            ),
808
          ),
809 810 811 812
        ),
      ),
    ));

813
    final Offset center = tester.getCenter(find.byType(Slider));
814
    final TestGesture gesture = await tester.startGesture(center);
815 816 817

    expect(value, equals(0.5));

818
    await gesture.moveBy(const Offset(1.0, 0.0));
819

820
    expect(value, lessThan(0.5));
821 822 823

    await gesture.up();
  });
824 825

  testWidgets('Slider sizing', (WidgetTester tester) async {
826
    await tester.pumpWidget(Directionality(
827
      textDirection: TextDirection.ltr,
828 829
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
830
        child: const Material(
831 832
          child: Center(
            child: Slider(
833 834 835
              value: 0.5,
              onChanged: null,
            ),
836
          ),
837 838 839 840 841
        ),
      ),
    ));
    expect(tester.renderObject<RenderBox>(find.byType(Slider)).size, const Size(800.0, 600.0));

842
    await tester.pumpWidget(Directionality(
843
      textDirection: TextDirection.ltr,
844 845
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
846
        child: const Material(
847 848 849
          child: Center(
            child: IntrinsicWidth(
              child: Slider(
850 851 852
                value: 0.5,
                onChanged: null,
              ),
853
            ),
854 855 856 857
          ),
        ),
      ),
    ));
858
    expect(tester.renderObject<RenderBox>(find.byType(Slider)).size, const Size(144.0 + 2.0 * 16.0, 600.0));
859

860
    await tester.pumpWidget(Directionality(
861
      textDirection: TextDirection.ltr,
862 863
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
864
        child: const Material(
865 866
          child: Center(
            child: OverflowBox(
867 868
              maxWidth: double.infinity,
              maxHeight: double.infinity,
869
              child: Slider(
870 871 872
                value: 0.5,
                onChanged: null,
              ),
873
            ),
874 875 876 877
          ),
        ),
      ),
    ));
878
    expect(tester.renderObject<RenderBox>(find.byType(Slider)).size, const Size(144.0 + 2.0 * 16.0, 32.0));
879
  });
880

881
  testWidgets('Slider respects textScaleFactor', (WidgetTester tester) async {
882
    final Key sliderKey = UniqueKey();
883 884
    double value = 0.0;

885 886
    Widget buildSlider({
      double textScaleFactor,
887 888
      bool isDiscrete = true,
      ShowValueIndicator show = ShowValueIndicator.onlyForDiscrete,
889
    }) {
890
      return Directionality(
891
        textDirection: TextDirection.ltr,
892
        child: StatefulBuilder(
893
          builder: (BuildContext context, StateSetter setState) {
894 895 896 897
            return MediaQuery(
              data: MediaQueryData(textScaleFactor: textScaleFactor),
              child: Material(
                child: Theme(
898
                  data: Theme.of(context).copyWith(
899 900
                        sliderTheme: Theme.of(context).sliderTheme.copyWith(showValueIndicator: show),
                      ),
901 902
                  child: Center(
                    child: OverflowBox(
903 904
                      maxWidth: double.infinity,
                      maxHeight: double.infinity,
905
                      child: Slider(
906 907 908 909 910 911 912 913 914 915 916 917
                        key: sliderKey,
                        min: 0.0,
                        max: 100.0,
                        divisions: isDiscrete ? 10 : null,
                        label: '${value.round()}',
                        value: value,
                        onChanged: (double newValue) {
                          setState(() {
                            value = newValue;
                          });
                        },
                      ),
918 919 920 921 922 923 924 925 926 927 928 929 930
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      );
    }

    await tester.pumpWidget(buildSlider(textScaleFactor: 1.0));
    Offset center = tester.getCenter(find.byType(Slider));
    TestGesture gesture = await tester.startGesture(center);
931
    await tester.pumpAndSettle();
932

933
    expect(tester.renderObject(find.byType(Slider)), paints..scale(x: 1.0, y: 1.0));
934 935

    await gesture.up();
936
    await tester.pumpAndSettle();
937 938 939 940

    await tester.pumpWidget(buildSlider(textScaleFactor: 2.0));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
941
    await tester.pumpAndSettle();
942

943 944 945
    expect(tester.renderObject(find.byType(Slider)), paints..scale(x: 2.0, y: 2.0));

    await gesture.up();
946
    await tester.pumpAndSettle();
947 948 949 950 951 952 953 954 955

    // Check continuous
    await tester.pumpWidget(buildSlider(
      textScaleFactor: 1.0,
      isDiscrete: false,
      show: ShowValueIndicator.onlyForContinuous,
    ));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
956
    await tester.pumpAndSettle();
957 958 959 960

    expect(tester.renderObject(find.byType(Slider)), paints..scale(x: 1.0, y: 1.0));

    await gesture.up();
961
    await tester.pumpAndSettle();
962 963 964 965 966 967 968 969

    await tester.pumpWidget(buildSlider(
      textScaleFactor: 2.0,
      isDiscrete: false,
      show: ShowValueIndicator.onlyForContinuous,
    ));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
970
    await tester.pumpAndSettle();
971 972

    expect(tester.renderObject(find.byType(Slider)), paints..scale(x: 2.0, y: 2.0));
973 974

    await gesture.up();
975
    await tester.pumpAndSettle();
976 977
  });

978
  testWidgets('Slider has correct animations when reparented', (WidgetTester tester) async {
979
    final Key sliderKey = GlobalKey(debugLabel: 'A');
980 981 982 983
    double value = 0.0;

    Widget buildSlider(int parents) {
      Widget createParents(int parents, StateSetter setState) {
984
        Widget slider = Slider(
985 986 987 988 989 990 991 992 993 994 995
          key: sliderKey,
          value: value,
          divisions: 4,
          onChanged: (double newValue) {
            setState(() {
              value = newValue;
            });
          },
        );

        for (int i = 0; i < parents; ++i) {
996
          slider = Column(children: <Widget>[slider]);
997 998 999 1000
        }
        return slider;
      }

1001
      return Directionality(
1002
        textDirection: TextDirection.ltr,
1003
        child: StatefulBuilder(
1004
          builder: (BuildContext context, StateSetter setState) {
1005 1006 1007
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
1008 1009 1010 1011 1012 1013 1014 1015
                child: createParents(parents, setState),
              ),
            );
          },
        ),
      );
    }

1016
    Future<void> testReparenting(bool reparent) async {
1017 1018 1019 1020 1021 1022
      final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(Slider));
      final Offset center = tester.getCenter(find.byType(Slider));
      // Move to 0.0.
      TestGesture gesture = await tester.startGesture(Offset.zero);
      await tester.pump();
      await gesture.up();
1023
      await tester.pumpAndSettle();
1024 1025 1026 1027 1028 1029 1030 1031
      expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
      expect(
        sliderBox,
        paints
          ..circle(x: 208.5, y: 16.0, radius: 1.0)
          ..circle(x: 400.0, y: 16.0, radius: 1.0)
          ..circle(x: 591.5, y: 16.0, radius: 1.0)
          ..circle(x: 783.0, y: 16.0, radius: 1.0)
1032
          ..circle(x: 16.0, y: 16.0, radius: 6.0),
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
      );

      gesture = await tester.startGesture(center);
      await tester.pump();
      // Wait for animations to start.
      await tester.pump(const Duration(milliseconds: 25));
      expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
      expect(
        sliderBox,
        paints
1043
          ..circle(x: 105.0625, y: 16.0, radius: 3.791776657104492)
1044 1045 1046 1047 1048
          ..circle(x: 17.0, y: 16.0, radius: 1.0)
          ..circle(x: 208.5, y: 16.0, radius: 1.0)
          ..circle(x: 400.0, y: 16.0, radius: 1.0)
          ..circle(x: 591.5, y: 16.0, radius: 1.0)
          ..circle(x: 783.0, y: 16.0, radius: 1.0)
1049
          ..circle(x: 105.0625, y: 16.0, radius: 6.0),
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
      );

      // Reparenting in the middle of an animation should do nothing.
      if (reparent) {
        await tester.pumpWidget(buildSlider(2));
      }

      // Move a little further in the animations.
      await tester.pump(const Duration(milliseconds: 10));
      expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
      expect(
        sliderBox,
        paints
1063
          ..circle(x: 185.5457763671875, y: 16.0, radius: 8.0)
1064 1065
          ..circle(x: 17.0, y: 16.0, radius: 1.0)
          ..circle(x: 208.5, y: 16.0, radius: 1.0)
1066
          ..circle(x: 400.0, y: 16.0, radius: 1.0)
1067 1068
          ..circle(x: 591.5, y: 16.0, radius: 1.0)
          ..circle(x: 783.0, y: 16.0, radius: 1.0)
1069
          ..circle(x: 185.5457763671875, y: 16.0, radius: 6.0),
1070 1071
      );
      // Wait for animations to finish.
1072
      await tester.pumpAndSettle();
1073 1074 1075 1076 1077 1078 1079 1080 1081
      expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
      expect(
        sliderBox,
        paints
          ..circle(x: 400.0, y: 16.0, radius: 16.0)
          ..circle(x: 17.0, y: 16.0, radius: 1.0)
          ..circle(x: 208.5, y: 16.0, radius: 1.0)
          ..circle(x: 591.5, y: 16.0, radius: 1.0)
          ..circle(x: 783.0, y: 16.0, radius: 1.0)
1082
          ..circle(x: 400.0, y: 16.0, radius: 6.0),
1083 1084
      );
      await gesture.up();
1085
      await tester.pumpAndSettle();
1086 1087 1088 1089 1090 1091 1092 1093
      expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
      expect(
        sliderBox,
        paints
          ..circle(x: 17.0, y: 16.0, radius: 1.0)
          ..circle(x: 208.5, y: 16.0, radius: 1.0)
          ..circle(x: 591.5, y: 16.0, radius: 1.0)
          ..circle(x: 783.0, y: 16.0, radius: 1.0)
1094
          ..circle(x: 400.0, y: 16.0, radius: 6.0),
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
      );
    }

    await tester.pumpWidget(buildSlider(1));
    // Do it once without reparenting in the middle of an animation
    await testReparenting(false);
    // Now do it again with reparenting in the middle of an animation.
    await testReparenting(true);
  });

1105
  testWidgets('Slider Semantics', (WidgetTester tester) async {
1106
    final SemanticsTester semantics = SemanticsTester(tester);
1107

1108
    await tester.pumpWidget(Directionality(
1109
      textDirection: TextDirection.ltr,
1110 1111 1112 1113
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
        child: Material(
          child: Slider(
1114 1115 1116
            value: 0.5,
            onChanged: (double v) {},
          ),
1117 1118
        ),
      ),
1119
    ));
1120

1121 1122 1123
    expect(
        semantics,
        hasSemantics(
1124 1125
          TestSemantics.root(children: <TestSemantics>[
            TestSemantics.rootChild(
1126
              id: 1,
1127 1128 1129 1130
              value: '50%',
              increasedValue: '55%',
              decreasedValue: '45%',
              textDirection: TextDirection.ltr,
1131 1132
              actions: SemanticsAction.decrease.index | SemanticsAction.increase.index,
            ),
1133 1134 1135 1136
          ]),
          ignoreRect: true,
          ignoreTransform: true,
        ));
1137 1138

    // Disable slider
1139
    await tester.pumpWidget(Directionality(
1140
      textDirection: TextDirection.ltr,
1141 1142
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
1143
        child: const Material(
1144
          child: Slider(
1145 1146 1147
            value: 0.5,
            onChanged: null,
          ),
1148 1149
        ),
      ),
1150
    ));
1151

1152 1153 1154
    expect(
        semantics,
        hasSemantics(
1155
          TestSemantics.root(),
1156 1157 1158
          ignoreRect: true,
          ignoreTransform: true,
        ));
1159 1160 1161

    semantics.dispose();
  });
1162

1163
  testWidgets('Slider Semantics - iOS', (WidgetTester tester) async {
1164
    final SemanticsTester semantics = SemanticsTester(tester);
1165 1166

    await tester.pumpWidget(
1167
      Theme(
1168 1169 1170
        data: ThemeData.light().copyWith(
          platform: TargetPlatform.iOS,
        ),
1171
        child: Directionality(
1172
          textDirection: TextDirection.ltr,
1173 1174 1175 1176
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: Material(
              child: Slider(
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
                value: 100.0,
                min: 0.0,
                max: 200.0,
                onChanged: (double v) {},
              ),
            ),
          ),
        ),
      ),
    );

    expect(
      semantics,
      hasSemantics(
1191 1192
        TestSemantics.root(children: <TestSemantics>[
          TestSemantics.rootChild(
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
            id: 2,
            value: '50%',
            increasedValue: '60%',
            decreasedValue: '40%',
            textDirection: TextDirection.ltr,
            actions: SemanticsAction.decrease.index | SemanticsAction.increase.index,
          ),
        ]),
        ignoreRect: true,
        ignoreTransform: true,
      ));
    semantics.dispose();
  });

  testWidgets('Slider semantics with custom formatter', (WidgetTester tester) async {
1208
    final SemanticsTester semantics = SemanticsTester(tester);
1209

1210
    await tester.pumpWidget(Directionality(
1211
      textDirection: TextDirection.ltr,
1212 1213 1214 1215
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
        child: Material(
          child: Slider(
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
            value: 40.0,
            min: 0.0,
            max: 200.0,
            divisions: 10,
            semanticFormatterCallback: (double value) => value.round().toString(),
            onChanged: (double v) {},
          ),
        ),
      ),
    ));

    expect(
        semantics,
        hasSemantics(
1230 1231
          TestSemantics.root(children: <TestSemantics>[
            TestSemantics.rootChild(
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
              id: 3,
              value: '40',
              increasedValue: '60',
              decreasedValue: '20',
              textDirection: TextDirection.ltr,
              actions: SemanticsAction.decrease.index | SemanticsAction.increase.index,
            ),
          ]),
          ignoreRect: true,
          ignoreTransform: true,
        ));
    semantics.dispose();
  });

1246
  testWidgets('Value indicator appears when it should', (WidgetTester tester) async {
1247
    final ThemeData baseTheme = ThemeData(
1248 1249 1250 1251 1252
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
    SliderThemeData theme = baseTheme.sliderTheme;
    double value = 0.45;
1253
    Widget buildApp({SliderThemeData sliderTheme, int divisions, bool enabled = true}) {
1254
      final ValueChanged<double> onChanged = enabled ? (double d) => value = d : null;
1255
      return Directionality(
1256
        textDirection: TextDirection.ltr,
1257 1258 1259 1260 1261
        child: MediaQuery(
          data: MediaQueryData.fromWindow(window),
          child: Material(
            child: Center(
              child: Theme(
1262
                data: baseTheme,
1263
                child: SliderTheme(
1264
                  data: sliderTheme,
1265
                  child: Slider(
1266 1267 1268 1269 1270
                    value: value,
                    label: '$value',
                    divisions: divisions,
                    onChanged: onChanged,
                  ),
1271 1272 1273 1274 1275 1276 1277 1278
                ),
              ),
            ),
          ),
        ),
      );
    }

1279
    Future<void> expectValueIndicator({
1280 1281 1282
      bool isVisible,
      SliderThemeData theme,
      int divisions,
1283
      bool enabled = true,
1284 1285
    }) async {
      // Discrete enabled widget.
1286 1287 1288
      await tester.pumpWidget(buildApp(sliderTheme: theme, divisions: divisions, enabled: enabled));
      final Offset center = tester.getCenter(find.byType(Slider));
      final TestGesture gesture = await tester.startGesture(center);
1289
      // Wait for value indicator animation to finish.
1290
      await tester.pumpAndSettle();
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

      final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(Slider));
      expect(
        sliderBox,
        isVisible
            ? (paints..path(color: theme.valueIndicatorColor))
            : isNot(paints..path(color: theme.valueIndicatorColor)),
      );
      await gesture.up();
    }

    // Default (showValueIndicator set to onlyForDiscrete).
    await expectValueIndicator(isVisible: true, theme: theme, divisions: 3, enabled: true);
    await expectValueIndicator(isVisible: false, theme: theme, divisions: 3, enabled: false);
    await expectValueIndicator(isVisible: false, theme: theme, enabled: true);
    await expectValueIndicator(isVisible: false, theme: theme, enabled: false);

    // With showValueIndicator set to onlyForContinuous.
    theme = theme.copyWith(showValueIndicator: ShowValueIndicator.onlyForContinuous);
    await expectValueIndicator(isVisible: false, theme: theme, divisions: 3, enabled: true);
    await expectValueIndicator(isVisible: false, theme: theme, divisions: 3, enabled: false);
    await expectValueIndicator(isVisible: true, theme: theme, enabled: true);
    await expectValueIndicator(isVisible: false, theme: theme, enabled: false);

    // discrete enabled widget with showValueIndicator set to always.
    theme = theme.copyWith(showValueIndicator: ShowValueIndicator.always);
    await expectValueIndicator(isVisible: true, theme: theme, divisions: 3, enabled: true);
    await expectValueIndicator(isVisible: false, theme: theme, divisions: 3, enabled: false);
    await expectValueIndicator(isVisible: true, theme: theme, enabled: true);
    await expectValueIndicator(isVisible: false, theme: theme, enabled: false);

    // discrete enabled widget with showValueIndicator set to never.
    theme = theme.copyWith(showValueIndicator: ShowValueIndicator.never);
    await expectValueIndicator(isVisible: false, theme: theme, divisions: 3, enabled: true);
    await expectValueIndicator(isVisible: false, theme: theme, divisions: 3, enabled: false);
    await expectValueIndicator(isVisible: false, theme: theme, enabled: true);
    await expectValueIndicator(isVisible: false, theme: theme, enabled: false);
  });
1329 1330

  testWidgets("Slider doesn't start any animations after dispose", (WidgetTester tester) async {
1331
    final Key sliderKey = UniqueKey();
1332 1333
    double value = 0.0;
    await tester.pumpWidget(
1334
      Directionality(
1335
        textDirection: TextDirection.ltr,
1336
        child: StatefulBuilder(
1337
          builder: (BuildContext context, StateSetter setState) {
1338 1339 1340 1341 1342
            return MediaQuery(
              data: MediaQueryData.fromWindow(window),
              child: Material(
                child: Center(
                  child: Slider(
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
                    key: sliderKey,
                    value: value,
                    divisions: 4,
                    onChanged: (double newValue) {
                      setState(() {
                        value = newValue;
                      });
                    },
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );

    final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byKey(sliderKey)));
    await tester.pumpAndSettle(const Duration(milliseconds: 100));
    expect(value, equals(0.5));
    await gesture.moveBy(const Offset(-500.0, 0.0));
    await tester.pumpAndSettle(const Duration(milliseconds: 100));
    // Change the tree to dispose the original widget.
1366
    await tester.pumpWidget(Container());
1367 1368 1369
    expect(await tester.pumpAndSettle(const Duration(milliseconds: 100)), equals(1));
    await gesture.up();
  });
1370
}