Home Blog Page 13

Programmatically Take Screenshots in Flutter

0
Programmatically take screenshot in flutter

In the digital age, screenshots are an essential component of daily routine  life. They is an efficient part to collect and share the data. that can be very useful in a variety of circumstances. Mobile application screenshots can be used for a variety of the purposes including testing, debugging, documentation etc.. This blog main topic will be how to take a screenshot in a Flutter app.

When testing, debugging, or showcasing the user interface of your Flutter application, the ability to take screenshots is a useful feature. Flutter’s straightforward API makes taking screenshots a breeze. In this blog, we’ll look at how to screenshot in Flutter.

Flutter is a well liked and lightweight framework for building mobile applications . That provides a quick and simple way to create visually appealing, powerful mobile apps for the iOS and Android operating systems. Developers can easily create original interfaces and layouts with Flutter’s high variety of widgets and tools.

How to Programmatically take a Screenshot in flutter

Get started…

Step 1) : Add the Screenshot Package

You must first include the Screenshot package in your project in order to take screenshots in Flutter. In your pubspec.yaml file, open the dependencies section, and then add the following code:

screenshot: ^any

Step 2) : After saving the file launch flutter pub. enter your terminal and start the package download.

flutter pub get

Step 3) : import the file

import 'package:screenshot/screenshot.dart';

Step 4) :Make a screenshot controller

The creation of a Screenshot Controller instance is the next step. You can use this controller to take a screenshot of your widget.

final _screenshotController = ScreenshotController();

Step 5) :Wrap the screenshot of your widget

You must wrap your widget in the Screenshot widget in order to take a screenshot of it. following code should be included in the build method :

Screenshot(
  controller: _screenshotController,
  child: //your widget here,
),

The child property holds the widget that we want to screenshot , and the controller property gives us access to the Screenshot Controller object we previously set up.

Step 6) : Taking a Screenshot

By calling the capture() method on the Screenshot Controller instance, you can finally take a screenshot. Here is an illustration of how to use a floating action button to invoke the capture() method:

FloatingActionButton(
  onPressed: () async {
    final image = await _screenshotController.capture();
    /// do something with the image
  },
  child: Icon(Icons.camera_alt),
),

Step 7) :How to Show or Save a Screenshot

After taking the screenshot you have the option of viewing it or saving it to as a  file. An example of how to show the screenshot that was taken is provided here below :

Image.memory(
  image,
  fit:BoxFit.cover,
),

here,…. you can save screenshot image data  to a file use writeAsBytes function in flutter framework .

final directory = await getApplicationDocumentsDirectory();
final imagePath = '${directory.path}/screenshot.png';

final file = File(imagePath);
await file.writeAsBytes(image);

Step 8) :Reduce the Controller Widget and Screenshot Widget.

It’s critical to remove the Screenshot widget and the Screenshot Controller instance from your code after the screenshot has been taken in order to avoid any performance impact. To accomplish this, set the controller property of the Screenshot widget to null:

Screenshot(
  controller: null,
  child: //your widget here,
),

Additionally, by removing the Screenshot Controller instance:
@override
void dispose() {
///Controller dispose here  
  super.dispose();
}

It’s over now! These easy steps will help you take screenshots of your Flutter app.


screenshot of a widget using RepaintBoundary in Flutter

Video Tutorial

i) : order to take the screenshots of widgets . we will use RepaintBoundary widget as the parent widget. Global key must be used to specify RepaintBoundary.

Visit Flutter RepaintBoundary for more information.

static GlobalKey screen = new GlobalKey();
 RepaintBoundary(
        key: screen,
     child: ChildWidgets(
   )

So that it can be quickly saved in device local storage and transformed into an image bites, the RepaintBoundary widget must then be converted to an image.

Step 1) :Use RepaintBoundary to create widget.

Make widget in your file called MyWidget or any name :

