Flutter QR Camera is not working when page is changed - flutter

i have a screen that scans qr code and sends some informations to my db, when i navigate to camera page first time camera works perfectly but when i navigate to another page then back to camera page again camera is not working
if i save codes, camera works again. i think its about state but i cannot manage to solve this problem.
void initState() {
super.initState();
_animationController =
AnimationController(vsync: this, duration: const Duration(seconds: 1));
_animation = Tween<double>(begin: 0, end: 1).animate(CurvedAnimation(
parent: _animationController, curve: Curves.easeInOutCirc));
_animationController.forward();
}
void dispose() {
try {
qrViewController?.dispose();
} catch (e) {
print("error");
}
super.dispose();
}
Future<void> reassemble() async {
// TODO: implement reassemble
super.reassemble();
if (Platform.isAndroid) {
await qrViewController!.pauseCamera();
}
qrViewController!.resumeCamera();
}
void onQrViewCreated(QRViewController qrViewController) {
setState(() {
this.qrViewController = qrViewController;
});
qrViewController.scannedDataStream.listen((event) async {}
}

I changed my qr_code_scanner package with same publisher's mobile_scanner and this is working well

Related

Parallel Timer animation in flutter

I made an automatic scrolling bar in flutter, the way I made it scroll automatically by using a combination of a timer and scroll controller, and then I linked the scroll controller to a regular ListView.builder, blew is the implementation of my way to do it
#override
void initState() {
super.initState();
int _length = widget.items.length;
currentItems = widget.items;
// scroll smothly to the end of the list then reverse the scroll
bool isAscending = true;
_timer = Timer.periodic(const Duration(milliseconds: 3000), (timer) {
if (isAscending) {
_currentIndex++;
if (_currentIndex == currentItems.length) {
isAscending = !isAscending;
}
}
if (!isAscending) {
_currentIndex--;
if (_currentIndex == 0) {
isAscending = !isAscending;
}
}
_scrollController.animateTo(_currentIndex * 304.0,
duration: const Duration(milliseconds: 3000), curve: Curves.linear);
});
}
my problem here is when I navigate to another screen or press the phone's home button and stay away from this widget for a while and then come back, I'd find it scrolling so fast to catch the currentIndex ( which is far away from where I left it ) in a period of time that I specified for the .animateTo method, I'm guessing the solution is to somehow pause the timer when I'm not in the same route, but I didn't find any reference of how to implement that

internet listen on every screen flutter

I want to check the internet on every page in my flutter app how I can do that without repeating the same code on every screen?
here is my code
bool internet = true;
#override
void initState() {
countryController.text = "+961";
Connectivity().onConnectivityChanged.listen((connectionState) {
if (connectionState == ConnectivityResult.none) {
setState(() {
internet = false;
});
// valueNotifierBook.incrementNotifier();
} else {
setState(() {
internet = true;
});
}
});
super.initState();
}

Timer.periodic flutter,

#override
void initState() {
super.initState();
bool isLastPage = (_currentPage.round() == sliderArrayList.length - 1 );
//Trying to build automatic scrollable
Timer.periodic(Duration(seconds: 5), (Timer timer) {
if (_pageController.hasClients && !isLastPage) {
_pageController.nextPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeIn);}
if(isLastPage){
timer.cancel();
}
});
}
I am trying to build a periodic timer which scrolls pages in pageview, flutter after every 5 seconds, however it keeps on scrolling even after the last page. I have tried to implement the above method if(isLastPage){timer.cancel();} and it doesnt work.
You've to evaluate that expression everytime timer ticks. Also, Put the isLastPage check before the hasClients check.
Timer.periodic(Duration(seconds: 5), (Timer timer) {
final isLastPage = _currentPage.round() == sliderArrayList.length - 1;
if (isLastPage) {
timer.cancel();
return;
}
if (_pageController.hasClients) {
_pageController.nextPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeIn);
}
});

Flutter Timer.periodic register/run once

i would like to have a Timer runing once in main.dart to check user activity
#override
void initState() {
super.initState();
initPlatformState();
_fetchMasterData("a", "b");
_startActivityTimer();
}
bool _activityTimerRunning = false;
void _startActivityTimer() {
if (!_activityTimerRunning) {
Timer.periodic(Duration(seconds: 5), (timer) {
_timerTicked(timer);
setState(() {
_activityTimerRunning = true;
});
print("Timer started");
});
}
}
But initState gets called more than once, so it is not the right place to register the timer. Where should it be placed?
The code in the question has i silly mistake, the print statement is in the wrong place. Here is the working code:
#override
void initState() {
super.initState();
initPlatformState();
_fetchMasterData("a", "b");
_startActivityTimer();
}
void _startActivityTimer() {
print("Timer started");
Timer.periodic(Duration(seconds: 5), (timer) {
_timerTicked(timer);
});
}
"TimerStarted" get executed only once
Try this sample code.
int start = 5;
fName(){
Timer _timer;
const oneSecond = Duration(seconds: 5);
_timer = Timer.periodic(
oneSecond,
(Timer timer) => setState((){
if(start == 0)
timer.cancel();
else{
_activityTimerRunning = true;
start -= 1;
}
})
);
}
When you want to start you timer, call fName() method, after that timer can started. So and also you can check condition, when
if(start != 0){
...
}

Carousel cannot Auto-Sliding in Flutter

I am trying to create carousel with auto-sliding. Before I got problem that "PageController.page cannot be accessed before a PageView is built with it"
I can solve it with reference from https://github.com/jlouage/flutter-carousel-pro/issues/21
But unfortunately the carousel cannot be auto-sliding. Please help me.
In carousel_pro.dart I have change like program in below :
final _controller = new PageController();
#override
void initState() {
super.initState();
if (_controller.hasClients) {
if (widget.autoplay) {
new Timer.periodic(widget.autoplayDuration, (_) {
if (_controller.page.round() == widget.images.length-1) {
_controller.animateToPage(
0,
duration: widget.animationDuration,
curve: widget.animationCurve,
);
} else {
_controller.nextPage(
duration: widget.animationDuration,
curve: widget.animationCurve);
}
});
}
}
}
You have to check whether the widget have images or not....
if (widget.images != null && widget.images.isNotEmpty) {
if (widget.autoplay) {
Timer.periodic(widget.autoplayDuration, (_) {
if (_controller.hasClients) {
if (_controller.page.round() == widget.images.length - 1) {
_controller.animateToPage(
0,
duration: widget.animationDuration,
curve: widget.animationCurve,
);
} else {
_controller.nextPage(
duration: widget.animationDuration,
curve: widget.animationCurve);
}
}
});
}
}
Also you have to reinstall the app.
It worked for me !!!