slider_test.dart 83.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// 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/cupertino.dart';
8
import 'package:flutter/foundation.dart';
9
import 'package:flutter/material.dart';
10
import 'package:flutter/rendering.dart';
11
import 'package:flutter/scheduler.dart';
12
import 'package:flutter/services.dart';
13
import 'package:flutter/src/physics/utils.dart' show nearEqual;
14 15
import 'package:flutter_test/flutter_test.dart';

16
import '../rendering/mock_canvas.dart';
17
import '../widgets/semantics_tester.dart';
18

19 20 21 22 23 24 25 26 27 28 29 30 31
// 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(
32 33
    PaintingContext context,
    Offset thumbCenter, {
34 35 36 37 38 39 40 41 42 43
    required Animation<double> activationAnimation,
    required Animation<double> enableAnimation,
    required bool isDiscrete,
    required TextPainter labelPainter,
    required RenderBox parentBox,
    required SliderThemeData sliderTheme,
    required TextDirection textDirection,
    required double value,
    required double textScaleFactor,
    required Size sizeWithOverflow,
44
  }) {
45
    log.add(thumbCenter);
46
    final Paint thumbPaint = Paint()..color = Colors.red;
47 48 49 50
    context.canvas.drawCircle(thumbCenter, 5.0, thumbPaint);
  }
}

51 52
class TallSliderTickMarkShape extends SliderTickMarkShape {
  @override
53
  Size getPreferredSize({required SliderThemeData sliderTheme, required bool isEnabled}) {
54 55 56 57 58
    return const Size(10.0, 200.0);
  }

  @override
  void paint(
59 60
    PaintingContext context,
    Offset offset, {
61 62 63 64 65 66
    required Offset thumbCenter,
    required RenderBox parentBox,
    required SliderThemeData sliderTheme,
    required Animation<double> enableAnimation,
    required bool isEnabled,
    required TextDirection textDirection,
67
  }) {
68 69 70 71 72
    final Paint paint = Paint()..color = Colors.red;
    context.canvas.drawRect(Rect.fromLTWH(offset.dx, offset.dy, 10.0, 20.0), paint);
  }
}

73
void main() {
74
  testWidgets('Slider can move when tapped (LTR)', (WidgetTester tester) async {
75
    final Key sliderKey = UniqueKey();
76
    double value = 0.0;
77 78
    double? startValue;
    double? endValue;
79

80
    await tester.pumpWidget(
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: Slider(
                      key: sliderKey,
                      value: value,
                      onChanged: (double newValue) {
                        setState(() {
                          value = newValue;
                        });
                      },
                      onChangeStart: (double value) {
                        startValue = value;
                      },
                      onChangeEnd: (double value) {
                        endValue = value;
                      },
                    ),
105
                  ),
106
                ),
107 108 109
              );
            },
          ),
110
        ),
111
      ),
112
    );
113

114
    expect(value, equals(0.0));
115
    await tester.tap(find.byKey(sliderKey));
116
    expect(value, equals(0.5));
117 118 119 120
    expect(startValue, equals(0.0));
    expect(endValue, equals(0.5));
    startValue = null;
    endValue = null;
121
    await tester.pump(); // No animation should start.
122
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
123 124 125 126 127 128

    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);
129
    expect(value, moreOrLessEquals(0.25, epsilon: 0.05));
130
    expect(startValue, equals(0.5));
131
    expect(endValue, moreOrLessEquals(0.25, epsilon: 0.05));
132
    await tester.pump(); // No animation should start.
133
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
134 135
  });

136
  testWidgets('Slider can move when tapped (RTL)', (WidgetTester tester) async {
137
    final Key sliderKey = UniqueKey();
138
    double value = 0.0;
139

140
    await tester.pumpWidget(
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.rtl,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: Slider(
                      key: sliderKey,
                      value: value,
                      onChanged: (double newValue) {
                        setState(() {
                          value = newValue;
                        });
                      },
                    ),
159
                  ),
160
                ),
161 162 163
              );
            },
          ),
164 165 166 167 168 169 170 171
        ),
      ),
    );

    expect(value, equals(0.0));
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.5));
    await tester.pump(); // No animation should start.
172
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
173 174 175 176 177 178

    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);
179
    expect(value, moreOrLessEquals(0.75, epsilon: 0.05));
180
    await tester.pump(); // No animation should start.
181
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
182 183
  });

184
  testWidgets("Slider doesn't send duplicate change events if tapped on the same value", (WidgetTester tester) async {
185
    final Key sliderKey = UniqueKey();
186
    double value = 0.0;
187 188
    late double startValue;
    late double endValue;
189
    int updates = 0;
190 191
    int startValueUpdates = 0;
    int endValueUpdates = 0;
192

193

194
    await tester.pumpWidget(
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: Slider(
                      key: sliderKey,
                      value: value,
                      onChanged: (double newValue) {
                        setState(() {
                          updates++;
                          value = newValue;
                        });
                      },
                      onChangeStart: (double value) {
                        startValueUpdates++;
                        startValue = value;
                      },
                      onChangeEnd: (double value) {
                        endValueUpdates++;
                        endValue = value;
                      },
                    ),
222 223
                  ),
                ),
224 225 226
              );
            },
          ),
227
        ),
228
      ),
229
    );
230

231 232 233
    expect(value, equals(0.0));
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.5));
234 235
    expect(startValue, equals(0.0));
    expect(endValue, equals(0.5));
236 237 238 239 240
    await tester.pump();
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.5));
    await tester.pump();
    expect(updates, equals(1));
241 242
    expect(startValueUpdates, equals(2));
    expect(endValueUpdates, equals(2));
243 244
  });

245
  testWidgets('Value indicator shows for a bit after being tapped', (WidgetTester tester) async {
246
    final Key sliderKey = UniqueKey();
247 248 249
    double value = 0.0;

    await tester.pumpWidget(
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: Slider(
                      key: sliderKey,
                      value: value,
                      divisions: 4,
                      onChanged: (double newValue) {
                        setState(() {
                          value = newValue;
                        });
                      },
                    ),
269 270
                  ),
                ),
271 272 273
              );
            },
          ),
274 275 276 277 278 279 280 281 282
        ),
      ),
    );

    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
283
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(2));
284 285
    await tester.pump(const Duration(milliseconds: 100));
    // Value indicator is longer than position.
286
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(1));
287
    await tester.pump(const Duration(milliseconds: 100));
288
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
289
    await tester.pump(const Duration(milliseconds: 100));
290
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
291 292
    await tester.pump(const Duration(milliseconds: 100));
    // Shown for long enough, value indicator is animated closed.
293
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(1));
294
    await tester.pump(const Duration(milliseconds: 101));
295
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
296 297 298
  });

  testWidgets('Discrete Slider repaints and animates when dragged', (WidgetTester tester) async {
299
    final Key sliderKey = UniqueKey();
300 301
    double value = 0.0;
    final List<Offset> log = <Offset>[];
302
    final LoggingThumbShape loggingThumb = LoggingThumbShape(log);
303
    await tester.pumpWidget(
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              final SliderThemeData sliderTheme = SliderTheme.of(context).copyWith(thumbShape: loggingThumb);
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: SliderTheme(
                      data: sliderTheme,
                      child: Slider(
                        key: sliderKey,
                        value: value,
                        divisions: 4,
                        onChanged: (double newValue) {
                          setState(() {
                            value = newValue;
                          });
                        },
                      ),
326 327 328
                    ),
                  ),
                ),
329 330 331
              );
            },
          ),
