Hi Guy’s Welcome to Proto Coders Point, In this flutter article let’s check out how to use PageView widget in flutter.
Example on PageView Widget you will see in this article:-
Simple 3 Pageview.
PageView with dots indicator.
Before going into flutter code, let’s explore more about flutter pageview widget.
What is Flutter PageView? Where & How to use it
A page-by-page scrollable list is build by using flutter pageview widget. It been widely used to create a beautiful carousel or slideshow UI, Which you may have observed in many applications.
In this Example, I will create 3 separate dart file (page1, page2, page3) in lib directory of my flutter project. This 3 pages will be shown in main.dart page by using PageView Widget.
Similarly create 2 more pages and design it as per your UI.
main.dart – Complete Code with Explanations
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'page1.dart';
import 'page2.dart';
import 'page3.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> {
// Initializing a Controller fore PageView
final PageController _pageViewController = PageController(initialPage: 0); // set the initial page you want to show
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_pageViewController.dispose(); // dispose the PageController
}
@override
Widget build(BuildContext context) {
return Scaffold(
//use pageview widget to show pages when user slides
body: PageView(
controller: _pageViewController,
children: [
Page1(),Page2(),Page3()
],
) ,
);
}
}
Example 2 – Pageview with indicator flutter example
Show a Page Slider active page dots, basically show a dots at bottom.
In Below Example, as I said will have dots indicator, which indicate which is the current active page, The App user can simply swipe left, right to switch between pages and also taps on the dot indicator to switch between pages.
Below code output:
Complete Source Code – main.dart
import 'package:flutter/material.dart';
import 'page1.dart';
import 'page2.dart';
import 'page3.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> {
// Initializing a Controller fore PageView
final PageController _pageViewController = PageController(initialPage: 0); // set the initial page you want to show
int _activePage = 0; // will hold current active page index value
//Create a List Holding all the Pages
final List<Widget> _Pages = [
Page1(),
Page2(),
Page3()
];
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_pageViewController.dispose(); // dispose the PageController
}
@override
Widget build(BuildContext context) {
return Scaffold(
// will make use of Stack Widget, so that One Widget can we placed on top
body: Stack(
children: [
PageView.builder(
controller: _pageViewController,
onPageChanged: (int index){
setState(() {
_activePage = index;
});
},
itemCount: _Pages.length,
itemBuilder: (BuildContext context, int index){
return _Pages[index];
}
),
//creating dots at bottom
Positioned(
bottom: 0,
left: 0,
right: 0,
height: 40,
child: Container(
color: Colors.black12,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List<Widget>.generate(
_Pages.length,
(index) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: InkWell(
onTap: () {
_pageViewController.animateToPage(index,
duration: const Duration(milliseconds: 300),
curve: Curves.easeIn);
},
child: CircleAvatar(
radius: 5,
// check if a dot is connected to the current page
// if true, give it a different color
backgroundColor: _activePage == index
? Colors.greenAccent
: Colors.white30,
),
),
)),
),
),
),
],
),
);
}
}
Hi Guy’s Welcome to Proto Coders Point. In this Flutter Tutorial, let’s learn how to format number in currency format in flutter dart.
Example:
$ 10,000.00 , ₹ 10,000.00
To display digit in flutter currency format, will make use of dart offlicial package “intl“ using this flutter intl package we can make user NumberFormat class to format number to currency in flutter.
Let’s get started
Adding/installing intl dependencies in flutter project
Open pubspec.yaml file
In pubspec.yaml file under dependencies section add intl as follow:
dependencies:
intl: ^0.17.0
Now, hitp pub get button or enter below command to download the intl package
flutter pub get
Import it to use intl
Then, To use intl package for currency number format you need to import it where reqiured:
import 'package:intl/intl.dart';
Video Tutorial
Intl NumberFormat Currency- flutter
NumberFormat in flutter dart
1. Simple Currency digit format
To show currency symbol ($, ₹, đ, €, ¥… etc) at the start of number use below code:
Hi Guys, Welcome to Proto Coders Point, In this flutter article let’s learn how to log long string in flutter, How to fix pring statement in flutter that only pring 1020 character & trancate the remaining string & leaves this symbol at end <..>.
Basically, The current version of dart language does not support printing logs that has more then 1020 character, instead print() statement in flutter will only print 1020 character & trancate remaining.
Check below screenshot
So, Here is a solution on How to Prin full long string/list without missing any data while debugging in flutter.
Print Long String in console While developing flutter app
Here is the right way to print logs message or data on console for debugging your flutter application.
The Right way to log data on console is by importing dart inbuilt package “dart:developer”, This allows a developer to print longs information on console while debugging.
So to use developer.log function in flutter app you need to import it.
Hi Guys, Welcome to Proto Coders Point, In this quick code snippet article, let’s learn different way to reverse a string in python with & without using python inbuild methods.
Different ways to reverse a string in python language
Let’s get Started
Example 1: Reverse a string in using for loop
Below example reverse a string without using inbuilt function in python
str = "ebutuoY no tnioP sredoC otorP ebircsbuS"
st =""
for i in str:
st = i + st
print(st)
Example 2: Reverse string in Python using an slice
We can easily reverse a string by using python operator called extended slice operator.
Basically slice operator in python accepts 3 parameters (start, stop & step), In below example for start & stop index we pass no value, which will by default start index from 0 and end with n-1, then in step parameter we pass -1, which means string be been slice and traverse from end of given string, up to the 1st index position and thus we have a reversed string.
def rev(s):
s = s[::-1]
return s
s = "Proto Coders Point"
print("The original string : ",s)
print("The reversed string : ",rev(s))
Example 3: python code to reverse a string using join & reversed function
Here we are using python reversed function that is an inbuild method using which we can reverse the given string
def rev(str):
rev_str = "". join(reversed(str))
return rev_str
s = "Proto Coders Point"
print ("The original string : ",s)
print ("The reversed string using join & reversed func : ",rev(s))
Hi Guys, Welcome to Proto Coders Point. In this Article let’s learn How to secure EMQX by adding mnesia authentication plugin and enabling mnesia auth , by which a user can only get connected to MQTT Broker if he has authorized username & password.
Here is a article & Video Tutorial on How to Install EMQX on Ubuntu Server & Connect to MQTT Broker by using MQTT SPY tool
How to Secure EMQX by mnesia authentication plugin – EMQX tutorial
Let’s Get Started
We will be be using emqx mnesia auth plugin to secure our mqtt broker, so that only user with authorized username & password and connect to Emqx mqtt broker.
Step 1: Connect to Server & Open Putty Terminal
To connect to server instance terminal using putty follow below steps:
1. In Session, Enter Host Name i.e IP of server and port.
2. Then in Connection -> Data, Enter username. Eg: ubuntu.
3. Then in Connection -> SSH -> Auth, Browse/select ppk file.
4. Connect, Click on Open.
Step 2: Turn Off EMQX allow_anonymous
By default, allow anonymous access in emqx is true, means anyone with server IP address can get access to MQTT Broker and read all the message, To secure it we need to Turn of anonymous access by just setting allow_anonymous to false.
To do so, we need to edit emqx.conf file.
open emqx.conf by using any ubuntu editor, I use vim editor. run below command to open the file.
sudo vim /etc/emqx/emqx.conf
This will open the file in terminal itself, use arrow key and search for allow_anonymous and set it to false as show below.
now by pressing Esc > :wq save the file.
Step 3: Config mnesia Authentication
As I said above, We will use mnesia auth plugin to secure auth to mqtt broker.
For that we must config emqx_auth_mnesia.conf file and add Username Authenticatiion in EMQX.
Open emqx_auth_mnesia.conf by running below cmd:
sudo vim /etc/emqx/plugins/emqx_auth_mnesia.conf
now by pressing Esc > :wq save the file.
Step 4: Active / Load EMQX mnesia Authentication
Now run below cmd to load mnesia emqx auth plugin:
emqx_ctl plugins load emqx_auth_mnesia
We have now successfully secure our MQTT Message Broker i.e EMQX.
Step 5: Restart EMQX
Now, After adding auth security to EMQX, We need to Restart it.
sudo emqx restart
Step 6: Connect to EMQX using MQTT-spy tool
I am using mqtt-spy tool to get connected to my mqtt broker and watch to the incoming message in queue.
Here are screenshot how to connect to EMQX broker.
DONE, Finally We have secured our EMQX by adding username & Password Authentication using mnesia emqx auth plugin.
Hi Guys, Welcome to Proto Coders Point. In this article let’s Install EMQX on ubuntu. Basically EMQX is a message broker mainly used for communication between IOT Device & Server clould.
What is MQTT
MQTT stands fo Message Ouewing Telemetry Transport/ MQTT broker is basically used in IOT devices, It an Intermediary that enable MQTT client to communicate.
MQTT is IOT utilized Qos level, that assure message delivery to receiver, even thougn connection amont devices are unstable.
Basically MQTT Broker is a message quewing that accepts or listens to all client messages & then delivers the message to defined destination client (i.e Subscribers).
EMQX (mqtt) is a protocal for exchanging message(data) between IOT devices & server applications.
Video Tutorial
How to Install EMQX on ubuntu server
1. Install Required Dependencies for EXQX
Before install exqx their are few dependencies to be install, run below command and install them.