본문 바로가기

Dead Code/Flutter_Dart

[플러터] SnackBar / AlertDialog

 

Flutter 2.0부터 Scaffold.of(context).showSnackBar 는 deprecated.

ScaffoldMessenger.of(context).showSnackBar 로 변경하여 사용하는데, 더 이상 Builder를 따로 쓰지 않아도 되는 듯하다.

 

//button1
ElevatedButton(
  child: const Text('Show SnackBar'),
  onPressed: () {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: const Text('this is snackbar'),
        action: SnackBarAction(
            label: 'Undo',
            onPressed: () {
              // tab snackbar
            }),
      ),
    );
  },
),

 

 

오랜만에 AlertDialog

 

//button2
ElevatedButton(
  child: Text('Show Dialog'),
  onPressed: () {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          content: Text('my dialog'),
          actions: [
            TextButton(                          
                child: Text('Button1'),
                onPressed: () {
                  print(dialogKey);
                }),
            TextButton(child: Text('Button2'), onPressed: () {}),
          ],
        );
      },
    );
  },
),

 

 

음 기억이 가물가물하다.