cupertino_slider.0.dart 2.63 KB
Newer Older
1 2 3 4 5 6
// 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/cupertino.dart';

7 8
/// Flutter code sample for [CupertinoSlider].

9
void main() => runApp(const CupertinoSliderApp());
10

11
class CupertinoSliderApp extends StatelessWidget {
12
  const CupertinoSliderApp({super.key});
13 14 15 16

  @override
  Widget build(BuildContext context) {
    return const CupertinoApp(
17 18
      theme: CupertinoThemeData(brightness: Brightness.light),
      home: CupertinoSliderExample(),
19 20 21 22
    );
  }
}

23
class CupertinoSliderExample extends StatefulWidget {
24
  const CupertinoSliderExample({super.key});
25 26

  @override
27
  State<CupertinoSliderExample> createState() => _CupertinoSliderExampleState();
28 29
}

30
class _CupertinoSliderExampleState extends State<CupertinoSliderExample> {
31
  double _currentSliderValue = 0.0;
32 33 34 35 36
  String? _sliderStatus;

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
37 38 39
      navigationBar: const CupertinoNavigationBar(
        middle: Text('CupertinoSlider Sample'),
      ),
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            // Display the current slider value.
            Text('$_currentSliderValue'),
            CupertinoSlider(
              key: const Key('slider'),
              value: _currentSliderValue,
              // This allows the slider to jump between divisions.
              // If null, the slide movement is continuous.
              divisions: 5,
              // The maximum slider value
              max: 100,
              activeColor: CupertinoColors.systemPurple,
              thumbColor: CupertinoColors.systemPurple,
              // This is called when sliding is started.
              onChangeStart: (double value) {
                setState(() {
                  _sliderStatus = 'Sliding';
                });
              },
              // This is called when sliding has ended.
              onChangeEnd: (double value) {
                setState(() {
                  _sliderStatus = 'Finished sliding';
                });
              },
              // This is called when slider value is changed.
              onChanged: (double value) {
                setState(() {
                  _currentSliderValue = value;
                });
              },
            ),
            Text(
              _sliderStatus ?? '',
              style: CupertinoTheme.of(context).textTheme.textStyle.copyWith(
78 79
                    fontSize: 12,
                  ),
80 81 82 83 84 85 86
            ),
          ],
        ),
      ),
    );
  }
}