internet listen on every screen flutter - 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();
}

Related

How to add continuously change color of container in flutter between RGB values

I want to create an animation where the container changes colors just the LED lights do in real life.
I this would be automatic so I dont want any 'onPressed()' working classes
I tried doing it through 'Timer' and 'AnimatedContainer()' but the timer only responds to initState value i.e. 0
Either help me setState the state of Container after every second or give me some another solution.
This is my code currently
late Timer timer;
int start = 0;
void startTimer() {
const oneSec = Duration(seconds: 1);
timer = Timer.periodic(
oneSec,
(Timer timer) {
setState(() {
start++;
});`your text`
},
);
}
#override
void initState() {
super.initState();
startTimer();
setState(() {
_changeColor();
});
}
_changeColor() {
setState(() {
if (start % 3 == 0) {
changingColor = Colors.green;
} else if (start % 3 == 1) {
changingColor = Colors.blue;
} else if (start % 3 == 2) {
changingColor = Colors.red;
}
});
}
Use setState for start won't call _changeColor, so you have to call _changeColor inside Timer.
class LEDWidget extends StatefulWidget {
const LEDWidget({super.key});
#override
State<LEDWidget> createState() => _LEDWidgetState();
}
class _LEDWidgetState extends State<LEDWidget> {
Color changingColor = Colors.green;
int start = 0;
late Timer timer;
#override
void dispose() {
timer.cancel();
super.dispose();
}
#override
void initState() {
super.initState();
timer = Timer.periodic(const Duration(seconds: 1), (_) {
start++;
_changeColor();
});
}
_changeColor() {
setState(() {
if (start % 3 == 0) {
changingColor = Colors.green;
} else if (start % 3 == 1) {
changingColor = Colors.blue;
} else if (start % 3 == 2) {
changingColor = Colors.red;
}
});
}
#override
Widget build(BuildContext context) {
return AnimatedContainer(
duration: const Duration(seconds: 1),
color: changingColor,
);
}
}

Flutter- App content disappear when NotificationListener sends notification for first time

I am creating an app where i am using NotificationListener<UserScrollNotification> to change the boolean value on the basis of scroll direction... but when i scroll page for very first time all the content gets disappear for 2-3 seconds...I am using Provider.... Please help me...
Here i am trying to change boolean variable
NotificationListener<UserScrollNotification>(
onNotification: (notification) {
final ScrollDirection direction =
notification.direction;
if (direction == ScrollDirection.reverse) {
value.changeVisiblity(true);
} else if (direction ==
ScrollDirection.forward) {
value.changeVisiblity(false);
}
return false;
},
here is my provider class where boolean is defined
bool isVisible = false;
changeVisiblity(bool value) {
isVisible = value;
notifyListeners();
}
here is a link to 14 seconds short video
enter link description here

Flutter NotificationListener crashed app for some seconds

I am using NotificationListener<UserScrollNotification> to change the boolean value on the basis of scroll direction... but when i scroll page for very first time when app starts all the content gets disappear for some seconds...I am using Provider,Please help me... It only happens for the very first time
Here i am trying to change boolean variable
NotificationListener<UserScrollNotification>(
onNotification: (notification) {
final ScrollDirection direction =
notification.direction;
if (direction == ScrollDirection.reverse) {
value.changeVisiblity(true);
} else if (direction ==
ScrollDirection.forward) {
value.changeVisiblity(false);
}
return false;
},
here is my provider class where boolean is defined
bool isVisible = false;
changeVisiblity(bool value) {
isVisible = value;
notifyListeners();
}
here is a video
Video

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){
...
}

How to notice when user released his/her finger from scrolling

I have a listView.Builder and want to do certain calculation based on position of scrollController when user released his/her finger on the screen?
the calculation part is easy in flutter but How can I notice when user released his finger from scrolling to do some action ?
This worked for me:
class _YourScreenState extends State<YourScreen> {
bool _isDragging = false;
ScrollUpdateNotification _notification;
#override
Widget build(BuildContext context) {
return NotificationListener<ScrollUpdateNotification>(
onNotification: (notification) {
_notification = notification;
if (_hasLiftedFinger) {
// do your thing
}
_setDraggingState();
// confidently use _isDragging here
return false;
},
child: YourListView(),
);
}
void _setDraggingState() {
if (_hasLoweredFinger) {
_isDragging = true;
return;
}
if (_hasLiftedFinger) {
_isDragging = false;
}
}
bool get _hasLoweredFinger => (!_isDragging && _notification.dragDetails != null);
bool get _hasLiftedFinger => (_isDragging && _notification.dragDetails == null);
}
Use NotificationListener Widget. Here is a short clip about it.
The code you may want would look like this:
#override
Widget build(BuildContext context) {
return NotificationListener<ScrollNotification>(
onNotification: (notification) {
if (notification is ScrollStartNotification) {
debugPrint('Started');
}
if (notification is ScrollUpdateNotification) {
debugPrint('Updated');
}
if (notification is ScrollEndNotification) {
debugPrint('Ended');
}
return false;
},
child: YourListView(),
);
}