How to convert String to Date and Date To String in Flutter.


MorUde Frrr - (Fly Or Not)


Many developer has question like how to convert String to Date or Date to String in Flutter? I’m gonna explain you both.

First we need to add new package date_format in “pubspec.yaml” file. Below is link that will help you for more information.











x


After installing the “date_format” package you have to import package in you .dart file.


import 'package:date_format/date_format.dart';

1)Convert Date to String

void convertStringFromDate() {
  final todayDate = DateTime.now();
  print(formatDate(todayDate, [yyyy, '-', mm, '-', dd, ' ', hh, ':', nn, ':', ss, ' ', am]));
}
     
Call above function by convertStringFromDate() that will print date that you given format. Also check your self by changing “yyyy to yy/ mm to MM or m/ dd to d” etc.
2)Convert String to Date

void convertDateFromString(String strDate){
  DateTime todayDate = DateTime.parse(strDate);
  print(todayDate);
  print(formatDate(todayDate, [yyyy, '/', mm, '/', dd, ' ', hh, ':', nn, ':', ss, ' ', am]));
}
  

Call above function by convertDateFromString('2018-09-27 13:27:00') and output will be flutter: 2018/09/27 01:09:00 PM


Create Login form and validation in flutter




Before we start simple login form, let me tell you some basic scenario what types of main widgets and GlobalKey  we are using here 

  • Form widget :: Actually form widget is container controller or we can said it’s group for form fields. When we use form widget also we have to set GlobalKey for uniquely identify current form and you can access other property of the form.

  • TextFormField Widget  :: Form without input field? We can’t visualize. We are creating form because end-user can input some data so here we are using TextFormField for get input from the end-use. It’s material design text input and it has validate method that will help us for validate this text field.

  • RaisedButton Widget :: It’s simple material design button and has click method that we will help us to execute another action.

In our demo we will use GlobalKey for access Form and Scaffold Widgets and its property. For more information click on GlobalKey I already attached link on that.





Example:


import 'package:flutter/material.dart';

void main() => runApp(new FormValidation());

class FormValidation extends StatelessWidget {
 @override
   Widget build(BuildContext context) {
     return new MaterialApp(
       title: "Form validation",
       home: LoginScreen(),
     );
   }
}

class LoginScreen extends StatefulWidget {
@override
 State<StatefulWidget> createState() {
   // TODO: implement createState
   return LoginScreenState();
 }
}

class LoginScreenState extends State <LoginScreen> {
 final _formKey = GlobalKey<FormState>();
 final _scaffoldKey = GlobalKey<ScaffoldState>();

 String _email;
 String _password;

 @override
   Widget build(BuildContext context) {
     // TODO: implement build
     return new Scaffold(
       key: _scaffoldKey,
       appBar: new AppBar(
         title: new Text("Login")
       ),
       body: new Container(
         padding: const EdgeInsets.all(20.0),
         child: formSetup(context)
       ),
     );
   }

   Widget formSetup(BuildContext context){
     return new Form(
           key: _formKey,
           child: new Column(
             children: <Widget>[
               new TextFormField(
                 decoration: InputDecoration(
                   hintText: "aa@bb.com",
                   labelText: "Email"                   
                 ),
                 keyboardType: TextInputType.emailAddress,
                 validator: (val){
                     if (val.length == 0)
                       return "Please enter email";                     
                     else if (!val.contains("@"))
                       return "Please enter valid email";                     
                     else
                       return null; 
                   },
                   onSaved: (val)=>_email=val,
               ),
               new TextFormField(
                 decoration: InputDecoration(
                   hintText: "Password",
                   labelText: "Password"                   
                 ),      
                 obscureText: true,          
                 validator: (val){
                     if (val.length == 0)
                       return "Please enter password";                     
                     else if (val.length <= 5)
                       return "Your password should be more then 6 char long";                     
                     else
                       return null; 
                   },
                   onSaved: (val)=>_password=val,
               ),
               new Padding(
                 padding: const EdgeInsets.only(top: 20.0),
               ),
               new RaisedButton(
                 child: new Text("Submit"),
                 onPressed: (){
                   if (_formKey.currentState.validate()) {
                     _formKey.currentState.save();
                     _scaffoldKey.currentState.showSnackBar(new SnackBar(
                       content: new Text("Your email: $_email and Password: $_password"),
                     ));
                   }
                 },
                 color: Colors.blue,
                 highlightColor: Colors.blueGrey,                 
               )
             ],
           ),
         );
   }
}





Here in that above demo we are using StatefulWidget and below GlobalKeys


final _formKey = GlobalKey<FormState>();
final _scaffoldKey = GlobalKey<ScaffoldState>();

For access its state and property.

We have two TextFormFields for input Email and Password. And it has  InputDecoration for decorate text input. The main method is validator method of Text field that will help you for validate form.

At the last RaisedButton, 


new RaisedButton(
     child: new Text("Submit"),
         onPressed: (){
              if (_formKey.currentState.validate()) {
                  _formKey.currentState.save();
                  _scaffoldKey.currentState.showSnackBar(new SnackBar(
                       content: new Text("Your email: $_email and Password: $_password"),
                ));
               }
           },
      color: Colors.blue,
      highlightColor: Colors.blueGrey,                 
)


It has one method onPressed in that method, we are checking form validation by

if (_formKey.currentState.validate()) …..

