返回介绍

Displaying SnackBars

发布于 2019-12-09 21:31:26 字数 4260 浏览 1100 评论 0 收藏 0

In some cases, it can be handy to briefly inform our users when certain actions take place. For example, when a user swipes away a message in a list, we might want to inform them the message has been deleted. We might even want to give them an option to undo the action!

In Material Design, this is the job of a SnackBar.

Directions

  1. Create a Scaffold
  2. Display a SnackBar
  3. Provide an additional action

1. Create a Scaffold

When creating apps that follow the Material Design guidelines, we’ll want to give our apps a consistent visual structure. In this case, we’ll need to display the SnackBar at the bottom of the screen, without overlapping other important Widgets, such as the FloatingActionButton!

The Scaffold Widget from the material library creates this visual structure for us and ensures important Widgets don’t overlap!

Scaffold(
  appBar: AppBar(
    title: Text('SnackBar Demo'),
  ),
  body: SnackBarPage(), // You'll fill this in below!
);

2. Display a SnackBar

With the Scaffold in place, you can display a SnackBar! First, you need to create a SnackBar, then display it using the Scaffold.

final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));

// Find the Scaffold in the Widget tree and use it to show a SnackBar
Scaffold.of(context).showSnackBar(snackBar);

3. Provide an additional action

In some cases, you might want to provide an additional action to the user when the SnackBar is displayed. For example, if they’ve accidentally deleted a message, we could provide an action to undo that change.

To achieve this, we can provide an additional action to the SnackBar Widget.

final snackBar = SnackBar(
  content: Text('Yay! A SnackBar!'),
  action: SnackBarAction(
    label: 'Undo',
    onPressed: () {
      // Some code to undo the change!
    },
  ),
);

Complete example

Note: In this example, the SnackBar displays when a user taps on a button. For more information on working with user input, please see the Gestures section of the Cookbook.

import 'package:flutter/material.dart';

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

class SnackBarDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SnackBar Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('SnackBar Demo'),
        ),
        body: SnackBarPage(),
      ),
    );
  }
}

class SnackBarPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: RaisedButton(
        onPressed: () {
          final snackBar = SnackBar(
            content: Text('Yay! A SnackBar!'),
            action: SnackBarAction(
              label: 'Undo',
              onPressed: () {
                // Some code to undo the change!
              },
            ),
          );

          // Find the Scaffold in the Widget tree and use it to show a SnackBar!
          Scaffold.of(context).showSnackBar(snackBar);
        },
        child: Text('Show SnackBar'),
      ),
    );
  }
}

SnackBar Demo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文