gpt4 book ai didi

flutter - 在按下的按钮上推送通知 Flutter

转载 作者:行者123 更新时间:2023-12-03 03:33:03 27 4
gpt4 key购买 nike

当按下“保存按钮”时,我想在屏幕上发送通知。
这很简单,没有 FireBase,没有 Internet……只是屏幕上显示“已完成”的简单通知。
我想尽可能地自定义它:屏幕上的位置、形状、颜色......
我找到了 SnackBar但不是我要找的。
这是我第一次处理通知,所以我知道一些事情......
我读到我需要一个 NotificationListener ,但目前,此通知必须生成 onTap所以我认为我不需要一直听到的东西......这是对资源的浪费。
编辑, pubspecs.yaml :

version: 1.0.0+1

environment:
sdk: ">=2.1.0 <3.0.0"

dependencies:
flutter:
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
flutter_svg: ^0.17.4
circular_profile_avatar: ^1.0.6
cloud_firestore: ^0.13.5
firebase_auth: ^0.16.0
firebase_core: ^0.4.3+3
firebase_messaging: ^6.0.13
firebase_storage: ^3.1.5
fluttertoast: ^5.0.1


provider: ^4.0.5

uuid: ^2.0.4
path_provider: ^1.6.5
flutter_image_compress: ^0.6.6+1
image_picker: ^0.6.7
cached_network_image: ^2.0.0

intl: ^0.16.1


dev_dependencies:
flutter_test:
sdk: flutter

最佳答案

您可以使用 fluttertoast flutter包裹。
添加依赖项 -
pubspec.yaml

fluttertoast: ^6.0.0
然后在需要的地方导入所需的库 -
import 'package:fluttertoast/fluttertoast.dart';
初始化 FlutterToast -
FlutterToast flutterToast;

@override
void initState() {
super.initState();
flutterToast = FlutterToast(context);
}
然后,您创建自己的自定义 toast 和调用 toast 的方法和按钮 onPressed回调您可以调用 _showToast() (它只是一个为处理 toast 容器创建和调用而创建的包装器),它将触发类似这样的 toast 通知-
_showToast() {
Widget toast = Container(
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
color: Colors.greenAccent,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.check),
SizedBox(
width: 12.0,
),
Text("This is a Custom Toast"),
],
),
);


flutterToast.showToast(
child: toast,
gravity: ToastGravity.BOTTOM,
toastDuration: Duration(seconds: 2),
);
}
这将根据您提供的参数弹出一条 toast 消息。
这是示例代码 -
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() {
//Run the App Widget
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Demo App",
home: HomeScreen(),
);
}
}

class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
FlutterToast flutterToast;

@override
void initState() {
super.initState();
flutterToast = FlutterToast(context);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Demo Notification"),
),
body: Container(),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.notifications),
onPressed: () {
_showToast();
},
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
);
}

_showToast() {
Widget toast = Container(
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
color: Colors.greenAccent,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.check),
SizedBox(
width: 12.0,
),
Text("This is a Custom Toast"),
],
),
);

flutterToast.showToast(
child: toast,
gravity: ToastGravity.CENTER,
toastDuration: Duration(seconds: 2),
);
}
}
输出-
Output
你可以阅读更多关于这个 here .

关于flutter - 在按下的按钮上推送通知 Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62748847/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com