332 333 334 335 336
        ),
      ),
    );

    final List<Offset> expectedLog = <Offset>[
337 338
      const Offset(24.0, 300.0),
      const Offset(24.0, 300.0),
339 340 341 342 343 344 345 346 347 348 349 350 351
      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);
352
    expect(log.last.dx, moreOrLessEquals(386.6, epsilon: 0.1));
353 354 355 356 357 358
    // 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);
359
    expect(log.last.dx, moreOrLessEquals(344.5, epsilon: 0.1));
360 361
    // Final position.
    await tester.pump(const Duration(milliseconds: 80));
362
    expectedLog.add(const Offset(24.0, 300.0));
363 364
    expect(value, equals(0.0));
    expect(log.length, 8);
365
    expect(log.last.dx, moreOrLessEquals(24.0, epsilon: 0.1));
366 367 368 369
    await gesture.up();
  });

  testWidgets("Slider doesn't send duplicate change events if tapped on the same value", (WidgetTester tester) async {
370
    final Key sliderKey = UniqueKey();
371 372 373 374
    double value = 0.0;
    int updates = 0;

    await tester.pumpWidget(
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: Slider(
                      key: sliderKey,
                      value: value,
                      onChanged: (double newValue) {
                        setState(() {
                          updates++;
                          value = newValue;
                        });
                      },
                    ),
394 395
                  ),
                ),
396 397 398
              );
            },
          ),
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
        ),
      ),
    );

    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 {
414
    final Key sliderKey = UniqueKey();
415 416
    double value = 0.0;
    final List<Offset> log = <Offset>[];
417
    final LoggingThumbShape loggingThumb = LoggingThumbShape(log);
418
    await tester.pumpWidget(
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              final SliderThemeData sliderTheme = SliderTheme.of(context).copyWith(thumbShape: loggingThumb);
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: SliderTheme(
                      data: sliderTheme,
                      child: Slider(
                        key: sliderKey,
                        value: value,
                        divisions: 4,
                        onChanged: (double newValue) {
                          setState(() {
                            value = newValue;
                          });
                        },
                      ),
441 442 443
                    ),
                  ),
                ),
444 445 446
              );
            },
          ),
447 448 449 450 451
        ),
      ),
    );

    final List<Offset> expectedLog = <Offset>[
452 453
      const Offset(24.0, 300.0),
      const Offset(24.0, 300.0),
454 455 456 457 458 459 460 461 462 463 464 465 466
      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);
467
    expect(log.last.dx, moreOrLessEquals(386.6, epsilon: 0.1));
468 469 470 471 472 473
    // 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);
474
    expect(log.last.dx, moreOrLessEquals(344.5, epsilon: 0.1));
475 476
    // Final position.
    await tester.pump(const Duration(milliseconds: 80));
477
    expectedLog.add(const Offset(24.0, 300.0));
478 479
    expect(value, equals(0.0));
    expect(log.length, 8);
480
    expect(log.last.dx, moreOrLessEquals(24.0, epsilon: 0.1));
481 482 483 484
    await gesture.up();
  });

  testWidgets('Slider take on discrete values', (WidgetTester tester) async {
485
    final Key sliderKey = UniqueKey();
486 487 488
    double value = 0.0;

    await tester.pumpWidget(
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: SizedBox(
                      width: 144.0 + 2 * 16.0, // _kPreferredTotalWidth
                      child: Slider(
                        key: sliderKey,
                        min: 0.0,
                        max: 100.0,
                        divisions: 10,
                        value: value,
                        onChanged: (double newValue) {
                          setState(() {
                            value = newValue;
                          });
                        },
                      ),
512 513 514
                    ),
                  ),
                ),
515 516 517
              );
            },
          ),
518 519 520 521
        ),
      ),
    );

522
    expect(value, equals(0.0));
523
    await tester.tap(find.byKey(sliderKey));
524
    expect(value, equals(50.0));
525
    await tester.drag(find.byKey(sliderKey), const Offset(5.0, 0.0));
526
    expect(value, equals(50.0));
527
    await tester.drag(find.byKey(sliderKey), const Offset(40.0, 0.0));
528
    expect(value, equals(80.0));
529

530
    await tester.pump(); // Starts animation.
531
    expect(SchedulerBinding.instance!.transientCallbackCount, greaterThan(0));
532 533 534 535
    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));
536
    // Animation complete.
537
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
538
  });
539

540
  testWidgets('Slider can be given zero values', (WidgetTester tester) async {
541
    final List<double> log = <double>[];
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: Material(
              child: Slider(
                value: 0.0,
                min: 0.0,
                max: 1.0,
                onChanged: (double newValue) {
                  log.add(newValue);
                },
              ),
            ),
558
          ),
559
        ),
560
      ),
561
    );
562 563 564 565 566

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

567 568 569 570 571
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
572 573 574 575 576 577 578 579 580
            data: MediaQueryData.fromWindow(window),
            child: Material(
              child: Slider(
                value: 0.0,
                min: 0.0,
                max: 0.0,
                onChanged: (double newValue) {
                  log.add(newValue);
                },
581
              ),
582
            ),
583 584 585 586 587 588 589
          ),
        ),
      ),
    );

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

593
  testWidgets('Slider uses the right theme colors for the right components', (WidgetTester tester) async {
594 595
    const Color customColor1 = Color(0xcafefeed);
    const Color customColor2 = Color(0xdeadbeef);
596
    final ThemeData theme = ThemeData(
597 598
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
599 600 601 602 603 604 605 606 607 608 609 610 611
      sliderTheme: const SliderThemeData(
        disabledThumbColor: Color(0xff000001),
        disabledActiveTickMarkColor: Color(0xff000002),
        disabledActiveTrackColor: Color(0xff000003),
        disabledInactiveTickMarkColor: Color(0xff000004),
        disabledInactiveTrackColor: Color(0xff000005),
        activeTrackColor: Color(0xff000006),
        activeTickMarkColor: Color(0xff000007),
        inactiveTrackColor: Color(0xff000008),
        inactiveTickMarkColor: Color(0xff000009),
        overlayColor: Color(0xff000010),
        thumbColor: Color(0xff000011),
        valueIndicatorColor: Color(0xff000012),
612
      ),
613 614 615 616
    );
    final SliderThemeData sliderTheme = theme.sliderTheme;
    double value = 0.45;
    Widget buildApp({
617 618 619
      Color? activeColor,
      Color? inactiveColor,
      int? divisions,
620
      bool enabled = true,
621
    }) {
622
      final ValueChanged<double>? onChanged = !enabled
623 624 625 626
        ? null
        : (double d) {
            value = d;
          };
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
      return MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: Material(
              child: Center(
                child: Theme(
                  data: theme,
                  child: Slider(
                    value: value,
                    label: '$value',
                    divisions: divisions,
                    activeColor: activeColor,
                    inactiveColor: inactiveColor,
                    onChanged: onChanged,
                  ),
Jose Alba's avatar
Jose Alba committed
644 645 646 647 648 649 650 651 652 653
                ),
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp());

654
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)))!;
655
    final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay));
656 657

    // Check default theme for enabled widget.
658 659 660 661 662 663 664 665
    expect(material, paints..rrect(color: sliderTheme.activeTrackColor)..rrect(color: sliderTheme.inactiveTrackColor));
    expect(material, paints..shadow(color: const Color(0xff000000)));
    expect(material, paints..circle(color: sliderTheme.thumbColor));
    expect(material, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledActiveTrackColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledInactiveTrackColor)));
    expect(material, isNot(paints..circle(color: sliderTheme.activeTickMarkColor)));
    expect(material, isNot(paints..circle(color: sliderTheme.inactiveTickMarkColor)));
