Flutter尺子选择插件flutter_ruler_picker的使用

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

Flutter尺子选择插件flutter_ruler_picker的使用

flutter_ruler_picker 是一个用于Flutter应用的尺子选择器插件,它可以帮助用户通过滑动选择数值。本文将介绍如何使用该插件,并提供一个完整的示例Demo。

示例效果

示例效果

示例代码

以下是一个完整的示例代码,展示了如何在Flutter项目中使用 flutter_ruler_picker 插件:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ruler_picker/flutter_ruler_picker.dart';

void main() => runApp(MyApp());

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

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  RulerPickerController? _rulerPickerController;

  num currentValue = 40;

  List<RulerRange> ranges = const [
    RulerRange(begin: 0, end: 10, scale: 0.1),
    RulerRange(begin: 10, end: 100, scale: 1),
    RulerRange(begin: 100, end: 1000, scale: 10),
    RulerRange(begin: 1000, end: 10000, scale: 100),
    RulerRange(begin: 10000, end: 100000, scale: 1000)
  ];

  @override
  void initState() {
    super.initState();
    _rulerPickerController = RulerPickerController(value: currentValue);
  }

  Widget _buildPositionBtn(num value) {
    return InkWell(
      onTap: () {
        _rulerPickerController?.value = value;
      },
      child: Container(
          padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
          color: Colors.blue,
          child: Text(
            value.toString(),
            style: TextStyle(color: Colors.white),
          )),
    );
  }

  Widget _buildChangeRangerBtn(String tip, List<RulerRange> rangeList) {
    return InkWell(
      onTap: () {
        setState(() {
          ranges = rangeList;
        });
      },
      child: Container(
          padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
          color: Colors.blue,
          child: Text(
            tip,
            style: TextStyle(color: Colors.white),
          )),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              currentValue.toStringAsFixed(1),
              style: const TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                  fontSize: 80),
            ),
            const SizedBox(height: 50),
            RulerPicker(
              controller: _rulerPickerController!,
              onBuildRulerScaleText: (index, value) {
                return value.toInt().toString();
              },
              ranges: ranges,
              scaleLineStyleList: const [
                ScaleLineStyle(
                    color: Colors.grey, width: 1.5, height: 30, scale: 0),
                ScaleLineStyle(
                    color: Colors.grey, width: 1, height: 25, scale: 5),
                ScaleLineStyle(
                    color: Colors.grey, width: 1, height: 15, scale: -1)
              ],
              onValueChanged: (value) {
                setState(() {
                  currentValue = value;
                });
              },
              width: MediaQuery.of(context).size.width,
              height: 80,
              rulerMarginTop: 8,
            ),
            SizedBox(height: 40),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildPositionBtn(8.4),
                SizedBox(width: 10),
                _buildPositionBtn(30),
                SizedBox(width: 10),
                _buildPositionBtn(50.5),
                SizedBox(width: 10),
                _buildPositionBtn(1000),
                SizedBox(width: 10),
                _buildPositionBtn(40000),
                SizedBox(width: 10),
                _buildPositionBtn(50000),
              ],
            ),
            SizedBox(height: 10),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildChangeRangerBtn("[20,100,1],[100,200,0.1]", [
                  RulerRange(begin: 20, end: 100, scale: 1),
                  RulerRange(begin: 100, end: 200, scale: 0.1)
                ]),
                SizedBox(width: 10),
                _buildChangeRangerBtn(
                    "[100,500]", [RulerRange(begin: 100, end: 500)]),
              ],
            )
          ],
        ),
      ),
    );
  }
}

关键点说明

  1. 导入依赖:首先确保在 pubspec.yaml 文件中添加了 flutter_ruler_picker 依赖:
    dependencies:
      flutter_ruler_picker: ^版本号
    

更多关于Flutter尺子选择插件flutter_ruler_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter尺子选择插件flutter_ruler_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用flutter_ruler_picker插件的一个基本示例。这个插件允许用户在界面上选择特定的长度或数值范围,非常适合尺子或滑动选择器的场景。

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

dependencies:
  flutter:
    sdk: flutter
  flutter_ruler_picker: ^最新版本号  # 请替换为实际最新版本号

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

接下来,在你的Dart文件中使用flutter_ruler_picker。以下是一个简单的示例,展示了如何集成和使用这个插件:

import 'package:flutter/material.dart';
import 'package:flutter_ruler_picker/flutter_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('Flutter Ruler Picker Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Selected Value: $selectedValue',
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 20),
            Expanded(
              child: RulerPicker(
                min: 0.0,
                max: 100.0,
                step: 1.0,
                value: selectedValue,
                onChange: (double newValue) {
                  setState(() {
                    selectedValue = newValue;
                  });
                },
                onChangeEnd: (double newValue) {
                  print('Final selected value: $newValue');
                },
                builder: (BuildContext context, double value) {
                  return Text(
                    '${value.toInt()}',
                    style: TextStyle(fontSize: 24, color: Colors.black),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个RulerPicker组件。这个组件允许用户在0到100之间选择一个值,步长为1。onChange回调会在用户拖动尺子时不断更新selectedValue,而onChangeEnd回调会在用户停止拖动时打印最终选择的值。

builder参数允许你自定义尺子上的文本显示,这里我们简单地显示了当前选择值的整数部分。

你可以根据需要调整minmaxstep等参数,以及自定义builder函数来满足你的特定需求。

回到顶部