child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
DateTime.now()
.toLocal()
.toString()
.split('.')[0], // Display current date and time
style: const TextStyle(fontSize: 18),
),
const Text(
'00:00:00',
style: TextStyle(
fontSize: 36, fontWeight: FontWeight.bold),
),
],
),
),
这是我的代码。我是颤振的新手。如果您建议我设计用户界面的正确方法,我将不胜感激。而且我无法添加动画
我在给定的片段中为您创建了一个
Single class
秒表应用程序。
class MyStopwatch extends StatefulWidget {
const MyStopwatch({Key? key}) : super(key: key);
@override
State<MyStopwatch> createState() => _MyStopwatchState();
}
class _MyStopwatchState extends State<MyStopwatch> {
final Stopwatch _stopwatch = Stopwatch();
late Duration _elapsedTime;
late String _elapsedTimeString;
late Timer timer;
@override
void initState() {
super.initState();
_elapsedTime = Duration.zero;
_elapsedTimeString = _formatElapsedTime(_elapsedTime);
// Create a timer that runs a callback every 100 milliseconds to update UI
timer = Timer.periodic(const Duration(milliseconds: 100), (Timer timer) {
setState(() {
// Update elapsed time only if the stopwatch is running
if (_stopwatch.isRunning) {
_updateElapsedTime();
}
});
});
}
// Start/Stop button callback
void _startStopwatch() {
if (!_stopwatch.isRunning) {
// Start the stopwatch and update elapsed time
_stopwatch.start();
_updateElapsedTime();
} else {
// Stop the stopwatch
_stopwatch.stop();
}
}
// Reset button callback
void _resetStopwatch() {
// Reset the stopwatch to zero and update elapsed time
_stopwatch.reset();
_updateElapsedTime();
}
// Update elapsed time and formatted time string
void _updateElapsedTime() {
setState(() {
_elapsedTime = _stopwatch.elapsed;
_elapsedTimeString = _formatElapsedTime(_elapsedTime);
});
}
// Format a Duration into a string (MM:SS.SS)
String _formatElapsedTime(Duration time) {
return '${time.inMinutes.remainder(60).toString().padLeft(2, '0')}:${(time.inSeconds.remainder(60)).toString().padLeft(2, '0')}.${(time.inMilliseconds % 1000 ~/ 100).toString()}';
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
}
@override
void dispose() {
timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Stopwatch App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Display elapsed time
Text(
_elapsedTimeString,
style: const TextStyle(fontSize: 40.0),
),
const SizedBox(height: 20.0),
// Start/Stop and Reset buttons
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _startStopwatch,
child: Text(_stopwatch.isRunning ? 'Stop' : 'Start'),
),
const SizedBox(width: 20.0),
ElevatedButton(
onPressed: _resetStopwatch,
child: const Text('Reset'),
),
],
),
],
),
),
);
}
}
您必须了解的一些重要事项。它将帮助您修改 根据您的需要编码。
_stopwatch
变量是Stopwatch
类的实例,用于测量经过的时间。_elapsedTime
表示当前经过的时间,_elapsedTimeString
是用于在用户界面中显示经过的时间的格式化字符串。timer
是一个周期性计时器,每100毫秒更新一次UI(以显示经过的时间)。