666 667 668

    // Test setting only the activeColor.
    await tester.pumpWidget(buildApp(activeColor: customColor1));
669 670 671 672 673 674 675
    expect(material, paints..rrect(color: customColor1)..rrect(color: sliderTheme.inactiveTrackColor));
    expect(material, paints..shadow(color: Colors.black));
    expect(material, paints..circle(color: customColor1));
    expect(material, isNot(paints..circle(color: sliderTheme.thumbColor)));
    expect(material, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledActiveTrackColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledInactiveTrackColor)));
676 677 678

    // Test setting only the inactiveColor.
    await tester.pumpWidget(buildApp(inactiveColor: customColor1));
679 680 681 682 683 684
    expect(material, paints..rrect(color: sliderTheme.activeTrackColor)..rrect(color: customColor1));
    expect(material, paints..shadow(color: Colors.black));
    expect(material, paints..circle(color: sliderTheme.thumbColor));
    expect(material, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledActiveTrackColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledInactiveTrackColor)));
685 686 687

    // Test setting both activeColor and inactiveColor.
    await tester.pumpWidget(buildApp(activeColor: customColor1, inactiveColor: customColor2));
688 689 690 691 692 693 694
    expect(material, paints..rrect(color: customColor1)..rrect(color: customColor2));
    expect(material, paints..shadow(color: Colors.black));
    expect(material, paints..circle(color: customColor1));
    expect(material, isNot(paints..circle(color: sliderTheme.thumbColor)));
    expect(material, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledActiveTrackColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledInactiveTrackColor)));
695 696 697

    // Test colors for discrete slider.
    await tester.pumpWidget(buildApp(divisions: 3));
698
    expect(material, paints..rrect(color: sliderTheme.activeTrackColor)..rrect(color: sliderTheme.inactiveTrackColor));
699
    expect(
700
        material,
701 702 703 704 705 706 707 708
        paints
          ..circle(color: sliderTheme.activeTickMarkColor)
          ..circle(color: sliderTheme.activeTickMarkColor)
          ..circle(color: sliderTheme.inactiveTickMarkColor)
          ..circle(color: sliderTheme.inactiveTickMarkColor)
          ..shadow(color: Colors.black)
          ..circle(color: sliderTheme.thumbColor)
    );
709 710 711
    expect(material, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledActiveTrackColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledInactiveTrackColor)));
712 713 714 715 716 717 718

    // Test colors for discrete slider with inactiveColor and activeColor set.
    await tester.pumpWidget(buildApp(
      activeColor: customColor1,
      inactiveColor: customColor2,
      divisions: 3,
    ));
719
    expect(material, paints..rrect(color: customColor1)..rrect(color: customColor2));
720
    expect(
721
        material,
722 723 724 725 726 727 728
        paints
          ..circle(color: customColor2)
          ..circle(color: customColor2)
          ..circle(color: customColor1)
          ..circle(color: customColor1)
          ..shadow(color: Colors.black)
          ..circle(color: customColor1));
729 730 731 732 733 734
    expect(material, isNot(paints..circle(color: sliderTheme.thumbColor)));
    expect(material, isNot(paints..circle(color: sliderTheme.disabledThumbColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledActiveTrackColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.disabledInactiveTrackColor)));
    expect(material, isNot(paints..circle(color: sliderTheme.activeTickMarkColor)));
    expect(material, isNot(paints..circle(color: sliderTheme.inactiveTickMarkColor)));
735 736 737 738 739

    // Test default theme for disabled widget.
    await tester.pumpWidget(buildApp(enabled: false));
    await tester.pumpAndSettle();
    expect(
740
        material,
741 742 743
        paints
          ..rrect(color: sliderTheme.disabledActiveTrackColor)
          ..rrect(color: sliderTheme.disabledInactiveTrackColor));
744 745 746 747
    expect(material, paints..shadow(color: Colors.black)..circle(color: sliderTheme.disabledThumbColor));
    expect(material, isNot(paints..circle(color: sliderTheme.thumbColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.activeTrackColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.inactiveTrackColor)));
748 749 750 751

    // Test setting the activeColor and inactiveColor for disabled widget.
    await tester.pumpWidget(buildApp(activeColor: customColor1, inactiveColor: customColor2, enabled: false));
    expect(
752
        material,
753 754 755
        paints
          ..rrect(color: sliderTheme.disabledActiveTrackColor)
          ..rrect(color: sliderTheme.disabledInactiveTrackColor));
756 757 758 759
    expect(material, paints..circle(color: sliderTheme.disabledThumbColor));
    expect(material, isNot(paints..circle(color: sliderTheme.thumbColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.activeTrackColor)));
    expect(material, isNot(paints..rrect(color: sliderTheme.inactiveTrackColor)));
760 761 762 763 764 765 766 767 768 769 770

    // 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);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(value, equals(2.0 / 3.0));
    expect(
      valueIndicatorBox,
      paints
771 772
        ..path(color: sliderTheme.valueIndicatorColor)
        ..paragraph(),
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    );
    await gesture.up();
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();

    // 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);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(value, equals(2.0 / 3.0));
    expect(
      valueIndicatorBox,
      paints
        ..rrect(color: customColor1) // active track
        ..rrect(color: customColor2) // inactive track
        ..circle(color: customColor1.withOpacity(0.12)) // overlay
        ..circle(color: customColor2) // 1st tick mark
        ..circle(color: customColor2) // 2nd tick mark
        ..circle(color: customColor2) // 3rd tick mark
        ..circle(color: customColor1) // 4th tick mark
        ..shadow(color: Colors.black)
        ..circle(color: customColor1) // thumb
        ..path(color: sliderTheme.valueIndicatorColor), // indicator
    );
    await gesture.up();
  });

806
  testWidgets('Slider can tap in vertical scroller', (WidgetTester tester) async {
807
    double value = 0.0;
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: Material(
              child: ListView(
                children: <Widget>[
                  Slider(
                    value: value,
                    onChanged: (double newValue) {
                      value = newValue;
                    },
                  ),
                  Container(
                    height: 2000.0,
                  ),
                ],
827
              ),
828
            ),
829
          ),
830 831
        ),
      ),
832
    );
833 834 835 836 837 838 839

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

  testWidgets('Slider drags immediately (LTR)', (WidgetTester tester) async {
    double value = 0.0;
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: Material(
              child: Center(
                child: Slider(
                  value: value,
                  onChanged: (double newValue) {
                    value = newValue;
                  },
                ),
              ),
855
            ),
856
          ),
857
        ),
858
      ),
859
    );
860

861 862 863
    final Offset center = tester.getCenter(find.byType(Slider));
    final TestGesture gesture = await tester.startGesture(center);

864
    expect(value, equals(0.5));
865 866 867 868 869 870

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

    expect(value, greaterThan(0.5));

    await gesture.up();
871 872
  });

873
  testWidgets('Slider drags immediately (RTL)', (WidgetTester tester) async {
874
    double value = 0.0;
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.rtl,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: Material(
              child: Center(
                child: Slider(
                  value: value,
                  onChanged: (double newValue) {
                    value = newValue;
                  },
                ),
              ),
890
            ),
891
          ),
892 893
        ),
      ),
894
    );
895

896
    final Offset center = tester.getCenter(find.byType(Slider));
897
    final TestGesture gesture = await tester.startGesture(center);
898 899 900

    expect(value, equals(0.5));

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

903
    expect(value, lessThan(0.5));
904 905 906

    await gesture.up();
  });