We can access form current state by using formKey. validation() method return true then Just display toast(showSnackBar). If return false then display string error message that already putted in validation() method of TextForm Field

That’s It!!


Hope you guys this is enough for your understanding. If you have any query or question then comment it.




Repeats value of number of time in List or Array in Flutter?



As we know in many programming languages like swift and etc. if you want to take array or list that has same value in all the index then we can do by one line of code. Repeat same value for specific time.

Ex. (In Swift language)
If In the Array I want to repeat 0 for 5 times then I can do by one line of code like below.
let fiveZeros = Array(repeating: "0", count: 5)
print(fiveZeros)
// Prints "["0", "0", "0", "0", "0"]"

So I'm looking same for flutter. is there any specific built-in function that will help us to achieve same features?

Yes in the Flutter you can use "filled factory of List" That will help us to achieve same features.

Ex.

final fiveZeros = List.filled(5, 0);print(fiveZeros);  //-> [0, 0, 0, 0, 0]


If you have any query or question then please give your feedback as comment.

Different between StatelessWidget and StatefulWidget in the flutter


As we are beginner for Flutter and many of the developers are getting confused for StatelessWidget and StatefulWidget. Here we are going to clear you what is exact difference between StatelessWidget and StatefulWidget.


Same like another language Like Swift, Kotlin Etc. you may heard about mutable and immutable property. Same things we can apply here for those widget.
In the Swift we declare let keyword with static variable and var keyword with dynamic variable.
So what is mutable and immutable ?
  • Mutable means changeable, you can change property of mutable variable or function at run time. In other world we can say it’s value is dynamic.
  • Immutable mean properly of the variable or function will not be change after set it once. In other world we can say it’s value is Static.
For StatelessWidget - It’s static, This widget will not be change after run time.


StatefulWidget - It’s dynamic, This widget will change any time where you want.
So If any API will call and JSON data are coming and You want it to display on ListView then you must use with StatefulWidget. Any operation if you want to display on run time that will be done by StatefulWidget.
StatelessWidget - Work with static data. If you render any static list that will not be change any time that you can go with this widget.



Hope you guys this is enough for your understanding. If you have any query or question then comment it.

What is flutter and how can I install in my system?


What Is Flutter?


What is flutter? In simple word flutter is open source framework that uning Dart language that created by Google. We can say Flutter is Hybrid framework for create iOS and Android App. There are so many hybrid framework available in market like PhoneGap, ionic, Xamarin so what is different? Flutter provide native look of App same like iOS and Android.

What is difference between Native and hybrid application?

You can find many tutorial on this topic so we should not go in deep but I would like to explain in one sentence.

=> Hybrid App means It has one code that compatible with all the platforms like iOS, Android, Window, BlackBerry and Etc.

=> Native App has separate code for separate platforms like iOS has it’s own iOS code, Android has It’s android code and Etc.

If we are talking about majority usage of OS in mobile technology then two OS coming in our mind and one is iOS and another is Android.

So If you are looking for develope App in iOS and Android and save your money, time and developement then Flutter provides best platform for you.

How can we say flutter is better than other Hybrid frameworks?

=>Here  are main reason that we are going with Flutter.

  1. Speedy
  2. Less write code for develope App in compare to others
  3. Gives native look approx. 95%
  4. No need to use JS, HTML 5 code If you are JAVA developer then 70% work you made easy
  5. Attractive UI/Animation and easy to implement
  6. Secure


 Installation 


How can we develop application by Flutte?

I’m not here deeply explain you how to install flutter in our system because you can find it from official website so I don’t think so we should west our time to explain it again. But here are some important points those I like to point it down.

Here we are going with Mac OS and Xcode and below are minimum requirement of flutter.

Operating Systems: macOS (64-bit)        

Disk Space: 700 MB (does not include disk space for Xcode or Android Studio).
Tools: Flutter depends on below command-line tools so It will be available in your terminal. “bash, mkdir, rm, git, curl, unzip, which”
Xcode Version: Xcode 7.2 or newer

There is no any rule to use any specific editor for develop app in Flutter. You can use simple text editor too. But If you want to any rich and intelligent editor that will make your work easy and save your time then you can go with below two editor.


NOTE: Atom editor is no longer with flutter development (Click here for know more).


Here we are going with VC Code for Mac OS download it.



Get the Flutter SDK

  1. Download the following zip to get the latest beta release of the Flutter SDK
  2. Extract the file in the desired location,
E.g.:
$ cd ~/development
$ unzip ~/Downloads/flutter_macos_v0.4.4-beta.zip
Add the flutter tool to your path:
$ export PATH=`pwd`/flutter/bin:$PATH


The above command sets your PATH variable temporarily, that means it’s working only for the current terminal window. If you will close terminal and reopen it and write any flutter command the it will not work for you because you have to set permanently path for Flutter.


For update permanent path.


  1. Open / Create $HOME/.bash_profile, text file will be opened.
  2. Copy your path where you installed/store Flutter directory
  3. Set this Flutter directory path to below command
         
   export PATH=[PATH_TO_FLUTTER_GIT_DIRECTORY]/flutter/bin:$PATH


     4) Run source $HOME/.bash_profile to refresh the current window.
     5) Verify that the flutter/bin directory is now in your PATH by running:

echo $PATH

Now your permanent path is set.
You need to run Flutter Doctor command and install required commands and tools.
For more information click on below link



Below are some important links for Flutter