Skip to content

Instantly share code, notes, and snippets.

@hilfritz
Last active April 6, 2022 06:57
Show Gist options
  • Save hilfritz/b302af283db4f2b65513844ba7991073 to your computer and use it in GitHub Desktop.
Save hilfritz/b302af283db4f2b65513844ba7991073 to your computer and use it in GitHub Desktop.
Android: Flutter
Loading image with updates
- takes more process/memory than regular image loading in flutter
- advantage: sure that the image is updated properly
1. load image from /data/picture1.jpg (1001 bytes)
2. replace image but retain same name /data/picture1.png (850 bytes)
* normal loading will not update the image, using the below code the change in image gets reflected
ClipRRect(
borderRadius: BorderRadius.circular(16.0),
child: Image.memory(
Uint8List.fromList(
File(imagePath).readAsBytesSync()),
width: <<imageWidth>>,
height: <<imageHeight>>,
fit: BoxFit.fitWidth,
)
),
Downloading images loaded from network
https://stackoverflow.com/questions/49455079/flutter-save-a-network-image-to-local-directory
Dismiss keyboard on scroll widget
https://github.com/flutter/flutter/issues/36869
class DismissKeyboardOnScroll extends StatelessWidget {
final Widget child;
final Function onDismiss;
const DismissKeyboardOnScroll({Key key, this.child, this.onDismiss})
: super(key: key);
@override
Widget build(BuildContext context) {
return NotificationListener<ScrollStartNotification>(
onNotification: (x) {
if (x.dragDetails == null) {
return;
}
FocusScope.of(context).unfocus();
if (onDismiss != null) {
onDismiss();
}
},
child: child,
);
}
}
Usage:
DismissKeyboardOnScroll(
child: listView, //any view that scrolls
)
RX Subjects
PublishSubject<String> _hideKeyboardStream;
void initializeHideKeyboardStream(){
print("initializeHideKeyboardStream:");
if (_hideKeyboardStream==null) {
_hideKeyboardStream = PublishSubject<String>();
_hideKeyboardStream.debounce(const Duration(milliseconds: 500))
.listen((x) {
print("initializeHideKeyboardStream: hideKeyboard");
FocusScope.of(context).requestFocus(FocusNode());
}, onError: (e, s) {
print("initializeHideKeyboardStream: error");
}, onDone: () {
print("initializeHideKeyboardStream: ondone");
});
}
}
USAGE: Trigger the emit of observables
void hideKeyboard() {
_hideKeyboardStream.add("");
}
LISTEN TO PAGE VISIBILITY USING RX
- funcitonality mimics the approach is similar to onResume() in android
- it gets triggered everytime the page goes to foreground
class _LogPageState extends State<LogPage> {
PublishSubject<bool> isCurrentStream;
@override
void initState() {
isCurrentStream = PublishSubject<bool>();
isCurrentStream.debounce(Duration(milliseconds: 500))
.distinct()
.listen((x){
if (x){
//HERE IS THE onResume() equivalent METHOD
//_presenter.run();
}
},
onError: (x,y){}, onDone: (){});
isCurrentStream.add(true); //CALL IT THe FIRST TIME
super.initState();
}
@override
Widget build(BuildContext context) {
isCurrentStream.add(ModalRoute.of(context).isCurrent);
}
@override
void dispose() {
isCurrentStream?.close();
super.dispose();
}
}
FOREACH LOOP WITH INDEX
List _sample = ['a','b','c'];
_sample.asMap().forEach((index, value) => f);
OR
convertedList.asMap().forEach((index, value) { });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment