Flutter时间范围选择插件time_range_selector_widget的使用

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

Flutter时间范围选择插件time_range_selector_widget的使用

Time Range Selector Widget

轻松选择时间范围。

注意

现在Material 3 主题已启用。

开始

要在项目中使用TimeRangeSelectorWidget,请按照以下步骤操作:

  1. 在项目中添加以下代码:
TimeRangeSelectorWidget(
    onChangeValue: (currentTime) {
        // TODO: 添加您的回调函数
    },
    childBuilder: (currentTime) {
        // TODO: 添加您的widget
        return Center(
            child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                    Text(currentTime.toString(), style: const TextStyle(fontSize: 70, fontWeight: FontWeight.bold, height: 1)),
                    Text(currentTime > 1 ? "Hours" : "Hour", style: const TextStyle(fontSize: 220, fontWeight: FontWeight.normal)),
                ],
            ),
        );
    },
)

示例

Example

Handler Builder

handleBuilder: (itemIndex, offset) {
    return Container(
        width: stockWidth - 8,
        height: stockWidth - 8,
        padding: const EdgeInsets(8),
        decoration: const BoxDecoration(
            color: Colors.white,
            shape: BoxShape.circle,
        ),
        child: FittedBox(child: Text((itemIndex + minTime).toString(), style: Theme.of(context).textTheme.headlineSmall?.copyWith(fontWeight: FontWeight.bold))),
    );
}

示例 - Handler Builder

Example - Handler Builder

Index Builder

positionalDotBuilder: (itemIndex, offset) {
    return FittedBox(child: Text((itemIndex + minTime).toString()));
}

示例 - Index Builder

Example - Index Builder

子构建器 - 小时

childBuilder: (currentTime) {
    return Container(
        decoration: BoxDecoration(
            gradient: LinearGradient(
                colors: [
                    Colors.black.withOpacity(0.05),
                    Colors.black.withOpacity(0.1)
                ],
            ),
        ),
        child: Center(
            child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                    Text(currentTime.toString(), style: const TextStyle(fontSize: 70, fontWeight: FontWeight.bold, height: 1)),
                    Text(currentTime > 1 ? "Hours" : "Hour", style: const TextStyle(fontSize: 220, fontWeight: FontWeight.normal)),
                ],
            ),
        ),
    );
}

示例 - 子构建器 - 小时

Example - Child Builder for Hour

子构建器 - 分钟

childBuilder: (currentTime) {
    return Container(
        decoration: BoxDecoration(
            gradient: LinearGradient(
                colors: [
                    Colors.black.withOpacity(0.05),
                    Colors black.withOpacity(0.1)
                ],
            ),
        ),
        child: Center(
            child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                    Text((currentTime * 5).toString(), style: const TextStyle(fontSize: 70, fontWeight: FontWeight.bold, height: 1)),
                    Text(currentTime > 0 ? "Minutes" : "Minute", style: const TextStyle(fontSize: 220, fontWeight: FontWeight.normal)),
                ],
            ),
        ),
    );
}

示例 - 子构建器 - 分钟

Example - Child Builder for Minute

容易自定义

Easily Customizable

示例代码


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

1 回复

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


当然,下面是一个关于如何在Flutter中使用time_range_selector_widget插件的示例代码。这个插件允许用户选择一个时间范围。首先,确保你已经将插件添加到你的pubspec.yaml文件中:

dependencies:
  flutter:
    sdk: flutter
  time_range_selector_widget: ^最新版本号  # 替换为最新版本号

然后,运行flutter pub get来获取依赖。

以下是一个完整的示例,展示如何使用time_range_selector_widget

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Time Range Selector Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: TimeRangeSelectorDemo(),
    );
  }
}

class TimeRangeSelectorDemo extends StatefulWidget {
  @override
  _TimeRangeSelectorDemoState createState() => _TimeRangeSelectorDemoState();
}

class _TimeRangeSelectorDemoState extends State<TimeRangeSelectorDemo> {
  DateTime? startTime;
  DateTime? endTime;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Time Range Selector Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Selected Time Range:', style: TextStyle(fontSize: 18)),
            if (startTime != null && endTime != null) {
              Text(
                'Start: ${startTime!.toLocal().toString().split(' ')[1]} - End: ${endTime!.toLocal().toString().split(' ')[1]}',
                style: TextStyle(fontSize: 16),
              ),
            } else {
              Text('No time range selected', style: TextStyle(fontSize: 16, color: Colors.grey)),
            },
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                showModalBottomSheet(
                  context: context,
                  builder: (context) {
                    return TimeRangeSelectorWidget(
                      initialStartTime: DateTime.now().subtract(Duration(hours: 24)),
                      initialEndTime: DateTime.now(),
                      onTimeRangeSelected: (startTime, endTime) {
                        setState(() {
                          this.startTime = startTime;
                          this.endTime = endTime;
                        });
                        Navigator.of(context).pop();
                      },
                    );
                  },
                );
              },
              child: Text('Select Time Range'),
            ),
          ],
        ),
      ),
    );
  }
}

代码解释:

  1. 导入依赖

    import 'package:time_range_selector_widget/time_range_selector_widget.dart';
    
  2. 设置主应用

    void main() {
      runApp(MyApp());
    }
    
  3. 创建主组件

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Time Range Selector Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: TimeRangeSelectorDemo(),
        );
      }
    }
    
  4. 创建包含状态管理的组件

    class TimeRangeSelectorDemo extends StatefulWidget {
      @override
      _TimeRangeSelectorDemoState createState() => _TimeRangeSelectorDemoState();
    }
    
    class _TimeRangeSelectorDemoState extends State<TimeRangeSelectorDemo> {
      DateTime? startTime;
      DateTime? endTime;
    
      @override
      Widget build(BuildContext context) {
        // ...
      }
    }
    
  5. 构建UI

    • 显示当前选择的时间范围。
    • 使用ElevatedButton来触发时间选择器的模态底部表单。
    • 使用TimeRangeSelectorWidget来让用户选择时间范围,并通过onTimeRangeSelected回调更新状态。

这个示例展示了如何使用time_range_selector_widget插件在Flutter应用中实现时间范围的选择功能。确保在实际项目中根据需要进行调整和扩展。

回到顶部