907 908

  testWidgets('Slider sizing', (WidgetTester tester) async {
909 910 911 912 913 914 915
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: const Material(
916 917 918 919
              child: Center(
                child: Slider(
                  value: 0.5,
                  onChanged: null,
920
                ),
921
              ),
922
            ),
923
          ),
924 925
        ),
      ),
926
    );
927 928
    expect(tester.renderObject<RenderBox>(find.byType(Slider)).size, const Size(800.0, 600.0));

929 930 931 932 933 934 935 936 937 938 939 940 941 942
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: const Material(
              child: Center(
                child: IntrinsicWidth(
                  child: Slider(
                    value: 0.5,
                    onChanged: null,
                  ),
                ),
943
              ),
944
            ),
945 946 947
          ),
        ),
      ),
948
    );
949
    expect(tester.renderObject<RenderBox>(find.byType(Slider)).size, const Size(144.0 + 2.0 * 24.0, 600.0));
950

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: const Material(
              child: Center(
                child: OverflowBox(
                  maxWidth: double.infinity,
                  maxHeight: double.infinity,
                  child: Slider(
                    value: 0.5,
                    onChanged: null,
                  ),
                ),
967
              ),
968
            ),
969 970 971
          ),
        ),
      ),
972
    );
973
    expect(tester.renderObject<RenderBox>(find.byType(Slider)).size, const Size(144.0 + 2.0 * 24.0, 48.0));
974
  });
975

976
  testWidgets('Slider respects textScaleFactor', (WidgetTester tester) async {
Jose Alba's avatar
Jose Alba committed
977 978 979 980
    final Key sliderKey = UniqueKey();
    double value = 0.0;

    Widget buildSlider({
981
      required double textScaleFactor,
Jose Alba's avatar
Jose Alba committed
982 983 984
      bool isDiscrete = true,
      ShowValueIndicator show = ShowValueIndicator.onlyForDiscrete,
    }) {
985 986 987 988 989 990 991 992 993
      return MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return MediaQuery(
                data: MediaQueryData(textScaleFactor: textScaleFactor),
                child: Material(
                  child: Theme(
994 995
                    data: Theme.of(context).copyWith(
                      sliderTheme: Theme.of(context).sliderTheme.copyWith(showValueIndicator: show),
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
                    ),
                    child: Center(
                      child: OverflowBox(
                        maxWidth: double.infinity,
                        maxHeight: double.infinity,
                        child: Slider(
                          key: sliderKey,
                          min: 0.0,
                          max: 100.0,
                          divisions: isDiscrete ? 10 : null,
                          label: '${value.round()}',
                          value: value,
                          onChanged: (double newValue) {
                            setState(() {
                              value = newValue;
                            });
                          },
                        ),
                      ),
                    ),
1016
                  ),
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
                ),
              );
            },
          ),
        ),
      );
    }

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

    expect(
1031
      tester.renderObject(find.byType(Overlay)),
1032 1033 1034 1035
      paints
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
1036 1037 1038
            Offset(0.0, -8.0),
            Offset(-276.0, -16.0),
            Offset(-216.0, -16.0),
1039 1040
          ],
          color: const Color(0xf55f5f5f),
1041 1042
        )
        ..paragraph(),
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    );

    await gesture.up();
    await tester.pumpAndSettle();

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

    expect(
1054
      tester.renderObject(find.byType(Overlay)),
1055 1056 1057 1058
      paints
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
1059 1060 1061
            Offset(0.0, -8.0),
            Offset(-304.0, -16.0),
            Offset(-216.0, -16.0),
1062 1063
          ],
          color: const Color(0xf55f5f5f),
1064 1065
        )
        ..paragraph(),
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
    );

    await gesture.up();
    await tester.pumpAndSettle();

    // 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);
    await tester.pumpAndSettle();

1081
    expect(tester.renderObject(find.byType(Overlay)),
1082 1083 1084 1085
      paints
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
1086 1087 1088
            Offset(0.0, -8.0),
            Offset(-276.0, -16.0),
            Offset(-216.0, -16.0),
1089 1090
          ],
          color: const Color(0xf55f5f5f),
1091 1092
        )
        ..paragraph(),
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    );

    await gesture.up();
    await tester.pumpAndSettle();

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

    expect(
1108
      tester.renderObject(find.byType(Overlay)),
1109 1110 1111 1112
      paints
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
1113 1114 1115
            Offset(0.0, -8.0),
            Offset(-276.0, -16.0),
            Offset(-216.0, -16.0),
1116 1117
          ],
          color: const Color(0xf55f5f5f),
1118 1119
        )
        ..paragraph(),
1120
    );
1121 1122

    await gesture.up();
1123
    await tester.pumpAndSettle();
1124
  });
1125

1126 1127
  testWidgets('Tick marks are skipped when they are too dense', (WidgetTester tester) async {
    Widget buildSlider({
1128
      required int divisions,
1129
    }) {
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
      return MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: Material(
              child: Center(
                child: Slider(
                  min: 0.0,
                  max: 100.0,
                  divisions: divisions,
                  value: 0.25,
                  onChanged: (double newValue) { },
                ),
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
              ),
            ),
          ),
        ),
      );
    }

    // Pump a slider with a reasonable amount of divisions to verify that the
    // tick marks are drawn when the number of tick marks is not too dense.
    await tester.pumpWidget(
      buildSlider(
        divisions: 4,
      ),
    );

1159
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)))!;
1160 1161

    // 5 tick marks and a thumb.
1162
    expect(material, paintsExactlyCountTimes(#drawCircle, 6));
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173

    // 200 divisions will produce a tick interval off less than 6,
    // which would be too dense to draw.
    await tester.pumpWidget(
      buildSlider(
        divisions: 200,
      ),
    );

    // No tick marks are drawn because they are too dense, but the thumb is
    // still drawn.
1174
    expect(material, paintsExactlyCountTimes(#drawCircle, 1));
1175 1176
  });

1177
  testWidgets('Slider has correct animations when reparented', (WidgetTester tester) async {
Jose Alba's avatar
Jose Alba committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    final Key sliderKey = GlobalKey(debugLabel: 'A');
    double value = 0.0;

    Widget buildSlider(int parents) {
      Widget createParents(int parents, StateSetter setState) {
        Widget slider = Slider(
          key: sliderKey,
          value: value,
          divisions: 4,
          onChanged: (double newValue) {
            setState(() {
              value = newValue;
            });
1191
          },
Jose Alba's avatar
Jose Alba committed
1192 1193 1194 1195 1196 1197 1198 1199
        );

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

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
      return MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: createParents(parents, setState),
                ),
              );
            },
          ),
        ),
      );
    }

    Future<void> testReparenting(bool reparent) async {
1218
      final MaterialInkController material = Material.of(tester.element(find.byType(Slider)))!;
1219 1220 1221 1222 1223 1224
      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();
      await tester.pumpAndSettle();
1225
      expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
1226
      expect(
1227
        material,
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
        paints
          ..circle(x: 26.0, y: 24.0, radius: 1.0)
          ..circle(x: 213.0, y: 24.0, radius: 1.0)
          ..circle(x: 400.0, y: 24.0, radius: 1.0)
          ..circle(x: 587.0, y: 24.0, radius: 1.0)
          ..circle(x: 774.0, y: 24.0, radius: 1.0)
          ..circle(x: 24.0, y: 24.0, radius: 10.0),
      );

      gesture = await tester.startGesture(center);
      await tester.pump();
      // Wait for animations to start.
      await tester.pump(const Duration(milliseconds: 25));
1241
      expect(SchedulerBinding.instance!.transientCallbackCount, equals(2));
1242
      expect(
1243
        material,
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
        paints
          ..circle(x: 111.20703125, y: 24.0, radius: 5.687664985656738)
          ..circle(x: 26.0, y: 24.0, radius: 1.0)
          ..circle(x: 213.0, y: 24.0, radius: 1.0)
          ..circle(x: 400.0, y: 24.0, radius: 1.0)
          ..circle(x: 587.0, y: 24.0, radius: 1.0)
          ..circle(x: 774.0, y: 24.0, radius: 1.0)
          ..circle(x: 111.20703125, y: 24.0, radius: 10.0),
      );

      // 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));
1261
      expect(SchedulerBinding.instance!.transientCallbackCount, equals(2));
1262
      expect(
1263
        material,
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
        paints
          ..circle(x: 190.0135726928711, y: 24.0, radius: 12.0)
          ..circle(x: 26.0, y: 24.0, radius: 1.0)
          ..circle(x: 213.0, y: 24.0, radius: 1.0)
          ..circle(x: 400.0, y: 24.0, radius: 1.0)
          ..circle(x: 587.0, y: 24.0, radius: 1.0)
          ..circle(x: 774.0, y: 24.0, radius: 1.0)
          ..circle(x: 190.0135726928711, y: 24.0, radius: 10.0),
      );
      // Wait for animations to finish.
      await tester.pumpAndSettle();
1275
      expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
1276
      expect(
1277
        material,
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
        paints
          ..circle(x: 400.0, y: 24.0, radius: 24.0)
          ..circle(x: 26.0, y: 24.0, radius: 1.0)
          ..circle(x: 213.0, y: 24.0, radius: 1.0)
          ..circle(x: 400.0, y: 24.0, radius: 1.0)
          ..circle(x: 587.0, y: 24.0, radius: 1.0)
          ..circle(x: 774.0, y: 24.0, radius: 1.0)
          ..circle(x: 400.0, y: 24.0, radius: 10.0),
      );
      await gesture.up();
      await tester.pumpAndSettle();
1289
      expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
1290
      expect(
1291
        material,
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
        paints
          ..circle(x: 26.0, y: 24.0, radius: 1.0)
          ..circle(x: 213.0, y: 24.0, radius: 1.0)
          ..circle(x: 400.0, y: 24.0, radius: 1.0)
          ..circle(x: 587.0, y: 24.0, radius: 1.0)
          ..circle(x: 774.0, y: 24.0, radius: 1.0)
          ..circle(x: 400.0, y: 24.0, radius: 10.0),
      );
    }

    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);
  });


1310
  testWidgets('Slider Semantics', (WidgetTester tester) async {
1311
    final SemanticsTester semantics = SemanticsTester(tester);
1312

1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    await tester.pumpWidget(MaterialApp(
      home: Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: MediaQueryData.fromWindow(window),
          child: Material(
            child: Slider(
              value: 0.5,
              onChanged: (double v) { },
            ),
1323
          ),
1324 1325
        ),
      ),
1326
    ));
1327

1328 1329
    await tester.pumpAndSettle();