import 'package:flutter/material.dart';

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RepaintBoundary(
      child: Container(
        color: Colors.blue,
        width: 200,
        height: 200,
        child: Center(
          child: Text(
            'Hello World!',
            style: TextStyle(
              color: Colors.white,
              fontSize: 30,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }
}

This widget has a 300 pixel wide by 300 pixel high container with a white background a font size of 30 bold font weight blue background.

We must first create a GlobalKey object to identify the widget from which we want to take the screenshot. There will also be developed a function called captureScreenshot that will take a screenshot and store it on the device storage.

/// here Define the function to capture the screenshot
  Future<void> captureScreenshot() async {
    try {
      /// Find boundary of the RepaintBoundary widget
      RenderRepaintBoundary boundary =
          _globalKey.currentContext.findRenderObject();
      /// Convert the widget to image
      ui.Image image = await boundary.toImage(pixelRatio: 3.0);
      // Convert image to a byte data
      ByteData byteData =
          await image.toByteData(format: ui.ImageByteFormat.png);
      /// Convert byte data to an Uint8List
      Uint8List pngBytes = byteData.buffer.asUint8List();
      /// Get the application documents directory to save the screenshot widget
      final directory = (await getApplicationDocumentsDirectory()).path;
      /// Create file object and save the screenshot
      File imgFile = File('$directory/screenshot.png');
      await imgFile.writeAsBytes(pngBytes);
      /// Print  message indicating that the screenshot has been taken
      print('Screenshot taken successfully' );
    } catch (e) {
      // Handle the error if there is any
      print(e);
    }
  }

This code creates a Flutter application that use  RepaintBoundary to take screenshot of a widget in flutter . The widget that needs to be captured is wrap in the RepaintBoundary widget .the widget is identified by a global key in flutter.

When the user selects the Take Screenshot button the screenshot is saved to the device local storage .The RepaintBoundary widget boundary is located the captureScreenshot function is invoked and the widget is converted to an image. the image is converted to byte data. byte data is converted to an Uint8List and screenshot is saved in local storage.

For , get this screenshots code , click here…..


Conclusion 👍

I hope you can put this to use.To take a screenshot. use the Flutter screenshot package. With the help of this package we can take screenshot in a specific widget or the entire application screen. In this blog post, we will look at using this package to take screenshot in Flutter application. This example code can be changed to suit your requirements.

Thanks for reading this article……

Have a beautiful day…..

How to use the Value Notifier in flutter.

0
flutter value Notifier

ValueNotifier is a unique subclass of Changenotifier that can hold a single value and alert widgets that are listening to it whenever that value changes. ValueNotifier extends Changenotifier.

ValueNotifier offers an easy and effective way to manage and update state in your application. making it a useful and lightweight tool for developing reactive data sources in Flutter.

When you need to update the UI due to changes in a value. ValueNotifier comes in handy. You can use a ValueNotifier for instance to keep track of changes to the value of a text field in a user interface . so that other UI elements can be updated to reflect the new value.

The main benefit of using a ValueNotifier over state management alternatives like setState or streams is that it is portable and simple to operate. It does not necessitate the creation of intricate streams and stream controllers, nor is it as resource-intensive as setState for straightforward use cases.

What is ValueNotifier?

Let’s first define ValueNotifier so that we can proceed to the specifics of how to use it in Flutter. A straightforward observable data model can be made using the class called ValueNotifier. It is a simplified version of the ChangeNotifier class, which forms the backbone of the reactive programming style used by Flutter. ValueNotifier has a single property named value that stores the model’s current value. The model alerts its listeners whenever its value changes. Due to this, it is simple to develop reactive Flutter applications, in which the UI is updated whenever the data changes.

How can ValueNotifier be used?

It’s very easy to use ValueNotifier in Flutter. You can create a ValueNotifier by providing it with an initial value. For instance , you  follow these steps to create a ValueNotifier that stores an integer value:

// syntax --> ValueNotifier<dataType> myValueNotifier = ValueNotifier<dataType>(value);

// Example :
ValueNotifier<int> myValueNotifier = ValueNotifier<int>(0);

In this example, we making a ValueNotifier that stores the value 0 as an integer.

When you have a use  ValueNotifier you can use the value property to read and modify its value. For instance you can follow these steps to read the value of the ValueNotifier:

Read the value of the ValueNotifier Object:

int currentValue = myValueNotifier.value;

ValueNotifier value property’s value can be modify by assigning  new value . For example the following steps can be used to update the ValueNotifier value to 100 :

myValueNotifier.value = 100;

The ValueNotifier notifier listeners updated whenever value is updated or changed . The addListener method can be used keep track of changes in ValueNotifier variable.

myValueNotifier.addListener(() {
  print('Value changed to ${myValueNotifier.value}');
});

In this example, we’re extending the myValueNotifier with a listener that prints a message whenever its value changes.

To build a basic data model that can be used to update the user interface, ValueNotifier is frequently used in Flutter widgets. ValueNotifier can be used in widgets by simply creating a new instance of it and passing it to the child widgets as necessary. Here’s a good example:

using ValueNotifier


class ValueNotifierExample extends StatelessWidget {
  ValueNotifierExample({Key? key}) : super(key: key);
  final ValueNotifier<int> myValueNotifier = ValueNotifier<int>(42);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(backgroundColor: Colors.blue.shade900,title: const Text("Value notifier implement")),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('Value: ${myValueNotifier.value}'),
          ElevatedButton(
            onPressed: () {
              myValueNotifier.value += 1;
            },
            child: const Text('Increment Value'),
          ),
        ],
      ),
    );
  }
}

