Flutter_20. TextField with Button, keyboardType, FocusScope, SnackBar
์ด๋ฒ ํฌ์คํ ์์๋ Button๊ณผ ํจ๊ป ์ฌ์ฉํ๋ TextField๋ฅผ ๋ฉ๋ชจํด๋ณด๋ ค ํ๋ค.
๋ฒํผ์ ํด๋ฆญํ๋ฉด, ์ ๋ ฅํ ๊ฐ์ SnackBar๋ก ์ถ๋ ฅํด๋ณด๊ฒ ๋ค.
2023.05.02 - [๊ฐ๋ฐํ๊ธฐ] - Flutter_07. ScaffoldMessenger, SnackBar, Function
Flutter_07. ScaffoldMessenger, SnackBar, Function
ํ๋ฉด ํ๋จ์ ์ฑ ํ๋ก์ธ์ค์ ๋ํ ๊ฐ๋ตํ ๋ฉ์์ง๋ฅผ ์ ๊ณตํ๋ SnackBar๋ฅผ ๋ฉ๋ชจํด๋ณด๊ฒ ๋ค. ์์ธํ ๋ด์ฉ์ ์๋ ๋งํฌ์์ ํ์ธ! https://docs.flutter.dev/cookbook/design/snackbars Display a snackbar How to implement a snackba
uraon.tistory.com
TextField, Button, SnackBar
1) ๋ณ์ ์ ์ธ ๋ฐ ์ด๊ธฐํ
class _HomeState extends State<Home> {
// Field
late TextEditingController textEditingController;
late String inputValue;
// Init
@override
void initState() {
super.initState();
textEditingController = TextEditingController();
inputValue = '';
}
// Dispose
@override
void dispose() {
textEditingController.dispose();
super.dispose();
}
2) TextField
โ TextField
- controller : TextEditingController
- decoration : InputDecoration(border, labelText)
- keyboardType : TextInputType
//- obscureText : ๋ฌธ์ ์จ๊น ์ฒ๋ฆฌ(ex ๋น๋ฐ๋ฒํธ ์ ๋ ฅ)
//- onSubmitted : (String value) async{ await... }
3) Button : TextEditingController.text.trim()
Case1. ํ ์คํธํ๋๊ฐ ๊ณต๋ฐฑ์ธ ๊ฒฝ์ฐ
Case2. ํ ์คํธํ๋์ ์ ๋ ฅ๋ ๊ฐ์ด ์์ง๋ง, ๋ฌธ์๊ฐ ์๋ ๊ฒฝ์ฐ(ex. ์คํ์ด์ค๋ฐ : " abc")
Case3. ํ ์คํธํ๋์ ์ ๋ ฅ๋ ๊ฐ์ด ์ ์์ ์ผ๋ก ์๋ ๊ฒฝ์ฐ
---------
- Case1 ๋๋ Case2์ ๊ฒฝ์ฐ, errorSnackBar(context) ํจ์ ํธ์ถ
- Case3์ ๊ฒฝ์ฐ, showSnackBar(context) ํจ์ ํธ์ถ
#์ฐธ๊ณ : https://api.flutter.dev/flutter/dart-core/String/trim.html
trim method - String class - dart:core library - Dart API
trim abstract method String trim() The string without any leading and trailing whitespace. If the string contains leading or trailing whitespace, a new string with no leading and no trailing whitespace is returned: final trimmed = '\tDart is fun\n'.trim();
api.flutter.dev
4) SnackBar Function ์ ์
โ ScaffoldMessenger.of(context).showSnackBar
SnackBar
- content: ๋ฌธ๊ตฌ,
- duration: ์ถ๋ ฅ ์ง์ ์๊ฐ
- backgroundColor: ์ค๋ต๋ง ์นผ๋ผ,
// --- Func SnackBar
showSnackBar(BuildContext context){
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('์
๋ ฅํ ๊ธ์๋ $inputValue ์
๋๋ค.'),
duration: const Duration(seconds: 2),
backgroundColor: Colors.blue,
)
);//ํจ์๋๊น ";" ์ถ๊ฐ!
}
errorSnackBar(BuildContext context){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('๊ธ์๋ฅผ ์
๋ ฅํ์ธ์.'),
duration: Duration(seconds: 2),
backgroundColor: Colors.red,
)
);//ํจ์๋๊น ";" ์ถ๊ฐ!
}
5) ์ ์ฅํ๊ณ ๊ธฐ๋ฅ ํ์ธ
keyboard, FocusScope
ํค๋ณด๋ ์ถ๋ ฅ ํ(ํค๋ณด๋ ์ถ๋ ฅ : Command + K), ํค๋ณด๋ ์ธ์ ๋ค๋ฅธ ์์ญ์ ํด๋ฆญํด๋ ํค๋ณด๋๋ ๋ด๋ ค๊ฐ์ง ์๋๋ค.
์ด๋ด๋ FocusScope๋ฅผ ์ฌ์ฉํ์ฌ, ํค๋ณด๋ ์ธ ๋ค๋ฅธ ์์ญ์ ํด๋ฆญ ํ ๊ฒฝ์ฐ ํค๋ณด๋๊ฐ ๋ด๋ ค๊ฐ๋๋ก ํ ์ ์๋ค.
https://api.flutter.dev/flutter/widgets/FocusScope-class.html
FocusScope class - widgets library - Dart API
A FocusScope is similar to a Focus, but also serves as a scope for its descendants, restricting focus traversal to the scoped controls. For example a new FocusScope is created automatically when a route is pushed, keeping the focus traversal from moving to
api.flutter.dev
1) Scaffold์ Widget ์ถ๊ฐ
2) GestureDetector : onTap์ผ๋ก FocusScope
FocusScope.of(context).unfocus();
3) ์ ์ฅํ๊ณ ๊ธฐ๋ฅ ํ์ธ