Don't wanna be here? Send us removal request.
Link
At some point in your app development journey, you may encounter the need to encode or decode data. A typical use case would be when trying to display an image stored in Base64 format; we will focus on Decode Base64 to Image in Flutter in this article.
Flutter is a widely popular mobile application development framework that makes creating beautiful apps for both Android and iOS using one codebase easier than ever before. Thanks to its extensive library of widgets, tools, and libraries you can quickly build beautiful and responsive apps in no time at all - though some tasks such as decoding Base64 data to an image may prove challenging - let’s examine some effective practices for decoding Base64 to an image in Flutter.
Before diving into implementation, let us first gain an understanding of Base64 encoding and why we use it.
What Is Base64 Encoding?
Base64 encoding is a way of translating binary data into ASCII text format that is easily transmitted across the internet. It uses 64 characters - A-Z, a-z, 0-9, +, / and = for padding characters - to represent binary data as printable characters in an easy way. Base64 encoded files can often be found embedded into web pages and mobile applications as images, videos or other binary content.
Now that we understand Base64 encoding, let’s learn to decode it using Flutter.
Decoding Base64 to Image in Flutter
Flutter makes decoding Base64 encoded strings to images an effortless process, using dart:convert library to decode them to bytes before calling Image.memory() constructor to build an image widget from these bytes. Here is a step-by-step guide on decoding Base64 into image in Flutter:
Step 1: Import the dart:convert library
For our base64 decoder to work, we must first import the dart:convert library into our Flutter project by including this line at the beginning of your dart file:
import 'dart:convert';
Step 2: Decode the Base64 String
Step two is decoding the Base64 string to bytes using the base64.decode() function from dart:convert library. This function accepts a Base64 encoded string as input and returns a list of bytes representing decoded data.
List<int> imageBytes = base64.decode(base64String);
Here, base64String represents the Base64 encoded string that we want to decode into an image file.
Step 3: Create an Image Widget
Once we have decoded bytes, we can use the Image.memory() constructor to build an image widget from them. This method takes in a List that represents all of our decoded bytes and returns an Image widget as output.
Image.memory(imageBytes);
Step 4: Display an Image
Its To complete our Flutter application, we must now add the image widget to the widget tree, just as any other widget.
Container( child: Image.memory(imageBytes), );
No worries here - with these simple steps we can easily convert Base64 encoded strings to images using Flutter.
Full Example code
import 'dart:convert'; import 'package:flutter/material.dart'; class Base64Image extends StatelessWidget { final String base64String; Base64Image(this.base64String); @override Widget build(BuildContext context) { return Image.memory( base64Decode(base64String), fit: BoxFit.cover, ); } }
Best Practices for Decoding Base64 to Image
When decoding Base64 data to an image with Flutter, there are some best practices you should keep in mind:
1. Always Implement Try-Catch Blocks
Decoding Base64 data carries with it the risk that its information might become invalid or corrupted and thus cause your app to crash. To prevent this from occurring, always implement try-catch blocks when decoding Base64 data.
try { var bytes = base64Decode(base64String); // Do something with the decoded bytes } catch (error) { // Handle the error }
As shown above, we use a try-catch block to detect any errors during decoding of Base64 data.
2. Optimize Memory Usage
Decoding Base64 data can be memory intensive. To reduce wasteful memory usage and optimize performance, decode chunks of Base64 data instead of decoding everything at once.
As shown above, we use this code snippet to decode Base64 data in chunks of 1024 characters, adding decoded bytes into a list. This helps reduce memory usage in your app.
Conclusion
In this article, we explored how to decode Base64 to image using Flutter. We provided an introduction into Base64 encoding and its usage before providing a step-by-step guide on decoding Base64 encoded strings to images using Flutter. By employing these techniques in your applications and creating beautiful yet functional user interfaces using images as content, this process should help make working with images simpler than ever!
via Learn Pain Less
0 notes
Link
Flutter provides the setState() method as a method to change the state of any given widget, but sometimes you may wish to update just a specific widget and not rebuild the whole tree. That is where StatefulBuilder comes into play - with it, you can update just one specific widget without rebuilding its entire tree!
In this article, we will cover how to refresh a specific widget using setState and StatefulBuilder in Flutter.
What Is StatefulBuilder?
StatefulBuilder is a Flutter widget that allows you to create widgets with their own mutable state, similar to StatefulWidget but differing in that its setState() method allows only its widget to update.
StatefulBuilder makes using StatefulBuilders easier. Simply wrap your widget with it, and provide a callback function that will be called when its state needs updating. The callback function takes two arguments; they are: BuildContext and StateSetter - these represent the context in which your widget resides wrapped by StatefulBuilders; while StateSetter acts as a way of updating its state directly.
StatefulBuilder to Refresh Widget
If you want to refresh a specific widget, wrap its instance with StatefulBuilder and call its StateSetter function for update of state. Here is an example:
StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return Column( children: [ Text('Counter: $counter'), RaisedButton( child: Text('Increment'), onPressed: () { setState(() { counter++; }); }, ), ], ); }, )
In this example, we use a Column widget which houses a Text widget displaying a counter value and an IncrementButton that increments it upon press. All three components are wrapped by StatefulBuilder for ease of use.
When the RaisedButton is pressed, its onPressed callback function will be triggered and we use its setState() method of the StateSetter to change its counter value accordingly. This causes both Text and Column widgets to redraw themselves with their new counter values but not Text widget.
Conclusion
Flutter’s StatefulBuilder widget offers you the flexibility of updating a specific widget’s state without rebuilding its entire tree, which can come in handy when updating only part of your UI and not all at once.
We hope this article has been beneficial in explaining how to refresh a widget using setState and StatefulBuilder in Flutter. If you have any queries or comments below, feel free to submit them below.
via Learn Pain Less
0 notes
Text
Send multiple files to server using retrofit 1.9
If you want to send multiple files,Images, Text etc then you need to send that whole data as MultipartTypedOutput. This is same as Multipart Entity in HTTP Client & Post.
So lets start :
this is my server url where i m going to upload files : xml http://192.168.1.106/learnpainless/android/file_upload/fileUpload.php and these are parameters : image,email,website
To send files to server we need to use POST method instead of GET method. I assume that you must know about POST and GET methods and you also know about how to use Retrofit API. and then we will attach “MultipartTypedOutput” to Body of our response to send over POST method.
0 notes
Text
How to create TextArea in Android
Hi Android coders!!!!!
So if you want to add TextArea in your Android App then you will notice that there is no TextArea available for Android. but TextArea is available for all other platforms like html,iOS,C# etc. But in android there is only EditText widget which is editable.
So what to do now?
But there is still an option available to make TextArea widget in android app. you can modify EditText so it can behave like TextArea. first of all figure out difference between EditText and TextArea so that we can easily modify EditText
TextArea is multiline
TextArea is scrollable from inside, so we can view all text inside textarea.
Text in TextArea start from Top-Left corner.
TextArea is box type.
TextArea can be scroll from top to bottom and right to left.
So we can add these features to normal EditText widget of android.
0 notes
Text
Android 6.0 Runtime Permission example (Real Example)
As you know that in API 23 (Marshmallows) new Runtime Permission is added to android development. This feature make App installation more faster than before, because on Android Marshmallows above device your app will not ask for Permission at installation time. Permissions will be granted dynamically to your app when you actually need it. That’s a cool Idea. But if you have existing App and you try to install that app on Android Marshmallows device then your app will install without Permissions as expected but if you try to access Camera, Storage then your app will crash saying “No Permissions etc. Exception”. Because you haven’t yet defined runtime Permissions in your app. So you need to modify your app to ask for Permissions when required.
But maybe you tried adding some codes to make runtime Permission work on your app but you still got crash, then here is simple tutorial to embed runtime Permission in your app.
0 notes
Text
Create custom ScrollView to enable or disable scrolling Android
In android ScrollView there is no way or method to disable scrolling. but sometime if you want to disable scrolling of ScrollView while you are inside inner element, like if you want to disable ScrollView when you are inside Map view. So you can scroll Map view easily if you put Map view inside ScrollView. Because Map view has its own Scrolling feature which conflicts ScrollView’s scrolling and you are unable to scrolling map view. So at that time you think to disable Scrolling of ScrollView when you touch or make focus on Map view. But when you try to implement this then you get to know that there is no method to disable scrolling of ScrollView. You can disable the whole ScrollView but that is not a correct way to do this. So we can make our own ScrollView which having extra features are enable Scrolling or Disable scrolling. and we can do this using Inheritancefeature of OOPS. So we will create a class which will Extend ScrollView class, which means our class will have all features derive from Base (ScrollView) class. ok so lets do this :
create a new class and name it what you want, i will name it as MyScrollView. and extend ScrollView class.
Now you need to create constructor of your class, Android studio will give you 4 type of Constructor. extend all of them. because in future we can use that constructors.
override 2 methods of ScrollView “onInterceptTouchEvent” and “onTouchEvent”.
create a boolean variable which will check that scrolling is enabled or disabled. by default set its value totrue.
now create boolean type method and return that variable which you have just created.
Now create a void method to enable and disable scrolling which requires a parameter of boolean type. i will name this method as setScrolling(boolean enable)
0 notes
Text
Scroll to specific view inside ScrollView Android
While building apps with too many controls or views on same page then we need to use ScrollView so we can view each and every view inside an activity.But sometimes we want to focus on specific view on button click over any other action to get attention from user. for example if you have 20 EditTexts on a page and 1 button at bottom and you had applied a validation on button click to check if EditText is not empty. but if EditText is not empty then that empty EditText will focus automatically. and in this tutorial we will make same.
But in this tutorial i will show you how you can focus on any view like TextView,ImageView, button etc. code will be as below :
suppose i have 20 ImageView on a page. and i want to make focus on 5th ImageView (and id of 5th imageview is img5) on button click. then i will wrote like this :
private void focusOnView(){ new Handler().post(new Runnable() { @Override public void run() { your_scrollView.scrollTo(0, your_view.getBottom()); } }); }
0 notes