Here is an example showing , how to create a ListView in Flutter using the ValueNotifier builder set:

import 'package:flutter/material.dart';

class ListViewValueNotifier extends StatefulWidget {
  const ListViewValueNotifier({super.key});

  @override
  _ListViewValueNotifierState createState() => _ListViewValueNotifierState();
}

class _ListViewValueNotifierState extends State<ListViewValueNotifier> {
  ValueNotifier<int> selectedIndex = ValueNotifier<int>(0);

  List<String> items = [
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4",
    "Item 5",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.blue.shade900,
        title: const Text("ListView with ValueNotifier"),
      ),
      body: ValueListenableBuilder(
        valueListenable: selectedIndex,
        builder: (BuildContext context, int index, Widget? child) {
          return ListView.builder(
            itemCount: items.length,
            itemBuilder: (BuildContext context, int i) {
              return ListTile(
                title: Text(items[i]),
                selected: i == index,
                onTap: () {
                  selectedIndex.value = i;
                },
              );
            },
          );
        },
      ),
    );
  }
}

Conclusion 👍

ValueNotifier is a component of Flutter’s core Reactive Programming Model, which enables the creation of responsive and effective user interfaces. A ValueNotifier is a simple class that enables the development of reactive data sources that alert listeners when their values change.

ValueNotifier offers an easy and effective way to manage and update state in your application, making it a useful and lightweight tool for developing reactive data sources in Flutter.

Thanks for reading this article 💙…..

Have a beautiful day…..

Create routes in flutter Navigation (Deep-Dive)

0
flutter routes
material page route flutter

An open-source framework called Flutter is used to create powerful, cross-platform mobile apps. The navigation system is one of the most crucial components of any mobile application. Routes a user-friendly and highly customizable navigation system is offered by Flutter. We’ll go into great detail about creating routes in Flutter in this blog .

An application with multiple pages is one in which we design screens with their own widgets one of which is always visible.

The screens are typically displayed in response to user interactions which is accomplished by using navigational functionalities.

What is Navigation?

In a Flutter application switching between screens or views is referred to as “navigation”. Any mobile application must have navigation because it allows users to move between the app’s various screens and interact with its features.

Each screen or view in Flutter is represented by a widget and navigation is accomplished by stacking routes. The previous screen is pushed onto the stack and the new screen is displayed when a user navigates to a new screen.

To implement navigation in an application Flutter offers various patterns and methods including:

Navigator widgets : The Navigator widget in Flutter controls a stack of Route objects that stand in for the app’s screens. It offers ways for developers to push and pop routes from the stack, allowing them to manage the application’s flow.

Named routes: Named routes let app developers give each screen in their application a special name so that users can find it using the Navigator.pushNamed() method.

Bottom navigation bar: A bottom navigation bar is a widget that enables users to quickly switch between the app’s various screens by displaying a list of icons or labels at the bottom of the screen.

Tabs: Users can switch between various categories or views on  screen by using tabs which offer a horizontal list of tab labels.

Drawer:  A drawer is a panel that slides out from the side of the screen and gives the user access to a list of navigation options.

These methods enable programmers to integrate flexible and user-friendly navigation system into their Flutter applications resulting in a seamless user experience.


What are Routes?

Routes in Flutter are a way for users to move between different screens or pages of your application. A widget and distinct route are used to represent each screen in your application. Push and pop operations, which are used to move around a stack, can be used to navigate routes.

