728x90
반응형
기존 스낵바 적용 방법은 해당 코드 처럼 적용을 해주면 됐었다.
Scaffold.of(context).showSnackBar(SnackBar(
content: const Text('snack'),
duration: const Duration(seconds: 1),
action: SnackBarAction(
label: 'ACTION',
onPressed: () { },
),
));
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
Scaffold(
key: scaffoldKey,
body: ...,
);
scaffoldKey.currentState.showSnackBar(mySnackBar);
-->새로운 SnackBar ( 스낵바 ) 적용방법
1. ScaffoldMessenger 사용
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text('snack'),
duration: const Duration(seconds: 1),
action: SnackBarAction(
label: 'ACTION',
onPressed: () { },
),
));
2. GlobalKey<ScaffoldMessengerState> 사용 (ScaffoldMessenger)
final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
ScaffoldMessenger(
key: scaffoldMessengerKey,
child: ...
)
scaffoldMessengerKey.currentState.showSnackBar(mySnackBar);
scaffoldMessengerKey.currentState.hideCurrentSnackBar(mySnackBar);
scaffoldMessengerKey.currentState.removeCurrentSnackBar(mySnackBar);
3. GlobalKey<ScaffoldMessengerState> 사용 2 (MeterialApp)
final GlobalKey<ScaffoldMessengerState> rootScaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
MaterialApp(
scaffoldMessengerKey: rootScaffoldMessengerKey,
home: ...
)
rootScaffoldMessengerKey.currentState.showSnackBar(mySnackBar);
rootScaffoldMessengerKey.currentState.hideCurrentSnackBar(mySnackBar);
rootScaffoldMessengerKey.currentState.removeCurrentSnackBar(mySnackBar);
출처
https://docs.flutter.dev/release/breaking-changes/scaffold-messenger
728x90
반응형
'Flutter' 카테고리의 다른 글
[j Flutter] Getx 사용해 BottomNavigationBar 만들기 (0) | 2021.11.09 |
---|---|
[j Flutter] flutter 2.0 null safety 버전 업그레이드 (0) | 2021.10.28 |
[j Flutter] local_auth을 사용하여 지문 인식하기 (0) | 2021.08.18 |
[j Flutter] apk파일 만들기 (0) | 2021.06.24 |
[Flutter] Error: The method ‘inheritFromWidgetOfExactType’ isn’t defined for the class ‘BuildContext’ 에러 해결하기 (0) | 2021.06.11 |