Flutter iOS风格滑块插件better_cupertino_slider的使用

发布于 1周前 作者 yibo5220 来自 Flutter

Flutter iOS风格滑块插件better_cupertino_slider的使用

安装插件

首先,你需要在 pubspec.yaml 文件中添加以下依赖项:

dependencies:
  better_cupertino_slider: ^1ersion^

然后运行以下命令来 安装插件:

flutter packages get

示例代码

下面是一个完整的示例代码,展示了如何使用 better_cupertino_slider 插件创建一个iOS风格的滑块。

import 'package:flutter/material.dart';
import 'package:better_cupertino_slider/better_cupertino_slider.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  double sliderValue = 2.5;
  final double maxValue = 5.0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  "Current Value : ${sliderValue.toStringAsFixed(2)}",
                  style: TextStyle(
                    color: Colors.redAccent,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text("normal"),
              ),
              Container(
                constraints: BoxConstraints.expand(height: 44),
                margin: EdgeInsets.symmetric(horizontal: 20),
                child: BetterCupertinoSlider(
                  min: 0.0,
                  max: maxValue,
                  value: sliderValue,
                  configure: BetterCupertinoSliderConfigure(),
                  onChanged: (value) {
                    setState(() {
                      sliderValue = value;
                    });
                  },
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text("custom track and thumb"),
              ),
              Container(
                constraints: BoxConstraints.expand(height: 44),
                margin: EdgeInsets.symmetric(horizontal: 20),
                child: BetterCupertinoSlider(
                  min: 0.0,
                  max: maxValue,
                  value: sliderValue,
                  configure: BetterCupertinoSliderConfigure(
                    trackHorizontalPadding: 8.0,
                    trackHeight: 6.0,
                    trackLeftColor: Colors.greenAccent,
                    trackRightColor: Colors.redAccent,
                  ),
                  onChanged: (value) {
                    setState(() {
                      sliderValue = value;
                    });
                  },
                  onChangeStart: (value) {},
                  onChangeEnd: (value) {},
                ),
              ),
              Container(
                constraints: BoxConstraints.expand(height: 44),
                margin: EdgeInsets.symmetric(horizontal: 20),
                child: BetterCupertinoSlider(
                  min: 0.0,
                  max: maxValue,
                  value: sliderValue,
                  configure: BetterCupertinoSliderConfigure(
                    trackHorizontalPadding: 8.0,
                    trackHeight: 4.0,
                    trackLeftColor: const Color(0xFFFF6B26),
                    trackRightColor: const Color(0xFFE3E9EF),
                    thumbRadius: 8.0,
                    thumbPainter: (canvas, rect) {
                      final RRect rrect = RRect.fromRectAndRadius(
                        rect,
                        Radius.circular(rect.shortestSide / 2.0),
                      );
                      const Color borderColor = Color(0x08000000);

                      // draw shadow
                      for (final BoxShadow shadow in kBetterSliderBoxShadows)
                        canvas.drawRRect(
                            rrect.shift(shadow.offset), shadow.toPaint());

                      // draw border
                      canvas.drawRRect(
                        rrect.inflate(0.5),
                        Paint()..color = borderColor,
                      );

                      // draw background
                      canvas.drawRRect(rrect, Paint()..color = Colors.white);

                      // draw center point
                      canvas.drawRRect(
                          rrect.deflateate(4), Paint()..color = Color(0xFFFF671F));
                    },
                  ),
                  onChanged: (value) {
                    setState(() {
                      sliderValue = value;
                    });
                  },
                  onChangeStart: (value) {},
                  onChangeEnd: (value) {},
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text("step"),
              ),
              Container(
                constraints: BoxConstraints.expand(height: 44),
                margin: EdgeInsets.symmetric(horizontal: 20),
                child: BetterCupertinoSlider(
                  min: 0.0,
                  max: maxValue,
                  value: sliderValue,
                  configure: BetterCupertinoSliderConfigure(),
                  onChanged: (value) {
                    setState(() {
                      sliderValue = value;
                    });
                  },
                  onChangeStart: (value) {},
                  onChangeEnd: (value) {
                    setState(() {
                      sliderValue = value.round().toDouble();
                    });
                  },
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text("no thumb"),
              ),
              Container(
                constraints: BoxConstraints.expand(height: 44),
                margin: EdgeInsets.symmetric(horizontal: 20),
                child: BetterCupertinoSlider(
                  min: 0.0,
                  max: maxValue,
                  value: sliderValue,
                  configure: BetterCupertinoSliderConfigure(
                    thumbRadius: 2,
                    thumbPainter: (canvas, rect) {},
                  ),
                  onChangeStart: (value) {},
                  onChangeEnd: (value) {
                    setState(() {
                      sliderValue = value.round().toDouble();
                    });
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter iOS风格滑块插件better_cupertino_slider的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter iOS风格滑块插件better_cupertino_slider的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用better_cupertino_slider插件的示例代码。这个插件提供了iOS风格的滑块控件。

首先,确保你已经在pubspec.yaml文件中添加了better_cupertino_slider依赖项:

dependencies:
  flutter:
    sdk: flutter
  better_cupertino_slider: ^x.y.z  # 请替换为最新版本号

然后,运行flutter pub get来安装依赖项。

接下来,你可以在你的Flutter应用中使用这个滑块控件。以下是一个完整的示例代码:

import 'package:flutter/material.dart';
import 'package:better_cupertino_slider/better_cupertino_slider.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Better Cupertino Slider Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SliderScreen(),
    );
  }
}

class SliderScreen extends StatefulWidget {
  @override
  _SliderScreenState createState() => _SliderScreenState();
}

class _SliderScreenState extends State<SliderScreen> {
  double _value = 50.0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Better Cupertino Slider Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Slider Value: $_value',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 24),
            BetterCupertinoSlider(
              value: _value,
              minValue: 0,
              maxValue: 100,
              divisions: 100,
              onChanged: (double newValue) {
                setState(() {
                  _value = newValue;
                });
              },
              onDragStarted: () {
                print('Drag started');
              },
              onDragEnded: (double value) {
                print('Drag ended with value: $value');
              },
            ),
          ],
        ),
      ),
    );
  }
}

代码解释

  1. 依赖项添加

    • pubspec.yaml文件中添加better_cupertino_slider依赖项。
  2. 主应用结构

    • MyApp是一个无状态小部件,它定义了应用的主题和主屏幕。
    • SliderScreen是一个有状态小部件,它包含滑块和显示滑块值的文本。
  3. 滑块控件

    • BetterCupertinoSlider是核心控件,它接受多个参数,如value(当前值)、minValue(最小值)、maxValue(最大值)、divisions(分段数量)等。
    • onChanged回调用于更新滑块的值。
    • onDragStartedonDragEnded回调分别用于处理拖动开始和结束的事件。
  4. 界面布局

    • 使用Column布局将文本和滑块垂直排列。
    • SizedBox用于在控件之间添加间距。

运行应用

确保你已经连接了一个iOS模拟器或设备,然后运行flutter run命令来启动应用。你应该能够看到一个包含iOS风格滑块的界面,并且滑块的值会实时更新到文本中。

希望这个示例对你有帮助!如果有更多问题,请随时提问。

回到顶部