The current screen is pushed onto a stack of screens when a user switches between them. The current screen is removed from the stack and the previous screen is shown when the user requests to return to the previous screen.

Making a Simple Route

In Flutter, building a simple route is very easy. Simply defining a widget to represent your screen and link it to a route name is all that is required.

Say we want to move from the home screen to the settings screen. We must create a new widget that represents the settings screen in order to create a route for the settings screen. 

Here’s an illustration:

import 'package:flutter/material.dart';

class SettingsScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings'),
      ),
      body: Center(
        child: Text('Settings Screen'),
      ),
    );
  }
}

Here… for an example is :In the previous illustration, we create a new widget called SettingsScreen that shows a straightforward screen with appBar and text widget in the center.

We must give our widget a route name now that we have it. The MaterialPageRoute class which generates a route that shows a full-screen modal dialog, can be used to achieve this. To create a route for our SettingsScreen, follow these steps:

MaterialPageRoute(
  builder: (context) => SettingsScreen(),
);

In the previous example a fresh MaterialPageRoute is made and connected to our SettingsScreen widget. callback function returns our widget builder parameter.


Choosing a Route to Follow

We can navigate to our route from our home screen now that we have defined it. The Navigator widget, which controls the navigation stack must be used in order to accomplish this.

Using the Navigator.push() method we can add a fresh route to the stack. Here an illustration:

Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => SettingsScreen()),
);

In the aforementioned example we call the Navigator.push() method passing the current BuildContext and our MaterialPageRoute object.

Pop the Route

To go back to the previous screen we can use the Navigator.pop() method. This method returns any data passed to the route along with the top route that was pulled from the navigation stack. Here’s an illustration:

Navigator.pop(context);

In the previous illustration, we used the Navigator.Using the pop() method, send the current BuildContext. This will take you back to the previous screen and pop the top route from the navigation stack

By assigning each route a distinct name, named routing organizes your app’s navigation and makes it simpler to move between screens. Named routing offers a way to navigate to a specific screen by using a named route as opposed to using the widget hierarchy.

The use of named routing in Flutter including how to define named routes pass data between screens, and handle route transitions will be covered in this blog.

Defining Named Routes

To use named routing in Flutter, you first need to define your routes. This is done in the main.dart file of your app, in the MaterialApp widget. The routes parameter, which is a map of String keys and WidgetBuilder values, is where you define your routes.

Let’s say, for illustration purposes, that our app has two screens: a home screen and a details screen. Our named routes can be described as follows:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => FirstScreen(),
        '/second': (context) => SecondScreen(),
      },
    );
  }
}

here defined two routes in the example: “/” for the first screen and “/secondScreen” for the second screen. here, we set the initialRoute to “/,” which is the when app launch on the first screen.

Named routes

We defined here two routes in this  example : “/” for the home screen and 

“/details” for the details screen define . Additionally we set the initial Route to “/” which causes the app launch on the homeScreen.

Making your way to named routes

The Navigator.pushNamed() method which accepts the route name as a parameter can be used to navigate to a named route.

For instance  we could follow these steps to move from the home screen to the details screen:

Navigator.pushNamed(context, '/details'');

By doing this  the user would see the details screen . it would be pushed onto the navigation stack.


Data Transfer between Screens in named routes

You’ll frequently need to pass data between your applocations screens. By passing arguments to the Navigator.pushNamed() method .Let’s say, for illustration that we want to pass the selected item to the details screen from a list of items on the home screen. By giving the item to the Navigator as an argument. we can accomplish this. the pushNamed() function.

Navigator.pushNamed(context, '/details', arguments: item);
class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final item = ModalRoute.of(context)!.settings.arguments as Item;

    return Scaffold(
      appBar: AppBar(
        title: Text(data.title),
      ),
      body: Center(
        child: Text(data.description),
      ),
    );
  }

Managing Route Changes

You can design the way that screens transition within your app in addition to naming routes and getting there. This can be accomplished by specifying a unique transition animation for each route using the MaterialPageRoute function Object() { [native code] }.

Take a fade-in transition, for instance, which we might want to use for the details screen. Making a new MaterialPageRoute and setting its transitionDuration and pageBuilder properties would allow us to accomplish this:

