in my flutter app I wanted to control will pop scope for IOS but it's not working. and I'm not using PageRoute as some examples suggested and CupertinoWillPopScope package is not working, it throws an error of anchorpoint
. so can anyone help me out?
I tried gestureDetector
for IOS but it's not triggering an action to previous page, which is what i wanted to do.
if (!Platform.isIOS) {
return WillPopScope(
onWillPop: () async {
timerController.startTimer();
!Navigator.of(context).userGestureInProgress;
return true;
},
child: MyPage(),
);
} else {
return GestureDetector(
onHorizontalDragUpdate: (details) {
int sensitivity = 8;
if (details.delta.dx > sensitivity) {
// Right Swipe
//timerController.startTimer();
} else if (details.delta.dx < -sensitivity) {
//Left Swipe
timerController.startTimer();
}
},
child: MyPage(),
);
}
CodePudding user response:
Let's assume you want to go from Page A(NewPage) to Page B(SecondRoute). To navigate to B from A, we use the following code:
Navigator.push(context, MaterialPageRoute(builder: (context) => const SecondRoute()))
And later, when the user comes back using swipe gesture or any other way to Page A and if you want to perform some action when user comes from B to A, we can call the 'then' in our Navigator.push function. Go through the below code to see text changes after user comes back to Page A from Page B.
import 'package:flutter/material.dart';
class NewPage extends StatefulWidget {
NewPage({Key? key}) : super(key: key);
@override
State<NewPage> createState() => _NewPageState();
}
class _NewPageState extends State<NewPage> {
String inputstring = 'Here is a default String';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(inputstring),
ElevatedButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const SecondRoute()))
.then((value) => {
setState(() {
inputstring = 'New Text after navigation is popped';
})
});
},
child: Text('GO To Second Page'),
),
],
),
),
),
);
}
}
class SecondRoute extends StatelessWidget {
const SecondRoute({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
);
}
}
Inside 'then' you can call any function you want to. Hopefully this serves what you are looking for.