Flutter数字时钟动画插件slide_digital_clock的使用

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

Flutter数字时钟动画插件slide_digital_clock的使用

使用

要使用此插件,只需导入包:

import 'package:slide_digital_clock/slide_digital_clock.dart';

默认值

is24HourTimeFormat : `true`
showSecondsDigit : `true`
areaAligment : `AlignmentDirectional.bottomCenter`
digitAnimationStyle : `Curves.decelerate`
hourMinuteDigitTextStyle : `Theme.of(context).textTheme.bodyText1`
secondDigitTextStyle : `Theme.of(context).textTheme.caption!.copyWith(fontSize: 10))`
amPmDigitTextStyle : `Theme.of(context).textTheme.caption!.copyWith(fontSize: 10, fontWeight: FontWeight.bold)`

演示

图片

示例

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

void main() {
  runApp(const ExampleApp());
}

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "Slider Digital Clock Demo App",
      home: Scaffold(
        appBar: AppBar(
            title: const Text("Slider Digital Clock Demo App"),
            actions: [
              // 在AppBar中添加一个数字时钟
              DigitalClock(
                hourMinuteDigitTextStyle: Theme.of(context)
                    .textTheme
                    .headline4!
                    .copyWith(color: Colors.white),
                secondDigitTextStyle: Theme.of(context)
                    .textTheme
                    .caption!
                    .copyWith(color: Colors.white),
                colon: Text(
                  ":",
                  style: Theme.of(context)
                      .textTheme
                      .subtitle1!
                      .copyWith(color: Colors.white),
                ),
              )
            ]),
        body: Column(
          children: [
            // 自定义分隔符
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const Text("自定义分隔符"),
                const SizedBox(width: 50),
                DigitalClock(
                  hourMinuteDigitTextStyle: const TextStyle(fontSize: 18),
                  colon: const Icon(Icons.ac_unit, size: 12),
                  colonDecoration: BoxDecoration(
                      border: Border.all(), shape: BoxShape.circle),
                )
              ],
            ),
            // 自定义样式
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const Text("自定义样式"),
                const SizedBox(width: 50),
                DigitalClock(
                  hourDigitDecoration: BoxDecoration(
                      color: Colors.yellow,
                      border: Border.all(color: Colors.blue, width: 2)),
                  minuteDigitDecoration: BoxDecoration(
                      color: Colors.yellow,
                      border: Border.all(color: Colors.red, width: 2)),
                  secondDigitDecoration: BoxDecoration(
                      color: Colors.blueGrey,
                      border: Border.all(color: Colors.blue),
                      shape: BoxShape.circle),
                  secondDigitTextStyle: Theme.of(context)
                      .textTheme
                      .caption!
                      .copyWith(color: Colors.white),
                ),
              ],
            ),
            // 显示AM-PM,禁用秒数
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const Text("显示AM-PM,禁用秒数"),
                const SizedBox(width: 50),
                DigitalClock(
                  is24HourTimeFormat: false,
                  showSecondsDigit: false,
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

更多关于Flutter数字时钟动画插件slide_digital_clock的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter数字时钟动画插件slide_digital_clock的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用slide_digital_clock插件来实现数字时钟动画的示例代码。

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

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

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

接下来是一个完整的Flutter应用示例,展示如何使用slide_digital_clock插件:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Slide Digital Clock Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ClockScreen(),
    );
  }
}

class ClockScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Slide Digital Clock'),
      ),
      body: Center(
        child: SlideDigitalClock(
          // 配置参数
          clockStyle: ClockStyle(
            // 自定义颜色
            primaryColor: Colors.blue,
            secondaryColor: Colors.white,
            // 自定义字体大小
            fontSize: 30.0,
            // 自定义滑动动画时间
            slideDuration: Duration(milliseconds: 1000),
          ),
          // 配置时间格式,HH:mm:ss
          timeFormat: 24HourFormat, // 或者 12HourFormat
          // 自定义时间更新间隔
          updateInterval: Duration(seconds: 1),
        ),
      ),
    );
  }
}

代码解释:

  1. 依赖引入: 在pubspec.yaml文件中添加slide_digital_clock依赖。

  2. 主应用入口MyApp类定义了一个基本的Flutter应用,设置了主题并指定了ClockScreen作为主页。

  3. ClockScreen

    • Scaffold:创建了一个包含应用栏的页面结构。
    • Center:将时钟居中显示。
    • SlideDigitalClock
      • clockStyle:用于自定义时钟的外观,包括颜色、字体大小、滑动动画时间等。
      • timeFormat:指定时间格式,可以是24小时制或12小时制。
      • updateInterval:设置时间更新的间隔。

注意事项:

  • 确保slide_digital_clock插件的最新版本号已经正确填写在pubspec.yaml文件中。
  • 你可以根据实际需求调整SlideDigitalClock的参数,比如颜色、字体大小、动画时间等。
  • 运行应用时,如果插件有任何更新或API变动,请参考插件的官方文档进行调整。

这样,你就可以在你的Flutter应用中实现一个带有动画效果的数字时钟了。

回到顶部