When switching between screens, Flutter offers a variety of built-in transition animations that can be used. There are many of these, such as FadeTransition, SlideTransition, and ScaleTransition. By extending the MaterialPageRoute class and overriding the buildTransitions method, you can also produce your own original transition animation.

class SlideRightRoute extends PageRouteBuilder {
  final Widget page;
  SlideRightRoute({required this.page})
      : super(
          transitionDuration: Duration(milliseconds: 500),
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            var begin = Offset(1.0, 0.0);
            var end = Offset.zero;
            var tween = Tween(begin: begin, end: end);
            var offsetAnimation = animation.drive(tween);
            return SlideTransition(
              position: offsetAnimation,
              child: child,
            );
          },
          pageBuilder: (context, animation, secondaryAnimation) => page,
        );
}

Navigator.push(
  context,
  SlideRightRoute(page: DetailsScreen()),
);

We create a new SlideRightRoute class that extends PageRouteBuilder in the aforementioned example. In order to create a SlideTransition that slides the screen in from the right, we define the transitionsBuilder method and set the transitionDuration property to 500 milliseconds. The Navigator.push method then receives our custom route as a parameter.


Choosing a Different Transition Duration

The duration of the route transition animation can be set using the transitionDuration property. You can change the value from the default of 300 milliseconds to one of your choosing.

For instance, we could modify the transitionDuration property as follows to make our custom slide-in-right transition last for 750 milliseconds:

class SlideRightRoute extends PageRouteBuilder {
  SlideRightRoute({required Widget page})
      : super(
          transitionDuration: Duration(milliseconds: 750),
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            //...
          },
          pageBuilder: (context, animation, secondaryAnimation) => page,
        );
}

defaultRouteName

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/second': (context) => SecondScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Second Screen'),
          onPressed: () {
            Navigator.pushNamed(context, '/second');
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go back to Home Screen'),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

The HomeScreen widget will be the first screen to be displayed when the programme is opened because the defaultRouteName attribute in this example is set to ‘/’. Each route in the app has a widget attached to it that should appear when the route is browsed to. These routes are defined by the routes parameter.

The Second’ route, which is represented by the SecondScreen widget is reached via the Navigator.pushNamed function. To return to the previous screen which in this instance is the HomeScreen widget use the Navigator.pop function.


PushReplacementNamed 

PushReplacementNamed is a method in Flutter , that lets you switch to a new screen while erasing the one that is currently on the navigation stack.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/second': (context) => SecondScreen(),
        '/third': (context) => ThirdScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Second Screen'),
          onPressed: () {
            Navigator.pushReplacementNamed(context, '/second');
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Third Screen'),
          onPressed: () {
            Navigator.pushReplacementNamed(context, '/third');
          },
        ),
      ),
    );
  }
}

class ThirdScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Third Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go back to Home Screen'),
          onPressed: () {
            Navigator.popUntil(context, ModalRoute.withName('/'));
          },
        ),
      ),
    );
  }
}



The HomeScreen and SecondScreen widgets in this example employ the pushReplacementNamed function to direct the user to the second and third routes, respectively. The current screen in the navigation stack gets replaced with the new screen when pushReplacementNamed is invoked.

The popUntil function is used by the ThirdScreen widget to return to the HomeScreen widget. The ModalRoute.withName(‘/’) predicate in this example matches the initial route, and the popUntil function continually pops the current route off the stack until the predicate supplied in its argument is true.


popAndPushNamed  

Pop the current screen off the navigation stack and push a new screen onto the stack all at once with Flutter’s popAndPushNamed function.

Here’s an illustration:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/second': (context) => SecondScreen(),
        '/third': (context) => ThirdScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Second Screen'),
          onPressed: () {
            Navigator.popAndPushNamed(context, '/second');
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Third Screen'),
          onPressed: () {
            Navigator.pushNamed(context, '/third');
          },
        ),
      ),
    );
  }
}

class ThirdScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Third Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go back to Home Screen'),
          onPressed: () {
            Navigator.popUntil(context, ModalRoute.withName('/'));
          },
        ),
      ),
    );
  }
}



In this illustration the HomeScreen widget popAndPushNamed function is used to simultaneously pop the active screen off the stack and put the “second” route into the stack. When you wish to replace the current screen with new one while also removing the current screen from the navigation stack this is useful.

