Detailed explanation of Flutter’s oktoast plug-in


Scan the castle for surprises

Introduction

oktoast is a Flutter library that provides a simple and easy-to-use way to display lightweight Toast messages (similar to Toast in Android). Toast messages are usually used to display temporary notifications or prompts on the screen to provide brief information feedback to users. The oktoast plug-in
OKToast is a flutter third-party library written purely in Dart, and does not require context for calls.

Detailed introduction

The following is some detailed introduction about flutter_oktoast:

Install and import

You can install the flutter_oktoast library by adding oktoast dependency in pubspec.yaml file.

dependencies:
  oktoast: ^3.3.1

Then pub upgrade

import

Then, in the file where you need to use oktoast, import the library.

import 'package:oktoast/oktoast.dart';

Put a layer of OKToast component outside MaterialApp

    return OKToast(
      child: MaterialApp(
        home: MyTabPage(),
      ),
    );

Why wrap MaterialApp?

Because this ensures that the toast can be displayed in front of all other controls.
The context can be cached so that it can be called from anywhere without passing in the context.

Display Toast message:

Using flutter_oktoast is very simple. You can use the showToast method to display Toast messages, which accepts a Widget as a parameter to specify the Toast content to be displayed.

showToast('content');

// position and second have default value, is optional

showToastWidget(Text('hello oktoast'));

The effect is as shown in the figure:

Insert image description here

Advanced use

Toast location

flutter_oktoast supports displaying Toast messages at different locations on the screen. You can use the position attribute to specify the position of the Toast. Optional positions include ToastPosition.top, ToastPosition.center, and ToastPosition.bottom.

showToast(
  Text('This is a Toast message'),
  position: ToastPosition.bottom,
);

Toast duration

You can set the duration of the Toast message through the duration attribute. By default, Toast messages will disappear automatically after 2 seconds. You can use the Duration class to specify the duration, for example Duration(seconds: 3) means 3 seconds.

showToast(
  Text('This is a Toast message'),
  duration: Duration(seconds: 3),
);

Custom Toast style

oktoast also provides some methods to customize the style of Toast. You can use the OkToast widget to wrap your application root widget and use the OkToast` properties to set the global Toast style.

    return OKToast(
      backgroundColor: Colors.black.withOpacity(0.8),
      textPadding: EdgeInsets.all(16.0),
      position: ToastPosition.bottom,
      duration: Duration(seconds: 30),

      child: MaterialApp(
        home: MyTabPage(),
      ),
    );

In the above example, some global Toast style properties are set, such as background color, text padding, border radius, display position, and duration.

The effect is as follows:
Insert image description here

Advanced usage

oktoast also provides some other advanced usage, such as displaying custom widgets in Toast messages, setting the offset of Toast messages, adding masks above Toast messages, etc. You can check out oktoast`s documentation and sample code for more details and usage examples.

scenes to be used

People who are familiar with Android development may not be familiar with toast. Here is a description of the usage scenarios of oktoast in Flutter applications for those without Android development experience:

Prompt message

oktoast can be used to display temporary prompt messages, such as successful operations, network connection problems, data saving, etc. It can display messages in different locations on the screen to attract the user's attention and provide instant feedback.

form validation

When the user submits the form, you can use oktoast to display the results of the form validation. For example, in a login form, you can use oktoast to display a message whether the username or password entered is valid or incorrect.

Operation feedback

When the user performs an operation, you can use oktoast to display the result or status of the operation. For example, in a file upload operation, you can use oktoast to display a message indicating success or failure of the upload.

Network request status

When an application makes a network request, you can use oktoast to display the status of the request, such as loading, loading completed, or loading failed messages.

debugging information

During the development and debugging phases, you can use oktoast to display debugging information, such as the values ​​of variables, execution results of methods, etc., to help developers quickly identify problems and debug code.

summary

The above are some common usage scenarios of oktoast. Of course it has many more uses.

Summarize

flutter_oktoast is a convenient and easy-to-use library for displaying Toast messages in Flutter applications. It provides a simple API to display Toast messages and supports custom styles and positions. By using oktoast, you can easily provide short notifications and prompts to your users.

Guess you like

Origin blog.csdn.net/yikezhuixun/article/details/132874380