How to create Toast in Flutter?

前端 未结 28 810
小蘑菇
小蘑菇 2020-12-12 10:46

Can I create something similar to Toasts in Flutter? Just a tiny notification window that is not directly in the face of the user and does not lock or fade the view behind i

28条回答
  •  醉酒成梦
    2020-12-12 11:10

    There is a three-way to show toast on flutter App.
    I will tell you about all three way that I know and choose which one you want to use. I would recommend the second one.

    1: using of the external package.

    this is the first method which is the easiest way to show toast on flutter app.

    first of all, you have to add this package to pubspec.YAML

    flutter_just_toast:^version_here
    

    then import the package in the file where you want to show a toast.

    'package:flutter_just_toast/flutter_just_toast.dart';
    

    and the last step shows the toast.

    Toast.show( message: "Your toast message", 
               duration: Delay.SHORT, 
               textColor: Colors.black);
    

    2 : using official way.

    this method is also simple but you have to deal with it. I am not saying that it is hard it is simple and clean I would recommend this method.

    for this method, all you have to do show toast is using the below code.

    Scaffold.of(context).showSnackBar(SnackBar(
              content: Text("Sending Message"),
            ));
    

    but remember that you have to use the scaffold context.

    3: using native API.

    now, this method does not make sense anymore when you already have the two methods above. using this method you have to write native code for android and iOS and then convert it to plugin and you are ready to go. this method will consume your time and you have to reinvent the wheel. which has already been invented.

提交回复
热议问题