1330
    expect(
1331 1332 1333 1334 1335
      semantics,
      hasSemantics(
        TestSemantics.root(
          children: <TestSemantics>[
            TestSemantics(
1336
              id: 1,
1337
              textDirection: TextDirection.ltr,
1338 1339 1340 1341 1342 1343
              children: <TestSemantics>[
                TestSemantics(
                  id: 2,
                  children: <TestSemantics>[
                    TestSemantics(
                      id: 3,
1344 1345 1346 1347
                      flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                      children: <TestSemantics>[
                        TestSemantics(
                          id: 4,
1348
                          flags: <SemanticsFlag>[SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable, SemanticsFlag.isSlider],
1349 1350 1351 1352 1353 1354 1355
                          actions: <SemanticsAction>[SemanticsAction.increase, SemanticsAction.decrease],
                          value: '50%',
                          increasedValue: '55%',
                          decreasedValue: '45%',
                          textDirection: TextDirection.ltr,
                        ),
                      ],
1356 1357 1358 1359
                    ),
                  ],
                ),
              ],
1360
            ),
1361 1362 1363 1364 1365 1366
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );
1367 1368

    // Disable slider
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
    await tester.pumpWidget(MaterialApp(
      home: Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: MediaQueryData.fromWindow(window),
          child: const Material(
            child: Slider(
              value: 0.5,
              onChanged: null,
            ),
1379
          ),
1380 1381
        ),
      ),
1382
    ));
1383

1384
    expect(
1385 1386 1387 1388 1389
      semantics,
      hasSemantics(
        TestSemantics.root(
          children: <TestSemantics>[
            TestSemantics(
1390 1391 1392 1393 1394
              id: 1,
              textDirection: TextDirection.ltr,
              children: <TestSemantics>[
                TestSemantics(
                  id: 2,
1395 1396
                  children: <TestSemantics>[
                    TestSemantics(
1397
                      id: 3,
1398 1399 1400 1401
                      flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                      children: <TestSemantics>[
                        TestSemantics(
                          id: 4,
1402 1403 1404 1405
                          flags: <SemanticsFlag>[
                            SemanticsFlag.hasEnabledState,
                            // isFocusable is delayed by 1 frame.
                            SemanticsFlag.isFocusable,
1406
                            SemanticsFlag.isSlider,
1407
                          ],
1408 1409 1410 1411 1412 1413
                          value: '50%',
                          increasedValue: '55%',
                          decreasedValue: '45%',
                          textDirection: TextDirection.ltr,
                        ),
                      ],
1414 1415
                    ),
                  ],
1416 1417 1418
                ),
              ],
            ),
1419 1420 1421 1422 1423 1424
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446

    await tester.pump();
    expect(
      semantics,
      hasSemantics(
        TestSemantics.root(
          children: <TestSemantics>[
            TestSemantics(
              id: 1,
              textDirection: TextDirection.ltr,
              children: <TestSemantics>[
                TestSemantics(
                  id: 2,
                  children: <TestSemantics>[
                    TestSemantics(
                      id: 3,
                      flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                      children: <TestSemantics>[
                        TestSemantics(
                          id: 4,
                          flags: <SemanticsFlag>[
                            SemanticsFlag.hasEnabledState,
1447
                            SemanticsFlag.isSlider,
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
                          ],
                          value: '50%',
                          increasedValue: '55%',
                          decreasedValue: '45%',
                          textDirection: TextDirection.ltr,
                        ),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

1467
    semantics.dispose();
1468
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android,  TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows }));
1469

1470 1471
  testWidgets('Slider Semantics', (WidgetTester tester) async {
    final SemanticsTester semantics = SemanticsTester(tester);
1472

1473
    await tester.pumpWidget(
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
        MaterialApp(
          home: Theme(
            data: ThemeData.light(),
            child: Directionality(
              textDirection: TextDirection.ltr,
              child: MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Slider(
                    value: 100.0,
                    min: 0.0,
                    max: 200.0,
                    onChanged: (double v) { },
                  ),
1488
                ),
1489 1490 1491
              ),
            ),
          ),
1492
        )
1493 1494 1495 1496 1497 1498
    );

    expect(
      semantics,
      hasSemantics(
        TestSemantics.root(
1499 1500
          children: <TestSemantics>[
            TestSemantics(
1501 1502
              id: 1,
              textDirection: TextDirection.ltr,
1503 1504
              children: <TestSemantics>[
                TestSemantics(
1505 1506 1507 1508
                  id: 2,
                  children: <TestSemantics>[
                    TestSemantics(
                      id: 3,
1509 1510 1511 1512
                      flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                      children: <TestSemantics>[
                        TestSemantics(
                          id: 4,
1513
                          flags: <SemanticsFlag>[SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable, SemanticsFlag.isSlider],
1514 1515 1516 1517 1518 1519 1520
                          actions: <SemanticsAction>[SemanticsAction.increase, SemanticsAction.decrease],
                          value: '50%',
                          increasedValue: '60%',
                          decreasedValue: '40%',
                          textDirection: TextDirection.ltr,
                        ),
                      ],
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

    // Disable slider
    await tester.pumpWidget(MaterialApp(
      home: Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: MediaQueryData.fromWindow(window),
          child: const Material(
            child: Slider(
              value: 0.5,
              onChanged: null,
            ),
          ),
        ),
      ),
    ));

    expect(
      semantics,
      hasSemantics(
        TestSemantics.root(
          children: <TestSemantics>[
            TestSemantics(
              id: 1,
              textDirection: TextDirection.ltr,
              children: <TestSemantics>[
                TestSemantics(
                  id: 2,
                  children: <TestSemantics>[
                    TestSemantics(
1562 1563 1564 1565 1566
                      id: 3,
                      flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                      children: <TestSemantics>[
                        TestSemantics(
                          id: 5,
1567
                          flags: <SemanticsFlag>[SemanticsFlag.hasEnabledState, SemanticsFlag.isSlider],
1568 1569 1570 1571 1572 1573
                          value: '50%',
                          increasedValue: '60%',
                          decreasedValue: '40%',
                          textDirection: TextDirection.ltr,
                        ),
                      ],
1574 1575
                    ),
                  ],
1576 1577 1578 1579 1580
                ),
              ],
            ),
          ],
        ),
1581 1582 1583 1584 1585 1586
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );
    semantics.dispose();
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
1587 1588

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

1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
    await tester.pumpWidget(MaterialApp(
      home: Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: MediaQueryData.fromWindow(window),
          child: Material(
            child: Slider(
              value: 40.0,
              min: 0.0,
              max: 200.0,
              divisions: 10,
              semanticFormatterCallback: (double value) => value.round().toString(),
              onChanged: (double v) { },
            ),
1605 1606 1607 1608 1609 1610
          ),
        ),
      ),
    ));

    expect(
1611 1612 1613 1614 1615
      semantics,
      hasSemantics(
        TestSemantics.root(
          children: <TestSemantics>[
            TestSemantics(
1616
              id: 1,
1617
              textDirection: TextDirection.ltr,
1618 1619 1620 1621 1622 1623
              children: <TestSemantics>[
                TestSemantics(
                  id: 2,
                  children: <TestSemantics>[
                    TestSemantics(
                      id: 3,
1624 1625 1626 1627
                      flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                      children: <TestSemantics>[
                        TestSemantics(
                          id: 4,
1628
                          flags: <SemanticsFlag>[SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable, SemanticsFlag.isSlider],
1629 1630 1631 1632 1633 1634 1635
                          actions: <SemanticsAction>[SemanticsAction.increase, SemanticsAction.decrease],
                          value: '40',
                          increasedValue: '60',
                          decreasedValue: '20',
                          textDirection: TextDirection.ltr,
                        ),
                      ],
1636 1637 1638 1639
                    ),
                  ],
                ),
              ],
1640
            ),
1641 1642 1643 1644 1645 1646
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );
1647 1648 1649
    semantics.dispose();
  });

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
  testWidgets('Slider is focusable and has correct focus color', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'Slider');
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    double value = 0.5;
    Widget buildApp({bool enabled = true}) {
      return MaterialApp(
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return SliderTheme(
                data: SliderThemeData(
                  overlayColor: Colors.orange[500],
                ),
                child: Slider(
                  value: value,
                  onChanged: enabled ? (double newValue) {
                    setState(() {
                      value = newValue;
                    });
                  } : null,
                  autofocus: true,
                  focusNode: focusNode,
                ),
              );
            }),
          ),
        ),
      );
    }
    await tester.pumpWidget(buildApp());

    // Check that the overlay shows when focused.
    await tester.pumpAndSettle();
    expect(focusNode.hasPrimaryFocus, isTrue);
    expect(
      Material.of(tester.element(find.byType(Slider))),
      paints..circle(color: Colors.orange[500]),
    );

    // Check that the overlay does not show when focused and disabled.
    await tester.pumpWidget(buildApp(enabled: false));
    await tester.pumpAndSettle();
    expect(focusNode.hasPrimaryFocus, isFalse);
    expect(
      Material.of(tester.element(find.byType(Slider))),
      isNot(paints..circle(color: Colors.orange[500])),
    );
  });

  testWidgets('Slider can be hovered and has correct hover color', (WidgetTester tester) async {
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    double value = 0.5;
    Widget buildApp({bool enabled = true}) {
      return MaterialApp(
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return SliderTheme(
                data: SliderThemeData(
                  overlayColor: Colors.orange[500],
                ),
                child: Slider(
                  value: value,
                  onChanged: enabled ? (double newValue) {
                    setState(() {
                      value = newValue;
                    });
                  } : null,
                ),
              );
            }),
          ),
        ),
      );
    }
    await tester.pumpWidget(buildApp());

    // Slider does not have overlay when enabled and not hovered.
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(find.byType(Slider))),
      isNot(paints..circle(color: Colors.orange[500])),
    );

    // Start hovering.
    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await gesture.addPointer();
    addTearDown(gesture.removePointer);
    await gesture.moveTo(tester.getCenter(find.byType(Slider)));

    // Slider has overlay when enabled and hovering.
    await tester.pumpWidget(buildApp());
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(find.byType(Slider))),
      paints..circle(color: Colors.orange[500]),
    );

    // Slider does not have an overlay when disabled and hovering.
    await tester.pumpWidget(buildApp(enabled: false));
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(find.byType(Slider))),
      isNot(paints..circle(color: Colors.orange[500])),
    );
  });

  testWidgets('Slider can be incremented and decremented by keyboard shortcuts - LTR', (WidgetTester tester) async {
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    double value = 0.5;
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Slider(
                value: value,
                onChanged: (double newValue) {
                  setState(() {
                    value = newValue;
                  });
                },
                autofocus: true,
              );
            }),
          ),
        ),
      ),
    );
    await tester.pumpAndSettle();

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
    await tester.pumpAndSettle();
    expect(value, 0.55);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
    await tester.pumpAndSettle();
    expect(value, 0.5);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
    await tester.pumpAndSettle();
    expect(value, 0.55);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
    await tester.pumpAndSettle();
    expect(value, 0.5);
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android,  TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows }));

  testWidgets('Slider can be incremented and decremented by keyboard shortcuts - LTR', (WidgetTester tester) async {
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    double value = 0.5;
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Slider(
                value: value,
                onChanged: (double newValue) {
                  setState(() {
                    value = newValue;
                  });
                },
                autofocus: true,
              );
            }),
          ),
        ),
      ),
    );
    await tester.pumpAndSettle();

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
    await tester.pumpAndSettle();
    expect(value, 0.6);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
    await tester.pumpAndSettle();
    expect(value, 0.5);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
    await tester.pumpAndSettle();
    expect(value, 0.6);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
    await tester.pumpAndSettle();
    expect(value, 0.5);
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));

  testWidgets('Slider can be incremented and decremented by keyboard shortcuts - RTL', (WidgetTester tester) async {
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    double value = 0.5;
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Directionality(
                textDirection: TextDirection.rtl,
                child: Slider(
                  value: value,
                  onChanged: (double newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                  autofocus: true,
                ),
              );
            }),
          ),
        ),
      ),
    );
    await tester.pumpAndSettle();

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
    await tester.pumpAndSettle();
    expect(value, 0.45);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
    await tester.pumpAndSettle();
    expect(value, 0.5);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
    await tester.pumpAndSettle();
    expect(value, 0.55);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
    await tester.pumpAndSettle();
    expect(value, 0.5);
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android,  TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows }));

  testWidgets('Slider can be incremented and decremented by keyboard shortcuts - RTL', (WidgetTester tester) async {
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    double value = 0.5;
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Directionality(
                textDirection: TextDirection.rtl,
                child: Slider(
                  value: value,
                  onChanged: (double newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                  autofocus: true,
                ),
              );
            }),
          ),
        ),
      ),
    );
    await tester.pumpAndSettle();

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
    await tester.pumpAndSettle();
    expect(value, 0.4);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
    await tester.pumpAndSettle();
    expect(value, 0.5);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
    await tester.pumpAndSettle();
    expect(value, 0.6);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
    await tester.pumpAndSettle();
    expect(value, 0.5);
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));

