꿈꾸는 시스템 디자이너

Flutter Example - SnackBar Widget 본문

Tutorial/Flutter with App

Flutter Example - SnackBar Widget

독행소년 2019. 9. 26. 11:53
import 'package:flutter/material.dart';

SnackBarWidgetState pageState;

class SnackBarWidget extends StatefulWidget {
  @override
  SnackBarWidgetState createState() {
    pageState = SnackBarWidgetState();
    return pageState;
  }
}

class SnackBarWidgetState extends State<SnackBarWidget> {
  // Global Key of Scaffold
  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      appBar: AppBar(title: Text("SnackBar Demo")),
      body: ListView(
        children: <Widget>[
          RaisedButton(
            child: Text("Show Snackbar - call directly"),
            onPressed: () {
              scaffoldKey.currentState.showSnackBar(
                SnackBar(
                  content: Text("Did you call me?"),
                  backgroundColor: Colors.orange,
                  action: SnackBarAction(
                    label: "Done",
                    textColor: Colors.white,
                    onPressed: () {},
                  ),
                ),
              );
            },
          ),
          RaisedButton(
            child: Text("Show Snackbar - with method"),
            onPressed: () {
              showSnackbarWithKey();
            },
          ),
          RaisedButton(
            child: Text("Show Snackbar - with other class"),
            onPressed: () {
              SnackBarManager.showSnackBar(scaffoldKey, "Hello");
            },
          ),
        ],
      ),
    );
  }

  showSnackbarWithKey() {
    scaffoldKey.currentState.showSnackBar(
      SnackBar(
        content: Text("Did you call me?"),
        backgroundColor: Colors.blue,
        action: SnackBarAction(
          label: "Done",
          textColor: Colors.white,
          onPressed: () {},
        ),
      ),
    );
  }
}

class SnackBarManager {
  static void showSnackBar(
      GlobalKey<ScaffoldState> scaffoldKey, String message) {
    scaffoldKey.currentState.showSnackBar(
      SnackBar(
        content: Text("Did you call me?"),
        backgroundColor: Colors.red,
        action: SnackBarAction(
          label: "Done",
          textColor: Colors.white,
          onPressed: () {},
        ),
      ),
    );
  }
}

 

 

▶ Go to Table of Contents | 강의 목차로 이동


※ This example is also available in the Flutter Code Examples app. | 본 예제는 Flutter Code Examples 앱에서도 제공됩니다.

 

Flutter Code Examples - Google Play 앱

Are you a beginner at Flutter? Check out the various features of Flutter through the demo. Source code for all demos is also provided.

play.google.com

Comments