From fd4b1e86e85d99640c608c5e5887a6517fab6359 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Tue, 15 Feb 2022 20:05:11 +0200 Subject: [PATCH] CupertinoSlidingSegmentedControl: Add an interactive example (#98156) --- ...cupertino_sliding_segmented_control.0.dart | 95 +++++++++++++++++++ ...tino_sliding_segmented_control.0_test.dart | 19 ++++ .../cupertino/sliding_segmented_control.dart | 9 ++ 3 files changed, 123 insertions(+) create mode 100644 examples/api/lib/cupertino/segmented_control/cupertino_sliding_segmented_control.0.dart create mode 100644 examples/api/test/cupertino/segmented_control/cupertino_sliding_segmented_control.0_test.dart diff --git a/examples/api/lib/cupertino/segmented_control/cupertino_sliding_segmented_control.0.dart b/examples/api/lib/cupertino/segmented_control/cupertino_sliding_segmented_control.0.dart new file mode 100644 index 0000000000..f2db3c8bf8 --- /dev/null +++ b/examples/api/lib/cupertino/segmented_control/cupertino_sliding_segmented_control.0.dart @@ -0,0 +1,95 @@ +// 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 CupertinoSlidingSegmentedControl + +import 'package:flutter/cupertino.dart'; + +enum Sky { midnight, viridian, cerulean } + +Map skyColors = { + Sky.midnight: const Color(0xff191970), + Sky.viridian: const Color(0xff40826d), + Sky.cerulean: const Color(0xff007ba7), +}; + +void main() => runApp(const MyApp()); + +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + static const String _title = 'CupertinoSlidingSegmentedControl Sample'; + + @override + Widget build(BuildContext context) { + return const CupertinoApp( + title: _title, + home: SegmentedControlSample(), + ); + } +} + +class SegmentedControlSample extends StatefulWidget { + const SegmentedControlSample({Key? key}) : super(key: key); + + @override + State createState() => _SegmentedControlSampleState(); +} + +class _SegmentedControlSampleState extends State { + Sky _selectedSegment = Sky.midnight; + + @override + Widget build(BuildContext context) { + return CupertinoPageScaffold( + backgroundColor: skyColors[_selectedSegment], + navigationBar: CupertinoNavigationBar( + // This Cupertino segmented control has the enum "Sky" as the type. + middle: CupertinoSlidingSegmentedControl( + backgroundColor: CupertinoColors.systemGrey2, + thumbColor: skyColors[_selectedSegment]!, + // This represents the currently selected segmented control. + groupValue: _selectedSegment, + // Callback that sets the selected segmented control. + onValueChanged: (Sky? value) { + if (value != null) { + setState(() { + _selectedSegment = value; + }); + } + }, + children: const { + Sky.midnight: Padding( + padding: EdgeInsets.symmetric(horizontal: 20), + child: Text( + 'Midnight', + style: TextStyle(color: CupertinoColors.white), + ), + ), + Sky.viridian: Padding( + padding: EdgeInsets.symmetric(horizontal: 20), + child: Text( + 'Viridian', + style: TextStyle(color: CupertinoColors.white), + ), + ), + Sky.cerulean: Padding( + padding: EdgeInsets.symmetric(horizontal: 20), + child: Text( + 'Cerulean', + style: TextStyle(color: CupertinoColors.white), + ), + ), + }, + ), + ), + child: Center( + child: Text( + 'Selected Segment: ${_selectedSegment.name}', + style: const TextStyle(color: CupertinoColors.white), + ), + ), + ); + } +} diff --git a/examples/api/test/cupertino/segmented_control/cupertino_sliding_segmented_control.0_test.dart b/examples/api/test/cupertino/segmented_control/cupertino_sliding_segmented_control.0_test.dart new file mode 100644 index 0000000000..3498b23cac --- /dev/null +++ b/examples/api/test/cupertino/segmented_control/cupertino_sliding_segmented_control.0_test.dart @@ -0,0 +1,19 @@ +// 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_api_samples/cupertino/segmented_control/cupertino_sliding_segmented_control.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Can change a selected segmented control', (WidgetTester tester) async { + await tester.pumpWidget( + const example.MyApp(), + ); + + expect(find.text('Selected Segment: midnight'), findsOneWidget); + await tester.tap(find.text('Cerulean')); + await tester.pumpAndSettle(); + expect(find.text('Selected Segment: cerulean'), findsOneWidget); + }); +} diff --git a/packages/flutter/lib/src/cupertino/sliding_segmented_control.dart b/packages/flutter/lib/src/cupertino/sliding_segmented_control.dart index 2b1ee3f96f..7a75237a82 100644 --- a/packages/flutter/lib/src/cupertino/sliding_segmented_control.dart +++ b/packages/flutter/lib/src/cupertino/sliding_segmented_control.dart @@ -283,6 +283,15 @@ class _SegmentSeparatorState extends State<_SegmentSeparator> with TickerProvide /// [thumbColor], [backgroundColor] arguments can be used to override the /// segmented control's colors from its defaults. /// +/// {@tool dartpad} +/// This example shows a [CupertinoSlidingSegmentedControl] with an enum type. +/// +/// The callback provided to [onValueChanged] should update the state of +/// the parent [StatefulWidget] using the [State.setState] method, so that +/// the parent gets rebuilt; for example: +/// +/// ** See code in examples/api/lib/cupertino/segmented_control/cupertino_sliding_segmented_control.0.dart ** +/// {@end-tool} /// See also: /// /// *