This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Material 3
Slider
example (#115638)
* Add Material 3 `Slider` example * Update doc * Update test titles
- Loading branch information
1 parent
7623486
commit aaa4a52
Showing
7 changed files
with
193 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
/// Flutter code sample for [Slider]. | ||
import 'package:flutter/material.dart'; | ||
|
||
void main() => runApp(const SliderApp()); | ||
|
||
class SliderApp extends StatelessWidget { | ||
const SliderApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const MaterialApp( | ||
home: SliderExample(), | ||
); | ||
} | ||
} | ||
|
||
class SliderExample extends StatefulWidget { | ||
const SliderExample({super.key}); | ||
|
||
@override | ||
State<SliderExample> createState() => _SliderExampleState(); | ||
} | ||
|
||
class _SliderExampleState extends State<SliderExample> { | ||
double _currentSliderPrimaryValue = 0.2; | ||
double _currentSliderSecondaryValue = 0.5; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Slider')), | ||
body: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
Slider( | ||
value: _currentSliderPrimaryValue, | ||
secondaryTrackValue: _currentSliderSecondaryValue, | ||
label: _currentSliderPrimaryValue.round().toString(), | ||
onChanged: (double value) { | ||
setState(() { | ||
_currentSliderPrimaryValue = value; | ||
}); | ||
}, | ||
), | ||
Slider( | ||
value: _currentSliderSecondaryValue, | ||
label: _currentSliderSecondaryValue.round().toString(), | ||
onChanged: (double value) { | ||
setState(() { | ||
_currentSliderSecondaryValue = value; | ||
}); | ||
}, | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_api_samples/material/slider/slider.0.dart' as example; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
testWidgets('Slider can change its value', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const example.SliderApp(), | ||
); | ||
|
||
expect(find.byType(Slider), findsOneWidget); | ||
|
||
final Finder sliderFinder = find.byType(Slider); | ||
|
||
Slider slider = tester.widget(sliderFinder); | ||
expect(slider.value, 20); | ||
|
||
final Offset center = tester.getCenter(sliderFinder); | ||
await tester.tapAt(Offset(center.dx + 100, center.dy)); | ||
await tester.pump(); | ||
|
||
slider = tester.widget(sliderFinder); | ||
expect(slider.value, 60.0); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_api_samples/material/slider/slider.2.dart' as example; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
testWidgets('Slider shows secondary track', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const example.SliderApp(), | ||
); | ||
|
||
expect(find.byType(Slider), findsNWidgets(2)); | ||
|
||
final Finder slider1Finder = find.byType(Slider).at(0); | ||
final Finder slider2Finder = find.byType(Slider).at(1); | ||
|
||
Slider slider1 = tester.widget(slider1Finder); | ||
Slider slider2 = tester.widget(slider2Finder); | ||
expect(slider1.secondaryTrackValue, slider2.value); | ||
|
||
const double targetValue = 0.8; | ||
final Rect rect = tester.getRect(slider2Finder); | ||
final Offset target = Offset(rect.left + (rect.right - rect.left) * targetValue, rect.top + (rect.bottom - rect.top) / 2); | ||
await tester.tapAt(target); | ||
await tester.pump(); | ||
|
||
slider1 = tester.widget(slider1Finder); | ||
slider2 = tester.widget(slider2Finder); | ||
expect(slider1.secondaryTrackValue, closeTo(targetValue, 0.05)); | ||
expect(slider1.secondaryTrackValue, slider2.value); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters