Home Blog Page 44

Why does flutter uses dart

0

Hi Guys, Welcome to Proto Coders Point. In this article let’s understand why flutter uses dart.

In flutter application developer, programmers loves building apps in flutter framework just because of the programming language it uses i.e. dart, At the time of inception about flutter, many experienced app developer analyzed and tested many language for flutter app building & at the end choose dart language. Here are few reason why does flutter use dart.

What is dart language

Dart programming language is an open-source, general purpose, object oriented programming language which make use of C-style syntax.

Who developed dart? => The Dart language is developed by Google in 2011.

The main purpose of developing dart language is to make easy in creating front end user interface for web and mobile app development.

Dart is compiled language so, it can’t be executed directly; instead, the compiler will parse it and send it to machine code.

dart supports the common concepts like classes, interface, functions. similar to any other programming language.


why flutter uses dart

Dart programming language manifesto is it makes use of AOT i.e. (Ahead Of Time), so AOT complies flutter dart code smoothly into native. This make developer enjoy the development process using dart for flutter app making.

Dart language has a ability of assigning object & garbage collection, without locks & does not require preemptive scheduling & memory sharing.

The main reason why flutter uses dart is, in flutter there is not separate layout language needed like JSX and XML, The layout i.e. UI design and backend programming can be code in single code, it makes developer to analyze the code and visualize the code with low effort.

In dart most of the features are similar to OOPS concept, is static and dynamic language, that make developer to learn dart quickly.

Feature of Dart language

  • OOPS: Dart is an object oriented programming language.
  • Fast code compilation: Start & Run Time performance must be great.
  • Portable: The Language is supported on any OS. (Cross Platform)
  • Ahead-of-Time:  (AOT) It is a Process of converting high level language into low level before execution of program, usually at build-time, to reduce the amount of work needed at run time.
  • Asynchronous Programming: Dart supports asynchronous programming which let your program run without getting blocked.

Flutter password strength validation checker

0
flutter password strength validation
password strength checker in flutter

Hi Guys, Welcome to Proto Coders Point, In this flutter article will learn how to implement password strength checker in flutter, That too without using any third party flutter_password_strength library’s.

Flutter Password Strength Checker

In this flutter example, Will follow below criteria to measure flutter password strength & validate:


To check if entered password contain Capital, Small letter, number & special character then you need to use below regular expression

RegExp pass_valid = RegExp(r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)");

Learn more about flutter password validation.


Flutter password validation + password strength checker

flutter password strength validation
flutter password strength validation

In this password validation + strength check tutorial, user entered password string length should be 8 or more characters, and the string should include Capital, Small, number & a special character.

Then, the strength of entered password will be shown in LinearProgressIndicator widget.

In LinearProgressIndicator:

  • red: Weak.
  • yellow: Medium.
  • blue: Strong
  • green: Super Strong

Only if flutter password strength is green then allowed to submit or create account, The submit form button is activiated to click as you can see the above gif example.


Password Validation Function Code Explaination

 //A function that validate user entered password
  bool validatePassword(String pass){
    String _password = pass.trim();

    if(_password.isEmpty){
      setState(() {
        password_strength = 0;
      });
    }else if(_password.length < 6 ){
      setState(() {
        password_strength = 1 / 4;                    //string length less then 6 character
      });
    }else if(_password.length < 8){
      setState(() {
        password_strength = 2 / 4;                   //string length greater then 6 & less then 8
      });
    }else{
      if(pass_valid.hasMatch(_password)){            // regular expression to check password valid or not
        setState(() {
          password_strength = 4 / 4;                 
        });
        return true;                                
      }else{
        setState(() {
          password_strength = 3 / 4;
        });
        return false;
      }
    }
    return false;
  }


Complete Source Code – Flutter Password Validation with strength check

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomepage(),
    );
  }
}

class MyHomepage extends StatefulWidget {
  const MyHomepage({Key? key}) : super(key: key);

  @override
  State<MyHomepage> createState() => _MyHomepageState();
}

class _MyHomepageState extends State<MyHomepage> {
  final _formKey = GlobalKey<FormState>();

  // regular expression to check if string
  RegExp pass_valid = RegExp(r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)");
  double password_strength = 0; 

   // 0: No password
  // 1/4: Weak
  // 2/4: Medium
  // 3/4: Strong
  //   1:   Great

  //A function that validate user entered password
  bool validatePassword(String pass){
    String _password = pass.trim();

    if(_password.isEmpty){
      setState(() {
        password_strength = 0;
      });
    }else if(_password.length < 6 ){
      setState(() {
        password_strength = 1 / 4;
      });
    }else if(_password.length < 8){
      setState(() {
        password_strength = 2 / 4;
      });
    }else{
      if(pass_valid.hasMatch(_password)){
        setState(() {
          password_strength = 4 / 4;
        });
        return true;
      }else{
        setState(() {
          password_strength = 3 / 4;
        });
        return false;
      }
    }
    return false;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Form(
          key: _formKey,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Padding(
                padding: const EdgeInsets.all(20.0),
                child: TextFormField(
                  onChanged: (value){
                    _formKey.currentState!.validate();
                  },
                  validator: (value){
                      if(value!.isEmpty){
                        return "Please enter password";
                      }else{
                       //call function to check password
                        bool result = validatePassword(value);
                        if(result){
                          // create account event
                         return null;
                        }else{
                          return " Password should contain Capital, small letter & Number & Special";
                        }
                      }
                  },
                  decoration: InputDecoration(border: OutlineInputBorder(),hintText: "Password"),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(12.0),
                child: LinearProgressIndicator(
                  value: password_strength,
                  backgroundColor: Colors.grey[300],
                  minHeight: 5,
                  color: password_strength <= 1 / 4
                      ? Colors.red
                      : password_strength == 2 / 4
                      ? Colors.yellow
                      : password_strength == 3 / 4
                      ? Colors.blue
                      : Colors.green,
                ),
              ),
              ElevatedButton(onPressed: password_strength != 1 ? null : (){
                //perform click event
              }, child: Text("Submit form"))
            ],
          ),
        ),
      ),
    );
  }
}



Flutter password validation – Capital Small letter, number & special character

0
flutter password validation-min
password should contain capital small, number and special character

Hi Guys, Welcome to Proto Coders Point. In this Flutter Article, will learn how to validate a password field in flutter so that we can make user to enter a strong password while filling a form in flutter app.

Flutter Password Validation

When a user enter’s password it should contain atleast one Capital Letter, Small Letters, Numbers & a special character.

Regular Expression for password validation

Below is regular expression to check a string if it contain atleast one Capital Letter, Small Letters, Numbers & a special character or no

RegExp pass_valid = RegExp(r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)");

Complete Source Code – Password Validation in flutter

Below code is just an Example to validate password field in flutter, So it simply have a form with only one TextField where user can enter password and a button that calls a function to check if entered password validates the RegularExpression.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomepage(),
    );
  }
}

class MyHomepage extends StatefulWidget {
  const MyHomepage({Key? key}) : super(key: key);

  @override
  State<MyHomepage> createState() => _MyHomepageState();
}

class _MyHomepageState extends State<MyHomepage> {
  final _formKey = GlobalKey<FormState>();

  // regular expression to check if string
  RegExp pass_valid = RegExp(r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)");

  //A function that validate user entered password
  bool validatePassword(String pass){
    String _password = pass.trim();

    if(pass_valid.hasMatch(_password)){
      return true;
    }else{
      return false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Form(
          key: _formKey,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Padding(
                padding: const EdgeInsets.all(20.0),
                child: TextFormField(
                  validator: (value){
                      if(value!.isEmpty){
                        return "Please enter password";
                      }else{
                       //call function to check password
                        bool result = validatePassword(value);
                        if(result){
                          // create account event
                         return null;
                        }else{
                          return " Password should contain Capital, small letter & Number & Special";
                        }
                      }
                  },
                  decoration: InputDecoration(border: OutlineInputBorder(),hintText: "Password"),
                ),
              ),
              ElevatedButton(onPressed: (){
                _formKey.currentState!.validate();
              }, child: Text("Submit form"))
            ],
          ),
        ),
      ),
    );
  }
}


Video Tutorial on youtube


Check out Similar Articles on Validation

Flutter Password Validation

Flutter Form Validation

Flutter Email Validation using plugin

Flutter Email Validation using Email Regex Pattern

How to add commas to number – Digit Grouping Number format – Dart

0
dart display number in digit grouping number format
dart display number in digit grouping number format

Hi Guys, Welcome to Proto Coders Point. In this dart tutorial will learn how to display large number by adding commas to the numbers, so basically will do Number formatting in digit grouping form.

For Example: 100000 will be displayed as 1,00,000.

In Dart we have a class i.e. NumberFormat which comes from flutter intl package.

Dart NumberFormat class

In Dart a number format class will help us by it’s ability to format number in a locale format.

From intl numberformat we will use decimalPattern to display number in Digit Grouping like this 1,00,000


Install intl package

If Dart Project:

dart pub add intl

In Flutter Project:

flutter pub add intl

Now import intl.dart where require

import 'package:intl/intl.dart';

Flutter Dart – Add commas digit grouping to large number

Example:

import 'package:intl/intl.dart';

void main() {
   var longnum = 100500064965849;
   var num = 10000000;
   var longnum2 = 87965100421;

  NumberFormat myFormat = NumberFormat.decimalPattern('en_us');
  print(myFormat.format(longnum));
  print(myFormat.format(num));
  print(myFormat.format(longnum2));
}

output

dart digit grouping

How to Check if flutter app in running on Web or Mobile – Flutter kIsWeb

0
Check if App is Running on Web or Mobile
Check if App is Running on Web or Mobile

Hi Guys, Welcome to Proto Coders Point.

Flutter kIsWeb – Check if flutter app is running on Web

kIsWeb in flutter is a constant that will return true if your flutter app is compiled and run on web browser.

By using kIsWeb we can check if our flutter app is running on web or mobile, as it return true if run on web.

So to use kIsWeb constant we must import foundation.dart in-build library.

Example on kIsWeb

import 'package:flutter/foundation.dart';

void main() {

  var isWeb = kIsWeb;
  print(isWeb);   // true or false
   
}

Video Tutorial

Video Tutorial – Futter kIsWeb

Complete Code Example on how to use flutter kIsWeb constant

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  var isWeb = kIsWeb;  // get is app is running on web or no // true or false
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }


  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(isWeb ? "App is Running on Web" : "App is Running on Mobile"),
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), 
    );
  }
}

Flutter web run on custom port – fixed port number

0
flutter web run on custom port

Hi Guys, Welcome to Proto Coders Point. In this article will learn how to run flutter web app on specified port number.

Flutter web change port number

By default, When you run flutter web app on browser localhost, each time app will start on random port number (eg: http://localhost:50465/#/).

Sometimes, In development process you may need to run flutter web app on fixed port, Say your server only accept request from particular port, or you have implement firebase auth services & for security reason you are accepting request only from certain ports), In that case you need to run flutter web app on fixed port.

To do it, will make use of flag '--web-port=<custom port number>'

// run flutter web on custom port 

flutter run -d chrome --web-port=4000

// screenshot below

screenshot flutter web running on custom defined port number