1927
  testWidgets('Value indicator appears when it should', (WidgetTester tester) async {
1928
    final ThemeData baseTheme = ThemeData(
1929 1930 1931 1932 1933
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
    SliderThemeData theme = baseTheme.sliderTheme;
    double value = 0.45;
1934 1935
    Widget buildApp({ required SliderThemeData sliderTheme, int? divisions, bool enabled = true }) {
      final ValueChanged<double>? onChanged = enabled ? (double d) => value = d : null;
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
      return MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: Material(
              child: Center(
                child: Theme(
                  data: baseTheme,
                  child: SliderTheme(
                    data: sliderTheme,
                    child: Slider(
                      value: value,
                      label: '$value',
                      divisions: divisions,
                      onChanged: onChanged,
                    ),
1953
                  ),
1954 1955 1956 1957 1958 1959 1960 1961
                ),
              ),
            ),
          ),
        ),
      );
    }

1962
    Future<void> expectValueIndicator({
1963 1964 1965
      required bool isVisible,
      required SliderThemeData theme,
      int? divisions,
1966
      bool enabled = true,
1967 1968
    }) async {
      // Discrete enabled widget.
1969 1970 1971
      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);
1972
      // Wait for value indicator animation to finish.
1973
      await tester.pumpAndSettle();
1974

1975

1976
      final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay));
1977
      expect(
1978
        valueIndicatorBox,
1979
        isVisible
1980 1981
            ? (paints..path(color: theme.valueIndicatorColor)..paragraph())
            : isNot(paints..path(color: theme.valueIndicatorColor)..paragraph()),
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
      );
      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);
  });
2013 2014

  testWidgets("Slider doesn't start any animations after dispose", (WidgetTester tester) async {
2015
    final Key sliderKey = UniqueKey();
2016 2017
    double value = 0.0;
    await tester.pumpWidget(
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: Slider(
                      key: sliderKey,
                      value: value,
                      divisions: 4,
                      onChanged: (double newValue) {
                        setState(() {
                          value = newValue;
                        });
                      },
                    ),
2037 2038
                  ),
                ),
2039 2040 2041
              );
            },
          ),
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
        ),
      ),
    );

    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.
2052
    await tester.pumpWidget(Container());
2053 2054 2055
    expect(await tester.pumpAndSettle(const Duration(milliseconds: 100)), equals(1));
    await gesture.up();
  });
2056

2057 2058
  testWidgets('Slider removes value indicator from overlay if Slider gets disposed without value indicator animation completing.', (WidgetTester tester) async {
    final Key sliderKey = UniqueKey();
2059
    const Color fillColor = Color(0xf55f5f5f);
2060 2061 2062
    double value = 0.0;

    Widget buildApp({
2063
      int? divisions,
2064 2065 2066
      bool enabled = true,
    }) {
      return MaterialApp(
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
        home: Scaffold(
          body: Builder(
            // The builder is used to pass the context from the MaterialApp widget
            // to the [Navigator]. This context is required in order for the
            // Navigator to work.
            builder: (BuildContext context) {
              return Column(
                children: <Widget>[
                  Slider(
                    key: sliderKey,
                    min: 0.0,
                    max: 100.0,
                    divisions: divisions,
                    label: '${value.round()}',
                    value: value,
                    onChanged: (double newValue) {
                      value = newValue;
                    },
                  ),
2086
                  ElevatedButton(
2087 2088
                    child: const Text('Next'),
                    onPressed: () {
2089
                      Navigator.of(context).pushReplacement(
2090 2091
                        MaterialPageRoute<void>(
                          builder: (BuildContext context) {
2092
                            return ElevatedButton(
2093
                              child: const Text('Inner page'),
2094
                              onPressed: () { Navigator.of(context).pop(); },
2095 2096 2097 2098 2099 2100 2101 2102 2103
                            );
                          },
                        ),
                      );
                    },
                  ),
                ],
              );
            },
2104 2105 2106 2107 2108 2109 2110
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp(divisions: 3));

2111
    final RenderObject valueIndicatorBox = tester.renderObject(find.byType(Overlay));
2112 2113 2114 2115 2116 2117 2118 2119 2120
    final Offset topRight = tester.getTopRight(find.byType(Slider)).translate(-24, 0);
    final TestGesture gesture = await tester.startGesture(topRight);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();

    expect(find.byType(Slider), isNotNull);
    expect(
      valueIndicatorBox,
      paints
2121 2122 2123 2124 2125 2126
        // Represents the raised button with text, next.
        ..path(color: Colors.black)
        ..paragraph()
        // Represents the Slider.
        ..path(color: fillColor)
        ..paragraph()
2127 2128
    );

2129 2130 2131
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 2));
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawParagraph, 2));

2132 2133 2134 2135 2136 2137 2138
    await tester.tap(find.text('Next'));
    await tester.pumpAndSettle();

    expect(find.byType(Slider), findsNothing);
    expect(
      valueIndicatorBox,
      isNot(
2139 2140 2141
        paints
          ..path(color: fillColor)
          ..paragraph(),
2142 2143 2144
      ),
    );

2145
    // Represents the ElevatedButton with inner Text, inner page.
2146 2147 2148
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1));
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawParagraph, 1));

2149 2150 2151 2152 2153
    // Don't stop holding the value indicator.
    await gesture.up();
    await tester.pumpAndSettle();
  });

2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
  testWidgets('Slider.adaptive', (WidgetTester tester) async {
    double value = 0.5;

    Widget buildFrame(TargetPlatform platform) {
      return MaterialApp(
        theme: ThemeData(platform: platform),
        home: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Material(
              child: Center(
                child: Slider.adaptive(
                  value: value,
                  onChanged: (double newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                ),
              ),
            );
          },
        ),
      );
    }

Dan Field's avatar
Dan Field committed
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
    for (final TargetPlatform platform in <TargetPlatform>[TargetPlatform.iOS, TargetPlatform.macOS]) {
      value = 0.5;
      await tester.pumpWidget(buildFrame(platform));
      expect(find.byType(Slider), findsOneWidget);
      expect(find.byType(CupertinoSlider), findsOneWidget);

      expect(value, 0.5, reason: 'on ${describeEnum(platform)}');
      final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(CupertinoSlider)));
      // Drag to the right end of the track.
      await gesture.moveBy(const Offset(600.0, 0.0));
      expect(value, 1.0, reason: 'on ${describeEnum(platform)}');
      await gesture.up();
    }

