플러터 순한맛 강좌 - 스낵바와 BuildContext

원종인·2022년 7월 1일
0

Flutter 공부

목록 보기
3/13

해당 코드는 코딩 셰프의 유튜브 영상을 기반으로 작성되었습니다
플러터(flutter) 순한맛 강좌 18 | 스낵바(Snack bar)와 BuildContext

이번 스낵바와 BuildContext는 솔직히 봐도 잘 이해가 안간다. 이 수업은 여러번 둘러봐야할 것 같다.
또한 해당 영상이 2020년에 찍은 터라 이전 글도 그렇고 지금 글도 그렇고 경고가 발생하는 부분이 있다. 해당 코드도 FlatButton이 이제는 서비스 하지 않아 오류가 나타나지만 이는 무시하고 진행했다. 그래도 정상작동은 하기 때문이다. 해당 수업이 우선 기본을 쌓는 것이기에 나는 무시하고 넘어갔지만 이 부분이 싫다면 추가적으로 찾아가면서 해야할 것이다.

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'SnackBar',
      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('Snack Bar'),
          centerTitle: true,
        ),
        body: Builder(
          builder: (BuildContext ctx) {
            return Center(
              child: FlatButton(
                child: Text(
                  'Show me',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
                color: Colors.red,
                onPressed: () {
                  Scaffold.of(ctx).showSnackBar(SnackBar(
                    content: Text('Hellow'),
                  ));
                },
              ),
            );
          },
        ));
  }
}
profile
아직 대학생입니다

0개의 댓글