Flutter尺子选择插件simple_ruler_picker的使用
Flutter尺子选择插件simple_ruler_picker的使用
简介
SimpleRulerPicker 是一个用于Flutter的可定制化尺子样式数字选择器,允许用户在最小值和最大值之间滚动选择一个数字。
功能特性
- 可滚动数字选择器:用户可以在 
minValue和maxValue之间滚动选择。 - 可自定义刻度:可以调整刻度标签大小、刻度项之间的宽度以及尺子线条的高度。
 
预览
| Portrait | Landscape | 
|---|---|
![]()  | 
![]()  | 
开始使用
安装
在项目的 pubspec.yaml 文件中添加 simple_ruler_picker:
dependencies:
  simple_ruler_picker: ^0.1.0
或者直接通过命令行安装:
flutter pub add simple_ruler_picker
使用示例
以下是一个完整的示例代码,展示了如何在项目中使用 SimpleRulerPicker:
import 'package:flutter/material.dart';
import 'package:simple_ruler_picker/simple_ruler_picker.dart';
void main() {
  runApp(const MyApp());
}
class ExamplePage extends StatelessWidget {
  const ExamplePage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffF6F8FC),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            children: [
              PickerCard(
                title: '您的身高',
                height: 180,
                child: SimpleRulerPicker(
                  height: 100,
                  minValue: 0,
                  maxValue: 250,
                  initialValue: 180,
                  onValueChanged: (value) {
                    debugPrint(value.toString());
                  },
                ),
              ),
              PickerCard(
                title: '您的体重',
                height: 200,
                child: SimpleRulerPicker(
                  height: 130,
                  minValue: 50,
                  maxValue: 300,
                  initialValue: 100,
                  selectedColor: Colors.blue,
                  lineStroke: 3,
                  longLineHeight: 50,
                  shortLineHeight: 25,
                  scaleItemWidth: 20,
                  lineColor: Colors.grey,
                  onValueChanged: (value) {
                    debugPrint(value.toString());
                  },
                ),
              ),
              PickerCard(
                title: '垂直轴选择器',
                height: 500,
                width: 200,
                child: Padding(
                  padding: const EdgeInsets.symmetric(vertical: 16),
                  child: SimpleRulerPicker(
                    height: 400,
                    minValue: 50,
                    maxValue: 300,
                    initialValue: 100,
                    selectedColor: Colors.green,
                    lineStroke: 3,
                    longLineHeight: 50,
                    shortLineHeight: 25,
                    scaleItemWidth: 20,
                    scaleLabelSize: 14,
                    scaleLabelWidth: 40,
                    axis: Axis.vertical,
                    lineColor: Colors.grey,
                    onValueChanged: (value) {
                      debugPrint(value.toString());
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Simple Ruler Picker',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const ExamplePage(),
    );
  }
}
class PickerCard extends StatelessWidget {
  final String title;
  final double height;
  final double? width;
  final Widget child;
  const PickerCard({
    super.key,
    required this.title,
    required this.height,
    this.width,
    required this.child,
  });
  @override
  Widget build(BuildContext context) {
    return Container(
      height: height,
      width: width,
      margin: const EdgeInsets.all(16),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(32),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: const TextStyle(
              fontSize: 16,
              color: Colors.black,
              fontWeight: FontWeight.bold,
            ),
          ),
          child
        ],
      ),
    );
  }
}
属性说明
| 属性 | 类型 | 默认值 | 描述 | 示例 | 
|---|---|---|---|---|
minValue | 
int | 
0 | 最小可选值 | minValue: 0 | 
maxValue | 
int | 
200 | 最大可选值 | maxValue: 100 | 
initialValue | 
int | 
100 | 初始显示值(必须在 minValue 和 maxValue 之间) | 
initialValue: 50 | 
scaleLabelSize | 
double | 
14 | 刻度标签文本大小 | scaleLabelSize: 16 | 
scaleLabelWidth | 
double | 
40 | 垂直轴时刻度标签文本宽度 | scaleLabelWidth: 40 | 
scaleBottomPadding | 
double | 
6 | 刻度标签底部内边距 | scaleBottomPadding: 8 | 
scaleItemWidth | 
int | 
10 | 每个刻度项的宽度(即尺子线上间距) | scaleItemWidth: 15 | 
onValueChanged | 
ValueChanged<int> | 
null | 选择值变化时的回调函数 | onValueChanged: (value) => print(value) | 
longLineHeight | 
double | 
24 | 尺子长线高度(通常用于主单位) | longLineHeight: 30 | 
shortLineHeight | 
double | 
12 | 尺子短线高度(通常用于次单位) | shortLineHeight: 15 | 
lineColor | 
Color | 
Colors.grey | 
尺子线的颜色(包括长线和短线) | lineColor: Colors.black | 
selectedColor | 
Color | 
Colors.orange | 
当前选中项的颜色 | selectedColor: Colors.blue | 
labelColor | 
Color | 
Colors.grey | 
刻度标签颜色 | labelColor: Colors.black | 
lineStroke | 
double | 
2 | 尺子线的粗细(描边宽度) | lineStroke: 3 | 
height | 
double | 
100 | 尺子选择器的整体高度 | height: 120 | 
其他信息
贡献
我们欢迎贡献!如果您想改进 SimpleRulerPicker 或添加更多功能,请自由地 fork 仓库并提交 pull request。
提交问题
如果发现 bug 或需要增强功能,请在 GitHub 仓库 中提交 issue。
更多关于Flutter尺子选择插件simple_ruler_picker的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter尺子选择插件simple_ruler_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何使用Flutter尺子选择插件 simple_ruler_picker 的代码案例。假设你已经将 simple_ruler_picker 添加到你的 pubspec.yaml 文件中,并且已经运行了 flutter pub get。
首先,确保你的 pubspec.yaml 文件中包含以下依赖项:
dependencies:
  flutter:
    sdk: flutter
  simple_ruler_picker: ^最新版本号 # 请替换为实际的最新版本号
