Skip to content

Commit

Permalink
Fixed Looking up a deactivated widget's ancestor
Browse files Browse the repository at this point in the history
=> rrousselGit/provider#679 shows how to do if
you still want listeners
  • Loading branch information
josephnglynn committed Mar 9, 2024
1 parent 8ca80ce commit 0dd37c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 65 deletions.
25 changes: 7 additions & 18 deletions lib/start_plan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class _StartPlanPageState extends State<StartPlanPage> {
late List<String> planExercises;

int selectedIndex = 0;
bool timerRunning = false;
final repsNode = FocusNode();
final weightNode = FocusNode();

Expand All @@ -40,28 +39,17 @@ class _StartPlanPageState extends State<StartPlanPage> {
weightController = TextEditingController();
planExercises = widget.plan.exercises.split(',');
getLast();
Provider.of<AppState>(context, listen: false).addListener(updateRunning);
}

@override
void dispose() {
Provider.of<AppState>(context, listen: false).removeListener(updateRunning);
repsController.dispose();
weightController.dispose();
repsNode.dispose();
weightNode.dispose();
super.dispose();
}

void updateRunning() {
final appState = Provider.of<AppState>(context, listen: false);
setState(() {
if (appState.secondsLeft == null)
timerRunning = false;
else
timerRunning = appState.secondsLeft! > 0;
});
}

void selectWeight() {
weightController.selection = TextSelection(
Expand Down Expand Up @@ -152,15 +140,19 @@ class _StartPlanPageState extends State<StartPlanPage> {
if (!mounted) return;
final appState = Provider.of<AppState>(context, listen: false);
appState.updateSeconds(210000, 210000);
setState(() {
timerRunning = true;
});
}

bool isTimerRunning(BuildContext context) {
final secondsLeft = context.watch<AppState>().secondsLeft;
if (secondsLeft == null) return false;
return secondsLeft > 0;
}

@override
Widget build(BuildContext context) {
var title = widget.plan.days.replaceAll(",", ", ");
title = title[0].toUpperCase() + title.substring(1).toLowerCase();
final timerRunning = isTimerRunning(context);

return Scaffold(
appBar: AppBar(
Expand Down Expand Up @@ -245,9 +237,6 @@ class _StartPlanPageState extends State<StartPlanPage> {
? FloatingActionButton(
onPressed: () {
android.invokeMethod('stop');
setState(() {
timerRunning = false;
});
},
child: const Icon(Icons.stop),
)
Expand Down
61 changes: 14 additions & 47 deletions lib/timer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,19 @@ class TimerPage extends StatefulWidget {
}

class _TimerPageState extends State<TimerPage> {
int duration = 0;
int elapsed = 0;

@override
void initState() {
super.initState();
android.invokeMethod('getProgress').then((args) => setState(() {
int secondsLeft = args[0];
int secondsTotal = args[1];
elapsed = secondsTotal - secondsLeft;
duration = secondsTotal;
}));
Provider.of<AppState>(context, listen: false).addListener(updateTime);
}

@override
void dispose() {
super.dispose();
Provider.of<AppState>(context, listen: false).removeListener(updateTime);
String generateTitleText(int duration, int elapsed) {
final minutes = ((duration - elapsed) ~/ 60).toString().padLeft(2, '0');
final seconds = ((duration - elapsed) % 60).toString().padLeft(2, '0');
return "$minutes:$seconds";
}

void updateTime() {
final appState = Provider.of<AppState>(context, listen: false);
setState(() {
int secondsLeft = appState.secondsLeft ?? 0;
int secondsTotal = appState.secondsTotal ?? 0;
elapsed = secondsTotal - secondsLeft;
duration = secondsTotal;
});
}

get minutes => ((duration - elapsed) ~/ 60).toString().padLeft(2, '0');
get seconds => ((duration - elapsed) % 60).toString().padLeft(2, '0');

@override
Widget build(BuildContext context) {
final appState = context.watch<AppState>();
final duration = appState.secondsTotal ?? 0;
final elapsed = (duration) - (appState.secondsLeft ?? 0);

return Scaffold(
appBar: AppBar(
title: const Text('Timer'),
Expand All @@ -55,26 +32,22 @@ class _TimerPageState extends State<TimerPage> {
child: Stack(
alignment: Alignment.center,
children: <Widget>[
progressWidget(context),
textWidget(context),
progressWidget(context, duration, elapsed),
textWidget(context, duration, elapsed),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
const platform = MethodChannel('com.presley.flexify/android');
platform.invokeMethod('stop');
setState(() {
duration = 0;
elapsed = 0;
});
},
child: const Icon(Icons.stop),
),
);
}

SizedBox progressWidget(BuildContext context) {
SizedBox progressWidget(BuildContext context, int duration, int elapsed) {
return SizedBox(
height: 300,
width: 300,
Expand All @@ -90,13 +63,13 @@ class _TimerPageState extends State<TimerPage> {
);
}

Column textWidget(BuildContext context) {
Column textWidget(BuildContext context, int duration, int elapsed) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(height: 32.0),
Text(
"$minutes:$seconds",
generateTitleText(duration, elapsed),
style: TextStyle(
fontSize: 50.0,
color: Theme.of(context).textTheme.bodyLarge!.color,
Expand All @@ -106,13 +79,7 @@ class _TimerPageState extends State<TimerPage> {
TextButton(
onPressed: () {
android.invokeMethod('add');
setState(() {
if (duration == 0)
duration += 61; // Immediately show progress at first.
else
duration += 60;
if (elapsed == 0 || elapsed > duration) elapsed = 1;
});
/* TODO: Make update show immediately */
},
child: const Text('+1 min'),
),
Expand Down

0 comments on commit 0dd37c1

Please sign in to comment.