Flutter 如何在颤振中使用sharedpref保存最大持续时间?
我试图使用SharedReference保存持续时间,但前提是我的上一个持续时间优于当前持续时间,因此显示最大持续时间。我的问题是我不知道如何比较两个字符串的持续时间 这是代码谢谢(@ZeRj)Flutter 如何在颤振中使用sharedpref保存最大持续时间?,flutter,duration,Flutter,Duration,我试图使用SharedReference保存持续时间,但前提是我的上一个持续时间优于当前持续时间,因此显示最大持续时间。我的问题是我不知道如何比较两个字符串的持续时间 这是代码谢谢(@ZeRj) 我尝试了下面的代码,但我不能使用>和字符串,并尝试分析int,但不能用date,我尝试用regex提取每个十进制数,但它太复杂了,我有一些错误我不理解 save_max_lastPress()async{ SharedPreferences prefs = await SharedPref
我尝试了下面的代码,但我不能使用>和字符串,并尝试分析int,但不能用date,我尝试用regex提取每个十进制数,但它太复杂了,我有一些错误我不理解
save_max_lastPress()async{
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
if(_lastButtonPress>Max_lastButtonPress)
{
prefs.setString('_max_lastPress', Max_lastButtonPress) ;
}
}
);
}
为此,您必须将最大值保存为持续时间 使用
prefs.setInt(“maxDuration”,maxDuration.toSeconds())
将其保存为共享首选项中的int,并使用
Duration(seconds: prefs.getInt("maxDuration")
您可以简单地比较两个持续时间实例
我编辑了最后一个示例以实现此功能:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutterfly/SharedPrefs.dart';
import 'package:flutterfly/SharedPrefs.dart' as prefix0;
class TestWidget extends StatefulWidget {
@override
_TestWidgetState createState() => _TestWidgetState();
}
class _TestWidgetState extends State<TestWidget> {
DateTime _lastButtonPress;
String _pressDuration;
Timer _ticker;
Duration _maxDuration;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Time since button pressed"),
Text(_pressDuration),
Text("Maximal Duration"),
Text(_formatDuration(_maxDuration)),
RaisedButton(
child: Text("Press me"),
onPressed: () {
_lastButtonPress = DateTime.now();
_updateTimer();
sharedPreferences.setString("lastButtonPress",_lastButtonPress.toIso8601String());
},
)
],
),
);
}
@override
void initState() {
super.initState();
//load max duration, if there is none start with 0
_maxDuration = Duration(seconds:sharedPreferences.getInt("maxDuration")??0);
final lastPressString = sharedPreferences.getString("lastButtonPress");
_lastButtonPress = lastPressString!=null ? DateTime.parse(lastPressString) : DateTime.now();
_updateTimer();
_ticker = Timer.periodic(Duration(seconds:1),(_)=>_updateTimer());
}
@override
void dispose() {
_ticker.cancel();
super.dispose();
}
void _updateTimer() {
final duration = DateTime.now().difference(_lastButtonPress);
//check for new max duration here
Duration newMaxDuration = _maxDuration;
if(duration> _maxDuration) {
//save when current duration is a new max
newMaxDuration = duration;
sharedPreferences.setInt("maxDuration",newMaxDuration.inSeconds);
}
final newDuration =_formatDuration(duration);
setState(() {
_maxDuration = newMaxDuration;
_pressDuration = newDuration;
});
}
String _formatDuration(Duration duration) {
String twoDigits(int n) {
if (n >= 10) return "$n";
return "0$n";
}
String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
return "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
}
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
导入“包:flutfly/SharedPrefs.dart”;
导入“package:flatterfly/SharedPrefs.dart”作为前缀x0;
类TestWidget扩展了StatefulWidget{
@凌驾
_TestWidgetState createState();
}
类_TestWidgetState扩展了状态{
DateTime\u lastButton按;
字符串持续时间;
定时器;
持续时间_最大持续时间;
@凌驾
小部件构建(构建上下文){
返回中心(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(“按钮按下后的时间”),
文字(_pressDuration),
文本(“最长持续时间”),
文本(_formatDuration(_maxDuration)),
升起的按钮(
孩子:文本(“按我”),
已按下:(){
_lastButtonPress=DateTime.now();
_updateTimer();
sharedPreferences.setString(“lastButtonPress”,u lastButtonPress.toIso8601String());
},
)
],
),
);
}
@凌驾
void initState(){
super.initState();
//加载最大持续时间(如果没有),从0开始
_maxDuration=持续时间(秒:SharedReferences.getInt(“maxDuration”)?0);
final lastPressString=SharedReferences.getString(“lastButtonPress”);
_lastButtonPress=lastPressString!=null?DateTime.parse(lastPressString):DateTime.now();
_updateTimer();
_ticker=Timer.periodic(持续时间(秒:1),()=>updateTimer());
}
@凌驾
无效处置(){
_ticker.cancel();
super.dispose();
}
void _updateTimer(){
最终持续时间=DateTime.now().difference(_lastbutonpress);
//在此处检查新的最大持续时间
Duration newMaxDuration=\u maxDuration;
如果(持续时间>\u最大持续时间){
//当前持续时间为新的最大值时保存
newMaxDuration=持续时间;
setInt(“maxDuration”,newMaxDuration.unseconds);
}
最终newDuration=_formatDuration(duration);
设置状态(){
_maxDuration=newMaxDuration;
_按持续时间=新建持续时间;
});
}
字符串\u formatDuration(持续时间){
字符串两位数(整数n){
如果(n>=10)返回“$n”;
返回“0$n”;
}
字符串twoDigitMinutes=twoDigits(持续时间.inMinutes.余数(60));
字符串twoDigitSeconds=twoDigits(duration.unseconds.rements(60));
返回“${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds”;
}
}
不完全是我搜索的内容,使用此代码,它会在触发最大持续时间时停止计数器。我想让当前计时器工作,但如果我再次按下该按钮,将显示一个具有最大持续时间的新变量。目标是在所有持续时间记录之间始终显示最大持续时间。对不起,如果我不清楚:)对不起,你的意图错了。我用一个新的编辑了我的答案example@ZeRj您知道如何在共享首选项中设置过期时间而不是使用持续时间吗?。。例如共享首选项中的数据将在11:00过期:00@uyhaW使用date.toIso8601String()函数并将字符串存储在共享首选项中。使用DateTime.parse()从字符串中恢复日期。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutterfly/SharedPrefs.dart';
import 'package:flutterfly/SharedPrefs.dart' as prefix0;
class TestWidget extends StatefulWidget {
@override
_TestWidgetState createState() => _TestWidgetState();
}
class _TestWidgetState extends State<TestWidget> {
DateTime _lastButtonPress;
String _pressDuration;
Timer _ticker;
Duration _maxDuration;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Time since button pressed"),
Text(_pressDuration),
Text("Maximal Duration"),
Text(_formatDuration(_maxDuration)),
RaisedButton(
child: Text("Press me"),
onPressed: () {
_lastButtonPress = DateTime.now();
_updateTimer();
sharedPreferences.setString("lastButtonPress",_lastButtonPress.toIso8601String());
},
)
],
),
);
}
@override
void initState() {
super.initState();
//load max duration, if there is none start with 0
_maxDuration = Duration(seconds:sharedPreferences.getInt("maxDuration")??0);
final lastPressString = sharedPreferences.getString("lastButtonPress");
_lastButtonPress = lastPressString!=null ? DateTime.parse(lastPressString) : DateTime.now();
_updateTimer();
_ticker = Timer.periodic(Duration(seconds:1),(_)=>_updateTimer());
}
@override
void dispose() {
_ticker.cancel();
super.dispose();
}
void _updateTimer() {
final duration = DateTime.now().difference(_lastButtonPress);
//check for new max duration here
Duration newMaxDuration = _maxDuration;
if(duration> _maxDuration) {
//save when current duration is a new max
newMaxDuration = duration;
sharedPreferences.setInt("maxDuration",newMaxDuration.inSeconds);
}
final newDuration =_formatDuration(duration);
setState(() {
_maxDuration = newMaxDuration;
_pressDuration = newDuration;
});
}
String _formatDuration(Duration duration) {
String twoDigits(int n) {
if (n >= 10) return "$n";
return "0$n";
}
String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
return "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
}
}