Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit aaa4a52

Browse files
authored
Add Material 3 Slider example (#115638)
* Add Material 3 `Slider` example * Update doc * Update test titles
1 parent 7623486 commit aaa4a52

File tree

7 files changed

+193
-81
lines changed

7 files changed

+193
-81
lines changed

examples/api/lib/material/slider/slider.0.dart

+22-25
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,44 @@
66
77
import 'package:flutter/material.dart';
88

9-
void main() => runApp(const MyApp());
9+
void main() => runApp(const SliderApp());
1010

11-
class MyApp extends StatelessWidget {
12-
const MyApp({super.key});
13-
14-
static const String _title = 'Flutter Code Sample';
11+
class SliderApp extends StatelessWidget {
12+
const SliderApp({super.key});
1513

1614
@override
1715
Widget build(BuildContext context) {
18-
return MaterialApp(
19-
title: _title,
20-
home: Scaffold(
21-
appBar: AppBar(title: const Text(_title)),
22-
body: const MyStatefulWidget(),
23-
),
16+
return const MaterialApp(
17+
home: SliderExample(),
2418
);
2519
}
2620
}
2721

28-
class MyStatefulWidget extends StatefulWidget {
29-
const MyStatefulWidget({super.key});
22+
class SliderExample extends StatefulWidget {
23+
const SliderExample({super.key});
3024

3125
@override
32-
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
26+
State<SliderExample> createState() => _SliderExampleState();
3327
}
3428

35-
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
29+
class _SliderExampleState extends State<SliderExample> {
3630
double _currentSliderValue = 20;
3731

3832
@override
3933
Widget build(BuildContext context) {
40-
return Slider(
41-
value: _currentSliderValue,
42-
max: 100,
43-
divisions: 5,
44-
label: _currentSliderValue.round().toString(),
45-
onChanged: (double value) {
46-
setState(() {
47-
_currentSliderValue = value;
48-
});
49-
},
34+
return Scaffold(
35+
appBar: AppBar(title: const Text('Slider')),
36+
body: Slider(
37+
value: _currentSliderValue,
38+
max: 100,
39+
divisions: 5,
40+
label: _currentSliderValue.round().toString(),
41+
onChanged: (double value) {
42+
setState(() {
43+
_currentSliderValue = value;
44+
});
45+
},
46+
),
5047
);
5148
}
5249
}

examples/api/lib/material/slider/slider.1.dart

+25-38
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,48 @@
66
77
import 'package:flutter/material.dart';
88

9-
void main() => runApp(const MyApp());
9+
void main() => runApp(const SliderApp());
1010

11-
class MyApp extends StatelessWidget {
12-
const MyApp({super.key});
13-
14-
static const String _title = 'Flutter Code Sample';
11+
class SliderApp extends StatelessWidget {
12+
const SliderApp({super.key});
1513

1614
@override
1715
Widget build(BuildContext context) {
1816
return MaterialApp(
19-
title: _title,
20-
home: Scaffold(
21-
appBar: AppBar(title: const Text(_title)),
22-
body: const MyStatefulWidget(),
17+
theme: ThemeData(
18+
colorSchemeSeed: const Color(0xff6750a4),
19+
useMaterial3: true,
2320
),
21+
home: const SliderExample(),
2422
);
2523
}
2624
}
2725

28-
class MyStatefulWidget extends StatefulWidget {
29-
const MyStatefulWidget({super.key});
26+
class SliderExample extends StatefulWidget {
27+
const SliderExample({super.key});
3028

3129
@override
32-
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
30+
State<SliderExample> createState() => _SliderExampleState();
3331
}
3432

35-
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
36-
double _currentSliderPrimaryValue = 0.2;
37-
double _currentSliderSecondaryValue = 0.5;
33+
class _SliderExampleState extends State<SliderExample> {
34+
double _currentSliderValue = 20;
3835

3936
@override
4037
Widget build(BuildContext context) {
41-
return Column(
42-
mainAxisAlignment: MainAxisAlignment.center,
43-
children: <Widget>[
44-
Slider(
45-
value: _currentSliderPrimaryValue,
46-
secondaryTrackValue: _currentSliderSecondaryValue,
47-
label: _currentSliderPrimaryValue.round().toString(),
48-
onChanged: (double value) {
49-
setState(() {
50-
_currentSliderPrimaryValue = value;
51-
});
52-
},
53-
),
54-
Slider(
55-
value: _currentSliderSecondaryValue,
56-
label: _currentSliderSecondaryValue.round().toString(),
57-
onChanged: (double value) {
58-
setState(() {
59-
_currentSliderSecondaryValue = value;
60-
});
61-
},
62-
),
63-
],
38+
return Scaffold(
39+
appBar: AppBar(title: const Text('Slider')),
40+
body: Slider(
41+
value: _currentSliderValue,
42+
max: 100,
43+
divisions: 5,
44+
label: _currentSliderValue.round().toString(),
45+
onChanged: (double value) {
46+
setState(() {
47+
_currentSliderValue = value;
48+
});
49+
},
50+
),
6451
);
6552
}
6653
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Flutter code sample for [Slider].
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() => runApp(const SliderApp());
10+
11+
class SliderApp extends StatelessWidget {
12+
const SliderApp({super.key});
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return const MaterialApp(
17+
home: SliderExample(),
18+
);
19+
}
20+
}
21+
22+
class SliderExample extends StatefulWidget {
23+
const SliderExample({super.key});
24+
25+
@override
26+
State<SliderExample> createState() => _SliderExampleState();
27+
}
28+
29+
class _SliderExampleState extends State<SliderExample> {
30+
double _currentSliderPrimaryValue = 0.2;
31+
double _currentSliderSecondaryValue = 0.5;
32+
33+
@override
34+
Widget build(BuildContext context) {
35+
return Scaffold(
36+
appBar: AppBar(title: const Text('Slider')),
37+
body: Column(
38+
mainAxisAlignment: MainAxisAlignment.center,
39+
children: <Widget>[
40+
Slider(
41+
value: _currentSliderPrimaryValue,
42+
secondaryTrackValue: _currentSliderSecondaryValue,
43+
label: _currentSliderPrimaryValue.round().toString(),
44+
onChanged: (double value) {
45+
setState(() {
46+
_currentSliderPrimaryValue = value;
47+
});
48+
},
49+
),
50+
Slider(
51+
value: _currentSliderSecondaryValue,
52+
label: _currentSliderSecondaryValue.round().toString(),
53+
onChanged: (double value) {
54+
setState(() {
55+
_currentSliderSecondaryValue = value;
56+
});
57+
},
58+
),
59+
],
60+
),
61+
);
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/slider/slider.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Slider can change its value', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.SliderApp(),
13+
);
14+
15+
expect(find.byType(Slider), findsOneWidget);
16+
17+
final Finder sliderFinder = find.byType(Slider);
18+
19+
Slider slider = tester.widget(sliderFinder);
20+
expect(slider.value, 20);
21+
22+
final Offset center = tester.getCenter(sliderFinder);
23+
await tester.tapAt(Offset(center.dx + 100, center.dy));
24+
await tester.pump();
25+
26+
slider = tester.widget(sliderFinder);
27+
expect(slider.value, 60.0);
28+
});
29+
}

examples/api/test/material/slider/slider.1_test.dart

+10-16
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,23 @@ import 'package:flutter_api_samples/material/slider/slider.1.dart' as example;
77
import 'package:flutter_test/flutter_test.dart';
88

99
void main() {
10-
testWidgets('Slider shows secondary track', (WidgetTester tester) async {
10+
testWidgets('Slider can change its value', (WidgetTester tester) async {
1111
await tester.pumpWidget(
12-
const example.MyApp(),
12+
const example.SliderApp(),
1313
);
1414

15-
expect(find.byType(Slider), findsNWidgets(2));
15+
expect(find.byType(Slider), findsOneWidget);
1616

17-
final Finder slider1Finder = find.byType(Slider).at(0);
18-
final Finder slider2Finder = find.byType(Slider).at(1);
17+
final Finder sliderFinder = find.byType(Slider);
1918

20-
Slider slider1 = tester.widget(slider1Finder);
21-
Slider slider2 = tester.widget(slider2Finder);
22-
expect(slider1.secondaryTrackValue, slider2.value);
19+
Slider slider = tester.widget(sliderFinder);
20+
expect(slider.value, 20);
2321

24-
const double targetValue = 0.8;
25-
final Rect rect = tester.getRect(slider2Finder);
26-
final Offset target = Offset(rect.left + (rect.right - rect.left) * targetValue, rect.top + (rect.bottom - rect.top) / 2);
27-
await tester.tapAt(target);
22+
final Offset center = tester.getCenter(sliderFinder);
23+
await tester.tapAt(Offset(center.dx + 100, center.dy));
2824
await tester.pump();
2925

30-
slider1 = tester.widget(slider1Finder);
31-
slider2 = tester.widget(slider2Finder);
32-
expect(slider1.secondaryTrackValue, closeTo(targetValue, 0.05));
33-
expect(slider1.secondaryTrackValue, slider2.value);
26+
slider = tester.widget(sliderFinder);
27+
expect(slider.value, 60.0);
3428
});
3529
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/slider/slider.2.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Slider shows secondary track', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.SliderApp(),
13+
);
14+
15+
expect(find.byType(Slider), findsNWidgets(2));
16+
17+
final Finder slider1Finder = find.byType(Slider).at(0);
18+
final Finder slider2Finder = find.byType(Slider).at(1);
19+
20+
Slider slider1 = tester.widget(slider1Finder);
21+
Slider slider2 = tester.widget(slider2Finder);
22+
expect(slider1.secondaryTrackValue, slider2.value);
23+
24+
const double targetValue = 0.8;
25+
final Rect rect = tester.getRect(slider2Finder);
26+
final Offset target = Offset(rect.left + (rect.right - rect.left) * targetValue, rect.top + (rect.bottom - rect.top) / 2);
27+
await tester.tapAt(target);
28+
await tester.pump();
29+
30+
slider1 = tester.widget(slider1Finder);
31+
slider2 = tester.widget(slider2Finder);
32+
expect(slider1.secondaryTrackValue, closeTo(targetValue, 0.05));
33+
expect(slider1.secondaryTrackValue, slider2.value);
34+
});
35+
}

packages/flutter/lib/src/material/slider.dart

+9-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ enum _SliderType { material, adaptive }
4141
/// {@youtube 560 315 https://www.youtube.com/watch?v=ufb4gIPDmEs}
4242
///
4343
/// {@tool dartpad}
44-
/// ![A slider widget, consisting of 5 divisions and showing the default value
44+
/// ![A legacy slider widget, consisting of 5 divisions and showing the default value
4545
/// indicator.](https://flutter.github.io/assets-for-api-docs/assets/material/slider.png)
4646
///
4747
/// The Sliders value is part of the Stateful widget subclass to change the value
@@ -51,10 +51,17 @@ enum _SliderType { material, adaptive }
5151
/// {@end-tool}
5252
///
5353
/// {@tool dartpad}
54+
/// This sample shows the creation of a [Slider] using [ThemeData.useMaterial3] flag,
55+
/// as described in: https://m3.material.io/components/sliders/overview.
56+
///
57+
/// ** See code in examples/api/lib/material/slider/slider.1.dart **
58+
/// {@end-tool}
59+
///
60+
/// {@tool dartpad}
5461
/// This example shows a [Slider] widget using the [Slider.secondaryTrackValue]
5562
/// to show a secondary track in the slider.
5663
///
57-
/// ** See code in examples/api/lib/material/slider/slider.1.dart **
64+
/// ** See code in examples/api/lib/material/slider/slider.2.dart **
5865
/// {@end-tool}
5966
///
6067
/// A slider can be used to select from either a continuous or a discrete set of

0 commit comments

Comments
 (0)