接下来,在你的 Dart 文件中,你可以按照以下方式使用 simple_ruler_picker:
import 'package:flutter/material.dart';
import 'package:simple_ruler_picker/simple_ruler_picker.dart';
void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Ruler Picker Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: RulerPickerScreen(),
    );
  }
}
class RulerPickerScreen extends StatefulWidget {
  @override
  _RulerPickerScreenState createState() => _RulerPickerScreenState();
}
class _RulerPickerScreenState extends State<RulerPickerScreen> {
  double selectedValue = 0.0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Ruler Picker Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Selected Value: $selectedValue',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 20),
            SimpleRulerPicker(
              minValue: 0.0,
              maxValue: 100.0,
              step: 1.0,
              value: selectedValue,
              onChanged: (newValue) {
                setState(() {
                  selectedValue = newValue;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}
在这个示例中,我们创建了一个简单的 Flutter 应用,其中包含一个尺子选择器(SimpleRulerPicker)。以下是代码的关键部分:
- 导入包:首先,我们导入了 
flutter和simple_ruler_picker包。 - 主应用:
MyApp是一个无状态小部件,它定义了应用的主题和主页。 - 尺子选择器屏幕:
RulerPickerScreen是一个有状态小部件,它包含一个文本小部件,用于显示当前选中的值,以及一个SimpleRulerPicker小部件。 - 尺子选择器:
SimpleRulerPicker被配置为从 0.0 到 100.0 的范围,步长为 1.0。value属性绑定到selectedValue状态变量,当用户在尺子上选择一个新值时,onChanged回调会更新selectedValue。 
这个示例展示了如何使用 simple_ruler_picker 来创建一个简单的尺子选择器,并将用户的选择实时反映到界面上。你可以根据需要调整尺子的范围、步长和其他属性。
        
      
            
            
            


