π¬ μ€λ΅λ°
νλ©΄ νλ¨λΆμ λ©μΈμ§μ°½μ λμ°λ κ²
πββοΈ ν μ€νΈμ°½κ³Ό μ°¨μ΄μ μ?
μ€λ΅λ°λ μμΉκ° μμ§μ΄μ§λ§,
ν μ€νΈμ°½μ κ²½μ° μμΉκ° νλ¨λΆμ κ³ μ
κΈ°μ‘΄ (#18κ°μ)
Scanffold.of(context)
-> νμ¬μ BuildContext μμ μλ‘ κ±°μ¬λ¬ μ¬λΌκ° κ°μ₯ κ°κΉμ΄ Scanffold μμ ―μ λ°ννλΌλ κ²
μ΄ κ³Όμ μμ Scanffold.of(context) κ° κ°κ³ μλ νμ¬μ Context λ‘λ Scanffold λ₯Ό μ°Ύμ μ μμ
-> μ΄λ₯Ό ν΄κ²°νκΈ° μν΄ Builder Widget μ μμ±ν΄ μλ‘μ΄ Context λ₯Ό μ λ¬λ°μ κ·Έ μμΉμμλΆν° Widget Tree μ κ·Έ Builder μμ ― μμ μμΉνκ³ μλ Scanffold μμ ―μ μ°Ύμλκ°κ³ μν¨
ScaffoldMessenger.of(context)
-> κ°μ₯ κ°κΉμ΄ ScaffoldMessenger μ μ°Ύμ λ°νScaffoldMessenger
-> λͺ¨λ μμ Scaffold λ€μ΄ λ±λ‘λμ΄ μμ΄ κ°μ₯ κ°κΉμ΄ Scaffold λ₯Ό μ°Ύμ λ°νν΄μ€import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyPage(),
);
}
}
class MyPage extends StatelessWidget {
const MyPage({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Scanffold Messenger - Main'),
),
body: HomeBody(),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.thumb_up),
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Like ad new Snack bar!'),
duration: Duration(seconds: 5),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => ThirdPage()));
},
),
),
);
},
),
);
}
}
class HomeBody extends StatelessWidget {
const HomeBody({super.key});
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: Text('GO to the secod page'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);
},
),
);
}
}
class SecondPage extends StatelessWidget {
const SecondPage({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: Text(
' 2. μ’μμ!',
style: TextStyle(
fontSize: 20.0,
color: Colors.redAccent,
),
),
),
);
}
}
class ThirdPage extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Third Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'3. μ’μμλ₯Ό μ·¨μνμκ² μ΅λκΉ?',
style: TextStyle(
fontSize: 20.0,
color: Colors.redAccent,
),
),
ElevatedButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('μ’μμκ° μ·¨μλμμ΅λλ€.'),
duration: Duration(seconds: 3),
),
);
},
child: Text('μ·¨μ'),
)
],
),
),
);
}
}
μ΄λ ν νμ΄μ§μμλ§ μ€λ΅λ°λ₯Ό 보μ¬μ£Όκ³ λ€λ₯Έ νλ©΄μΌλ‘ λμ΄κ°μ μ€λ΅λ°λ₯Ό μμ λ €λ©΄?
class ThirdPage extends StatelessWidget {
Widget build(BuildContext context) {
return ScaffoldMessenger(
child: Scaffold(
appBar: AppBar(
title: Text('Third Page'),
),
body: Builder(
builder: (context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'3. μ’μμλ₯Ό μ·¨μνμκ² μ΅λκΉ?',
style: TextStyle(
fontSize: 20.0,
color: Colors.redAccent,
),
),
ElevatedButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('μ’μμκ° μ·¨μλμμ΅λλ€.'),
duration: Duration(seconds: 3),
),
);
},
child: Text('μ·¨μ'),
)
],
),
);
},
),
),
);
}
}
μ€λ΅λ° μ€νμΌλ§
ElevatedButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'μ’μμκ° μ·¨μλμμ΅λλ€.',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
),
),
backgroundColor: Colors.teal,
duration: Duration(seconds: 3),
),
);
},
child: Text('μ·¨μ'),
),
π ν μ€νΈμ°½
β μλμ κ°μ΄ μ½λ μμ± λ° import
νλ¬ν° ν μ€νΈμ°½ μ΄μ => λ²μ μ€λ₯
ν μ€νΈμ°½ λ²μ μ€λ₯ ν΄κ²°
ElevatedButton(
onPressed: () {
showToast();
},
child: Text('Toast'),
style: ElevatedButton.styleFrom(
primary: Colors.orange,
),
),
void showToast() {
Fluttertoast.showToast(
msg: 'This is Toast!!',
gravity: ToastGravity.BOTTOM,
toastLength: Toast.LENGTH_SHORT,
);
}