When you wish to mimic a “refresh” of the current screen by replacing it with a new instance of the same screen, the popAndPushNamed function might be helpful.

pushNamedAndRemoveUntil

PushNamedAndRemoveUntil is a method in Flutter that lets you advance to a new screen while erasing all previous screens from the stack of screens you’re currently on, up until a certain condition is satisfied.

Here’s an illustration:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/second': (context) => SecondScreen(),
        '/third': (context) => ThirdScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Third Screen and remove previous screens'),
          onPressed: () {
            Navigator.pushNamedAndRemoveUntil(context, '/third', ModalRoute.withName('/'));
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Third Screen'),
          onPressed: () {
            Navigator.pushNamed(context, '/third');
          },
        ),
      ),
    );
  }
}

class ThirdScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Third Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go back to Home Screen'),
          onPressed: () {
            Navigator.popUntil(context, ModalRoute.withName('/'));
          },
        ),
      ),
    );
  }
}

The HomeScreen widget in this example uses the pushNamedAndRemoveUntil function to browse to the “third” route and delete all previous screens from the navigation stack. A RoutePredicate is used as the second input to pushNamedAndRemoveUntil, and it defines the prerequisite for halting the removal of earlier routes. ModalRoute.withName(‘/’) in this situation is used to indicate that any routes that come before the ‘/’ route (the first route) should be dropped.

When you wish to start again with new screen after removing a number of screens from the navigation stack . the pushNamedAndRemoveUntil function might be helpful. When creating logout function for instance you could wish to utilize.  it to delete all screens associated with the user’s session and start again with the login screen.

restorablePopAndPushNamed

With the help of the Flutter method restorablePopAndPushNamed you can move to a named route while simultaneously deleting the current route from the navigation stack and saving it for later restoration. Here is an illustration of how to use Flutter’s restorablePopAndPushNamed with named routes:


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/settings': (context) => SettingsScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Go to Settings'),
          onPressed: () {
            Navigator.restorablePopAndPushNamed(context, '/settings');
          },
        ),
      ),
    );
  }
}

class SettingsScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Go back to Home'),
          onPressed: () {
            Navigator.restorablePop(context);
          },
        ),
      ),
    );
  }
}

Two arguments are required by restorablePopAndPushNamed: a BuildContext and a String specifying the name of the route to be navigated to. It removes the current route from the navigation stack and replaces it with the named route.

RestorablePop pops the current route off the navigation stack and goes back to the previous screen with just a BuildContext argument.

Conclusion 👍

We thoroughly examined how to create routes in Flutter in this blog post. Routes are a way to move between screens or pages in your application, and we discovered that each screen is represented by a widget and connected to a particular route.

Additionally, we learned how to create a fundamental route, how to use the Navigator.push() method to travel to a route, and how to return.

Thanks for reading this article…..

Have a good day……

Integrate UPI payment gateway in flutter

0
flutter upi
flutter upi payment gateway

You will discover how to incorporate UPI Payment Gateway into your Flutter app in this article.

Payment gateways are now an essential component of mobile applications due to the increase in online transactions. Customers can instantly transfer money between bank accounts in India using the UPI (Unified Payment Interface) payment gateway without any hassle. A great way to give your users a safe and practical payment option is by integrating UPI payment gateway in a Flutter app. We will go over the exact steps for integrating a UPI payment gateway in a Flutter app in this blog post.

Requirements for UPI payments gateway 👍 in flutter

Make sure you have the following prerequisites before we begin:

  • Setup of the Flutter development environment on your computer
  • A tool or emulator to test the application
  • having a functional UPI payment gateway account

Step to Implement UPI Payment in Flutter App

Step 1) : project with the plugin upi_india add this dependency in pubspec.yaml file

upi_india: ^any

Step2) :Run the flutter pub get command to install the plugin after adding the dependency.

Step 3) : import the package named upi_India

We must import the upi_india package in our Dart file after adding the upi_india plugin to the project.

import 'package:upi_india/upi_india.dart';

Step 4) : Initialize UpiIndia

The UpiIndia object needs to be initialized with the necessary configuration information after the upi_india package has been imported. The amount to be transferred, the recipient’s UPI ID, the transaction note, and the payment app the user will use to complete the transaction are all included in the configuration information. An illustration of how to initialize UpiIndia is given below:

UpiIndia upi = UpiIndia();
await upi.initiateTransaction(
  amount: "10.0",
  app: UpiApp.amazonPay,
  receiverName: "Name",
  receiverUpiAddress: "example@upi",
  transactionNote: "Test Transaction",
);

The UpiIndia object is initialized in the code above with a payment of 10 INR to be sent to example@upi. The payment will be made using the Amazon Pay app, and the transaction note is set to “Test Transaction”.

Step 5) : Handle the UPI payment response

The user will be redirected to the UPI payment gateway app to complete the transaction after the payment has been started. The user will be directed back to our app once the transaction has been completed. To ascertain whether the transaction was successful or not, we must manage the UPI payment response. The onTransactionResponse and onTransactionError listeners of the UpiIndia object can be used to handle the payment response. An example of how to handle the payment response is given below:

upi.onTransactionResponse.listen((transactionDetails) {
  print("Transaction done successfully :: ${transactionDetails.toString()}");
});

upi.onTransactionError.listen((error) {
  print("Transaction failed with this error :: ${error.toString()}");
});

In the code above, we are watching for the UpiIndia object’s onTransactionResponse and onTransactionError events. The onTransactionResponse event is triggered if the transaction is successful.

Finally, call the initiateTransaction method when the user clicks on the payment button:

onPressed: () {
    initiateTransaction();
  },
  child: Text("Pay with UPI"),

That’s it! You have successfully integrated the UPI payment gateway in your Flutter app using the upi_india plugin.

For full example code , click here…..

Conclusion

Additionally, the upi_india plugin offers a getAllUpiApps method that can be used to obtain a list of all UPI apps that are currently installed on the user’s device. This method is helpful for showing the user a list of UPI apps and allowing them to select their preferred app for the transaction.

It is more assential part of flutter application to integratr UPI payment gateway in online mode.

The upi_india plugin handles UPI transactions and provides callbacks for transaction success and failure, making it simple to integrate the UPI payment gateway in a Flutter app.

Thanks for reading this article 💙…..

Have a beautiful day….

javascript random number between 1 and 10 or any range

0
js random number between 1 and 10
js random number between 1 and 10

Hi Guys, Welcome to Proto Coders Point, In this short article let’s checkout how to generate a random number between 1 and 10 or between any range you want iun javascript.

In JavaScript, To generate a random number between 1 and 10, we can make use of Math.random() function and by applying a bit of math operation.

Basically ‘Math.random()’ function will generate a random decimal floating number range from 0.0 to 1.0 and thus we need to multiple the generate number by 10 and round it to the nearst integer number by using Math.floor() function.

Here is a example

var rand = Math.random();
console.log(rand);  //0.7520321036592579


let random_number = Math.floor(rand * 10) + 1;
console.log(random_number); // 8

In above code, Math.random() function will generate random number from 0.0 to 1.0 [ 0.7520… is the generated number], then to make it range between 1 – 10 I am multiply it with 10 and add 1 to it because math.floor will round to 1 – 9.


Javascript random number between 1 and 100

Now say you want to generate a number between 1 to 100, so can we acheived by just modifying the scaling and shift value i.e just be multiplying the random number generated by 100 and adding 1 to it . For example:

let randomNumber = Math.floor(Math.random() * 100) + 1;
console.log(randomNumber);

random number between 0 and 1 python

0
python random between 0 and 1
python random between 0 and 1

Hi Guys, Welcome to Proto Coders Point, This short article on “Python random between 0 and 1”.

In Computer Science, Random Number Generation is key concept. While building any kind of application there is a need somewhere of random number between 0 and 1 python, might be for simulate events, encrypt data, statistical analysis.

In Python to generate a random number between 0 and 1, we can make use of python ‘random’ module

random in python

The `random` module in python is a built-in library that provide a functionality like generating random number. To generate a random number between 0 and 1, we can use random() function, which will return a decimal point number range from 0.0 to 1.0.

random number between 0 and 1 python example

import random

random_number = random.random()
print(random_number) // 0.8046595183024017

As you can see, In above code I have firstly imported random module then used to generate a random number from 0.0 to 1.0 and thus the random number is printed in console using print().