当应用程序在Flutter中打开时,如何显示快餐栏或底部工作表? [英] How to show a Snackbar or bottom sheet when the app opens in Flutter?

查看:76
本文介绍了当应用程序在Flutter中打开时,如何显示快餐栏或底部工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个要求,我需要在应用程序启动时显示 Snackbar ,但是当我尝试这样做时,它会给我上下文错误。我怎么知道我的窗口小部件树已构建并使用上下文显示小吃栏

So I have a requirement, where I need to show the Snackbar just when the application starts, but when I try to do it, it gives me context error. How can I know that my Widget tree has been built and use the context to display the Snackbar.

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    Scaffold.of(context).showSnackBar(SnackBar(content: Text('Welcome User')));
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[],
        ),
      ),
    );
  }
}


推荐答案

创建 GlobalKey 。 flutter.io/flutter/material/ScaffoldState-class.html rel = noreferrer> ScaffoldState ,以确保您定位正确的脚手架以在其上显示SnackBar。

Create a GlobalKey of type ScaffoldState to make sure you target the right Scaffold to show a SnackBar on.

将目标脚手架的密钥设置为已创建的密钥。

Set your target Scaffold's key to the key you have created.

在initState()函数中,使用 WidgetsBinding 类和 addPostFrameCallback 在构建布局时执行您的代码。在回叫中显示您的SnackBar。

In your initState() function use WidgetsBinding class and addPostFrameCallback to execute your code when the layout is built. Show your SnackBar in the call back.

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) => _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text('Welcome User'))));
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[],
        ),
      ),
    );
  }
}

这篇关于当应用程序在Flutter中打开时,如何显示快餐栏或底部工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