Flutter : 显示天、时、分、秒的倒计时小工具。

问题描述 投票:0回答:1

我想建立一个可以显示剩余天数、小时数、分钟数和秒数的扩展小部件(见截图)。

我怎样才能实现这个功能呢?

我已经看了一大堆flutter文档,但还是无法正确创建它。

我试图建立的小部件的截图

谢谢你的帮助!我想建立一个扩展的小工具,可以显示天数、小时、分钟和秒数(见截图)。

flutter dart countdowntimer
1个回答
0
投票

你可以复制粘贴运行完整的代码下面 你可以使用包 https:/pub.devpackagesflutter_countdown_timer。

代码段

CountdownTimer(
            endTime: 1594829147719,
            defaultDays: "==",
            defaultHours: "--",
            defaultMin: "**",
            defaultSec: "++",
            daysSymbol: "days",
            hoursSymbol: "hrs ",
            minSymbol: "min ",
            secSymbol: "sec",
            daysTextStyle: TextStyle(fontSize: 30, color: Colors.red),
            hoursTextStyle: TextStyle(fontSize: 30, color: Colors.orange),
            minTextStyle: TextStyle(fontSize: 30, color: Colors.lightBlue),
            secTextStyle: TextStyle(fontSize: 30, color: Colors.pink),
            daysSymbolTextStyle:
                TextStyle(fontSize: 20, color: Colors.green),
            hoursSymbolTextStyle:
                TextStyle(fontSize: 20, color: Colors.amberAccent),
            minSymbolTextStyle:
                TextStyle(fontSize: 20, color: Colors.black),
            secSymbolTextStyle:
                TextStyle(fontSize: 20, color: Colors.deepOrange))

工作示范

enter image description here

全码

import 'package:flutter/material.dart';
import 'package:flutter_countdown_timer/countdown_timer.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            CountdownTimer(
                endTime: 1594829147719,
                defaultDays: "==",
                defaultHours: "--",
                defaultMin: "**",
                defaultSec: "++",
                daysSymbol: "days",
                hoursSymbol: "hrs ",
                minSymbol: "min ",
                secSymbol: "sec",
                daysTextStyle: TextStyle(fontSize: 30, color: Colors.red),
                hoursTextStyle: TextStyle(fontSize: 30, color: Colors.orange),
                minTextStyle: TextStyle(fontSize: 30, color: Colors.lightBlue),
                secTextStyle: TextStyle(fontSize: 30, color: Colors.pink),
                daysSymbolTextStyle:
                    TextStyle(fontSize: 20, color: Colors.green),
                hoursSymbolTextStyle:
                    TextStyle(fontSize: 20, color: Colors.amberAccent),
                minSymbolTextStyle:
                    TextStyle(fontSize: 20, color: Colors.black),
                secSymbolTextStyle:
                    TextStyle(fontSize: 20, color: Colors.deepOrange)),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.