在颤动中显示运行时钟

时间:2018-11-14 17:35:16

标签: android dart flutter

我想知道是否有任何方法可以显示飞镖的实时时钟? 日期和时间(例如11/14/2018 19:34),时间将继续运行。

时间可以从设备本身获取。

3 个答案:

答案 0 :(得分:7)

以下使用intl plugin将时间格式化为MM/dd/yyyy hh:mm:ss。确保更新您的pubspec.yaml

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

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

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

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  String _timeString;

  @override
  void initState() {
    _timeString = _formatDateTime(DateTime.now());
    Timer.periodic(Duration(seconds: 1), (Timer t) => _getTime());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text(_timeString),
      ),
    );
  }

  void _getTime() {
    final DateTime now = DateTime.now();
    final String formattedDateTime = _formatDateTime(now);
    setState(() {
      _timeString = formattedDateTime;
    });
  }

  String _formatDateTime(DateTime dateTime) {
    return DateFormat('MM/dd/yyyy hh:mm:ss').format(dateTime);
  }
}

答案 1 :(得分:1)

class Clock extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final now = DateTime.now();
    final clock =
        Stream<DateTime>.periodic(const Duration(seconds: 1), (count) {
      return now.add(Duration(seconds: count));
    });
    return StreamBuilder<DateTime>(
      stream: clock,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Text(snapshot.data.toIso8601String());
        }
        return Text(DateTime.now().toIso8601String());
      },
    );
  }
}

或者,您每次都可以DateTime.now()(我不知道哪种表现更好)。

class Clock extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final clock = Stream<DateTime>.periodic(const Duration(seconds: 1), (_) {
      return DateTime.now();
    });
    return StreamBuilder<DateTime>(
      stream: clock,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Text(snapshot.data.toIso8601String());
        }
        return Text(DateTime.now().toIso8601String());
      },
    );
  }
}

答案 2 :(得分:0)

这与Albert给出的代码相同,但是如果您不想使用intl包,则可以对以下代码进行以下更改:

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

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

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.red),
      home: FlutterTimeDemo(),
    );
  }
}

class FlutterTimeDemo extends StatefulWidget{
  @override
  _FlutterTimeDemoState createState()=> _FlutterTimeDemoState();

}

class _FlutterTimeDemoState extends State<FlutterTimeDemo>
{
  String _timeString;

  @override
  void initState(){
    _timeString = "${DateTime.now().hour} : ${DateTime.now().minute} :${DateTime.now().second}";
    Timer.periodic(Duration(seconds:1), (Timer t)=>_getCurrentTime());
    super.initState();
  }

 @override
 Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Fluter Test'),),
      body:Center(
        child: Text(_timeString, style: TextStyle(fontSize: 30),),
      ),
    );
  }

  void _getCurrentTime()  {
    setState(() {
  _timeString = "${DateTime.now().hour} : ${DateTime.now().minute} :${DateTime.now().second}";
    });
  }
}