如何在Flutter中创建Toast?

如何在Flutter中创建Toast?,第1张

如何在Flutter中创建Toast?

您可以

ScaffoldState
使用
Scaffold.of(context)

然后做类似的事情

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

小吃店是材料设计中的官方“吐司”。参见https://material.io/design/components/snackbars.html#usage

这是一个完整的示例

import 'package:flutter/material.dart';void main() {  runApp(MyApp());}class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      home: const Home(),    );  }}class Home extends StatelessWidget {  const Home({    Key key,  }) : super(key: key);  @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: const Text('Snack bar'),      ),      /// We use [Builder] here to use a [context] that is a descendant of [Scaffold]      /// or else [Scaffold.of] will return null      body: Builder(        builder: (context) => Center(   child: RaisedButton(     child: const Text('Show toast'),     onPressed: () => _showToast(context),   ), ),      ),    );  }  void _showToast(BuildContext context) {    final scaffold = Scaffold.of(context);    scaffold.showSnackBar(      SnackBar(        content: const Text('Added to favorite'),        action: SnackBarAction( label: 'UNDO', onPressed: scaffold.hideCurrentSnackBar),      ),    );  }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5002868.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存