flutter(7) - Builder를 이용한 Snackbar

less than 1 minute read

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Appbar',
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: MyPage(),
    );
  }
}

class MyPage extends StatelessWidget {
  const MyPage({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Appbar icon menu'),
        centerTitle: true,
      ),
      body: Builder(builder: (BuildContext ctx) {
        return Center(
        child: FlatButton( // Raised button, Floating action button과 디자인과 모양만 다르지 같은 기능을 한다.
          child: Text('Show me',
          style: TextStyle(
            color: Colors.white
            ),
          ),
          color: Colors.red,
          onPressed: () {
            Scaffold.of(ctx).showSnackBar(SnackBar(content: Text('Hello')));
          },
        ),
      );
      }, )
    );
  }
}

Leave a comment