2193
    for (final TargetPlatform platform in <TargetPlatform>[TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows]) {
Dan Field's avatar
Dan Field committed
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
      value = 0.5;
      await tester.pumpWidget(buildFrame(platform));
      await tester.pumpAndSettle(); // Finish the theme change animation.
      expect(find.byType(Slider), findsOneWidget);
      expect(find.byType(CupertinoSlider), findsNothing);

      expect(value, 0.5, reason: 'on ${describeEnum(platform)}');
      final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(Slider)));
      // Drag to the right end of the track.
      await gesture.moveBy(const Offset(600.0, 0.0));
      expect(value, 1.0, reason: 'on ${describeEnum(platform)}');
      await gesture.up();
    }
2207
  });
2208 2209 2210 2211 2212

  testWidgets('Slider respects height from theme', (WidgetTester tester) async {
    final Key sliderKey = UniqueKey();
    double value = 0.0;
    await tester.pumpWidget(
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              final SliderThemeData sliderTheme = SliderTheme.of(context).copyWith(tickMarkShape: TallSliderTickMarkShape());
              return MediaQuery(
                data: MediaQueryData.fromWindow(window),
                child: Material(
                  child: Center(
                    child: IntrinsicHeight(
                      child: SliderTheme(
                        data: sliderTheme,
                        child: Slider(
                          key: sliderKey,
                          value: value,
                          divisions: 4,
                          onChanged: (double newValue) {
                            setState(() {
                              value = newValue;
                            });
                          },
                        ),
2236 2237 2238 2239
                      ),
                    ),
                  ),
                ),
2240 2241 2242
              );
            },
          ),
2243 2244 2245 2246 2247 2248 2249
        ),
      ),
    );

    final RenderBox renderObject = tester.renderObject<RenderBox>(find.byType(Slider));
    expect(renderObject.size.height, 200);
  });
2250

2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
  testWidgets('Slider changes mouse cursor when hovered', (WidgetTester tester) async {
    // Test Slider() constructor
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: Material(
            child: Center(
              child: MouseRegion(
                cursor: SystemMouseCursors.forbidden,
                child: Slider(
                  mouseCursor: SystemMouseCursors.text,
                  value: 0.5,
                  onChanged: (double newValue) { },
                ),
              ),
            ),
          ),
        ),
      )
    );

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
    await gesture.addPointer(location: tester.getCenter(find.byType(Slider)));
    addTearDown(gesture.removePointer);

    await tester.pump();

2279
    expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301

    // Test Slider.adaptive() constructor
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: Material(
            child: Center(
              child: MouseRegion(
                cursor: SystemMouseCursors.forbidden,
                child: Slider.adaptive(
                  mouseCursor: SystemMouseCursors.text,
                  value: 0.5,
                  onChanged: (double newValue) { },
                ),
              ),
            ),
          ),
        ),
      )
    );

2302
    expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323

    // Test default cursor
    await tester.pumpWidget(
      MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: Material(
            child: Center(
              child: MouseRegion(
                cursor: SystemMouseCursors.forbidden,
                child: Slider(
                  value: 0.5,
                  onChanged: (double newValue) { },
                ),
              ),
            ),
          ),
        ),
      )
    );

2324
    expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
2325 2326
  });

2327
  testWidgets('Slider implements debugFillProperties', (WidgetTester tester) async {
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();

    const Slider(
      activeColor: Colors.blue,
      divisions: 10,
      inactiveColor: Colors.grey,
      label: 'Set a value',
      max: 100.0,
      min: 0.0,
      onChanged: null,
      onChangeEnd: null,
      onChangeStart: null,
      semanticFormatterCallback: null,
      value: 50.0,
    ).debugFillProperties(builder);

    final List<String> description = builder.properties
      .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
      .map((DiagnosticsNode node) => node.toString()).toList();

    expect(description, <String>[
      'value: 50.0',
      'disabled',
      'min: 0.0',
      'max: 100.0',
      'divisions: 10',
      'label: "Set a value"',
      'activeColor: MaterialColor(primary value: Color(0xff2196f3))',
      'inactiveColor: MaterialColor(primary value: Color(0xff9e9e9e))',
    ]);
  });
2359

2360
  testWidgets('Slider track paints correctly when the shape is rectangular', (WidgetTester tester) async {
2361 2362 2363 2364 2365
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
            sliderTheme: const SliderThemeData(
              trackShape: RectangularSliderTrackShape(),
2366
            ),
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
        ),
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window),
            child: const Material(
              child: Center(
                child: Slider(
                  value: 0.5,
                  onChanged: null,
                ),
              ),
            ),
          ),
        ),
      ),
    );

    // _RenderSlider is the last render object in the tree.
    final RenderObject renderObject = tester.allRenderObjects.last;

    // The active track rect should start at 24.0 pixels,
    // and there should not have a gap between active and inactive track.
    expect(renderObject,
        paints
          ..rect(rect: const Rect.fromLTRB(24.0, 298.0, 400.0, 302.0)) // active track Rect.
          ..rect(rect: const Rect.fromLTRB(400.0, 298.0, 776.0, 302.0)) // inactive track Rect.
    );
  });
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430

  testWidgets('Slider can be painted in a narrower constraint', (WidgetTester tester) async {
    await tester.pumpWidget(
      const MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: Material(
            child: Center(
              child: SizedBox(
                height: 10.0,
                width: 10.0,
                child: Slider(
                  value: 0.5,
                  onChanged: null,
                ),
              ),
            ),
          ),
        ),
      ),
    );

    // _RenderSlider is the last render object in the tree.
    final RenderObject renderObject = tester.allRenderObjects.last;

    expect(renderObject,
        paints
          // active track RRect
          ..rrect(rrect: RRect.fromLTRBAndCorners(-14.0, 2.0, 5.0, 8.0, topLeft: const Radius.circular(3.0), bottomLeft: const Radius.circular(3.0)))
          // inactive track RRect
          ..rrect(rrect: RRect.fromLTRBAndCorners(5.0, 3.0, 24.0, 7.0, topRight: const Radius.circular(2.0), bottomRight: const Radius.circular(2.0)))
          // thumb
          ..circle(x: 5.0, y: 5.0, radius: 10.0, )
    );
  });
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457

  testWidgets('Update the divisions and value at the same time for Slider', (WidgetTester tester) async {
    // Regress test for https://github.com/flutter/flutter/issues/65943
    Widget buildFrame(double maxValue) {
      return MaterialApp(
        home: Material(
          child: Center(
            child: Slider.adaptive(
              value: 5,
              max: maxValue,
              divisions: maxValue.toInt(),
              onChanged: (double newValue) {},
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildFrame(10));

    // _RenderSlider is the last render object in the tree.
    final RenderObject renderObject = tester.allRenderObjects.last;

    // Update the divisions from 10 to 15, the thumb should be paint at the correct position.
    await tester.pumpWidget(buildFrame(15));
    await tester.pumpAndSettle(); // Finish the animation.

2458
    late RRect activeTrackRRect;
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
    expect(renderObject, paints..something((Symbol method, List<dynamic> arguments) {
      if (method != #drawRRect)
        return false;
      activeTrackRRect = arguments[0] as RRect;
      return true;
    }));

    // The thumb should at one-third(5 / 15) of the Slider.
    // The right of the active track shape is the position of the thumb.
    // 24.0 is the default margin, (800.0 - 24.0 - 24.0) is the slider's width.
    expect(nearEqual(activeTrackRRect.right, (800.0 - 24.0 - 24.0) * (5 / 15) + 24.0, 0.01), true);
  });
2471
}