现在我正在尝试重建代码。
现在的问题是动画控制器位于StatefulWidget中,因此当我转到另一页时,StatefulWidget消失了,动画控制器也无法正常工作。.
所以我想在changeNotifier类中处理动画Controller状态。
有什么解决办法吗?
----------------------------有状态类----------------- -----------
class _PomodoroState extends State<Pomodoro> with TickerProviderStateMixin {
SharedPreferences prefs;
SettingDataHandler settingDataHandler;
PomodoroHandler pomodoroHandler;
AnimationController animationController;
@override
void initState() {
super.initState();
animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 450));
}
@override
void dispose() {
super.dispose();
animationController.dispose();
}
Future<int> _initPref() async {
prefs = await SharedPreferences.getInstance();
var timeData = prefs.get('timeData');
if (timeData != null) {
settingDataHandler.selectedTimes["Pomodoro Setting"] = timeData;
}
pomodoroHandler.pomodoroTime = settingDataHandler.selectedTimes["Pomodoro Setting"];
pomodoroHandler.time = pomodoroHandler.pomodoroTime * 60;
return 0;
}
@override
Widget build(BuildContext context) {
screenWidth = MediaQuery.of(context).size.width;
settingDataHandler = Provider.of<SettingDataHandler>(context);
pomodoroHandler = Provider.of<PomodoroHandler>(context);
return Scaffold(
body: Container(
decoration: BoxDecoration(
color: Colors.black54,
),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
_ClockView(),
bottomBar()
],
)
)
);
}
Widget bottomBar(){
return Container(
width: screenWidth,
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 40),
child: Text(pomodoroHandler.formatTime(pomodoroHandler.elapsedTime, pomodoroHandler.time), style: TextStyle(color: Colors.black54, fontSize: 30, fontWeight: FontWeight.bold),)
),
_ClockButtons()
],
),
);
}
Widget _ClockView() {
return FutureBuilder(
future: _initPref(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData == false) {
return CircularProgressIndicator();
} else if (snapshot.hasError) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Error: ${snapshot.error}',
style: TextStyle(fontSize: 15),
)
);
}
double percent = pomodoroHandler.elapsedTime / pomodoroHandler.time;
return Padding(
padding: EdgeInsets.only(top: 100),
child: CircularPercentIndicator(
circularStrokeCap: CircularStrokeCap.round,
percent: percent,
animation: true,
animateFromLastPercent: true,
radius: 300.0,
lineWidth: 5.0,
progressColor: Colors.deepOrange,
center: Text(
pomodoroHandler.formatTime(pomodoroHandler.elapsedTime, pomodoroHandler.time),
style: TextStyle(
color: Colors.white, fontSize: 50.0, fontWeight: FontWeight.w300),
)
),
);
},
);
}
Widget _ClockButtons() {
return Row(
children: <Widget>[StopButton(), PalyAndPauseButton()],
);
}
Widget StopButton() {
return IconButton(
iconSize: 50,
splashColor: Colors.white30,
color: Colors.black54,
icon: Icon(Icons.stop),
onPressed: () => {
pomodoroHandler.ResetTimer(),
animationController.reverse(),
pomodoroHandler.timer.cancel(),
bottomSheet(),
pomodoroHandler.endTime = formatter.format(new DateTime.now())
},
);
}
Widget PalyAndPauseButton() {
return IconButton(
iconSize: 50,
splashColor: Colors.white30,
icon: AnimatedIcon(
icon: AnimatedIcons.play_pause,
progress: animationController,
color: Colors.black54),
onPressed: () => {
setState((){
pomodoroHandler.isPlaying = !pomodoroHandler.isPlaying;
pomodoroHandler.isPlaying ? animationController.forward() : animationController.reverse();
}),
HandleOnPressed()
},
);
}
HandleOnPressed() {
if (pomodoroHandler.isPlaying) {
pomodoroHandler.StartTimer(formatted, WhenTimerIsEnd);
} else {
pomodoroHandler.timer.cancel();
}
}
---------------------------- ChangeNotifier类----------------- -----------
class PomodoroHandler with ChangeNotifier {
Timer timer;
DateTime start;
String startTime;
String endTime;
int count = 0;
int elapsedTime = 0;
bool isPlaying = false;
int pomodoroTime;
int time;
Pomodoro pomodoro;
TextEditingController eventController;
ResetTimer() {
time = pomodoroTime * 60;
elapsedTime = 0;
notifyListeners();
}
formatTime(int now, int duration) {
String first = ((duration - now) ~/ 60).toString();
int seconds = ((duration - now) % 60);
String latter = (seconds < 10 ? "0" : "") + seconds.toString();
return first + ":" + latter;
}
StartTimer(String formatted, dynamic whenTimerIsEnd()) {
startTime = formatted;
start = new DateTime.now();
start = elapsedTime > 0
? start.subtract(Duration(seconds: elapsedTime))
: start;
timer = Timer.periodic(Duration(seconds: 1), (timer) {
if (time > elapsedTime) {
elapsedTime = DateTime.now().difference(start).inSeconds;
print(isPlaying);
} else {
whenTimerIsEnd();
}
notifyListeners();
});
}
StopTimer(){
ResetTimer();
timer.cancel();
}
}