Created with <3 with dartpad.dev.
Last active
January 26, 2024 21:14
-
-
Save hardy716/ce93c31112cc11222af92b2e6fdfb649 to your computer and use it in GitHub Desktop.
플러터챌린지 4일차 기본문제 - 신현호
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'HelloTextField', | |
debugShowCheckedModeBanner: false, | |
home: HelloTextFieldScreen(), | |
); | |
} | |
} | |
class HelloTextFieldScreen extends StatefulWidget { | |
@override | |
State<HelloTextFieldScreen> createState() => _HelloTextFieldScreenState(); | |
} | |
class _HelloTextFieldScreenState extends State<HelloTextFieldScreen> { | |
late final FocusNode leftFocusNode; | |
late final FocusNode rightFocusNode; | |
late final TextEditingController leftController; | |
late final TextEditingController rightController; | |
KeyEventResult _onKey(FocusNode node, RawKeyEvent event, TextEditingController controller) { | |
if (event is RawKeyDownEvent) { | |
FocusNode oppNode = (node == leftFocusNode) ? rightFocusNode : leftFocusNode; | |
bool isBackSpace = (event.logicalKey == LogicalKeyboardKey.backspace); | |
bool isTextEmpty = (controller.text.isEmpty == true); | |
if (isBackSpace && isTextEmpty) { | |
oppNode.requestFocus(); | |
return KeyEventResult.handled; | |
} | |
return KeyEventResult.ignored; | |
} | |
return KeyEventResult.ignored; | |
} | |
@override | |
void initState() { | |
super.initState(); | |
leftController = TextEditingController(text: "Hello"); | |
rightController = TextEditingController(text: "FlutterBoot!"); | |
leftFocusNode = FocusNode(onKey: (node, event) => _onKey(leftFocusNode, event, leftController)); | |
rightFocusNode = FocusNode(onKey: (node, event) => _onKey(rightFocusNode, event, rightController)); | |
WidgetsBinding.instance.addPostFrameCallback((timeStamp) { | |
rightFocusNode.requestFocus(); | |
}); | |
} | |
@override | |
void dispose() { | |
leftController.dispose(); | |
rightController.dispose(); | |
leftFocusNode.dispose(); | |
rightFocusNode.dispose(); | |
super.dispose; | |
} | |
@override | |
Widget build(BuildContext context) { | |
InputBorder border = UnderlineInputBorder( | |
borderSide: BorderSide(color: Theme.of(context).colorScheme.primary, width: 1.5), | |
); | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Hello TextField!'), | |
), | |
body: Center( | |
child: Row( | |
children: [ | |
const SizedBox(width: 10), | |
Flexible( | |
child: TextField( | |
focusNode: leftFocusNode, | |
controller: leftController, | |
onEditingComplete: () { | |
rightFocusNode.requestFocus(); | |
}, | |
), | |
), | |
const SizedBox(width: 20), | |
Flexible( | |
child: TextField( | |
autofocus: true, | |
focusNode: rightFocusNode, | |
controller: rightController, | |
onEditingComplete: () { | |
leftFocusNode.requestFocus(); | |
}, | |
decoration: InputDecoration( | |
focusedBorder: border, | |
enabledBorder: border, | |
disabledBorder: border, | |
errorBorder: border, | |
focusedErrorBorder: border, | |
), | |
), | |
), | |
const SizedBox(width: 10), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment