Created
June 12, 2021 23:37
-
-
Save diegoveloper/2f44d98d65187d6d359d8f5100901c0c to your computer and use it in GitHub Desktop.
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(MyApp()); | |
} | |
/// this is comment | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Learning', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
// ignore: public_member_api_docs | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> implements TextInputClient { | |
TextEditingValue _editingValue; | |
FocusNode _focusNode; | |
TextInputConnection _connection; | |
bool get _hasInputConnection => _connection != null && _connection.attached; | |
void requestKeyboard() { | |
if (_focusNode.hasFocus) { | |
_openInputConnection(); | |
} else { | |
FocusScope.of(context).requestFocus(_focusNode); | |
} | |
} | |
void _openInputConnection() { | |
if (!_hasInputConnection) { | |
_connection = TextInput.attach( | |
this, | |
TextInputConfiguration( | |
inputType: TextInputType.number, | |
autocorrect: false, | |
inputAction: TextInputAction.done, | |
), | |
); | |
_connection.setEditingState(_editingValue); | |
} | |
_connection.show(); | |
} | |
void _closeInputConnectionIfNeeded() { | |
if (_hasInputConnection) { | |
_connection.close(); | |
_connection = null; | |
} | |
} | |
void _onFocusChanged() { | |
if (_focusNode.hasFocus) { | |
_openInputConnection(); | |
} else { | |
_closeInputConnectionIfNeeded(); | |
} | |
setState(() {}); | |
} | |
@override | |
void initState() { | |
_editingValue = TextEditingValue.empty; | |
_focusNode = FocusNode(); | |
_focusNode.addListener(_onFocusChanged); | |
WidgetsBinding.instance.addPostFrameCallback( | |
(_) => requestKeyboard(), | |
); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
_focusNode?.dispose(); | |
_closeInputConnectionIfNeeded(); | |
super.dispose(); | |
} | |
String _formatNumber(String value) => value.padLeft(2, '0'); | |
@override | |
Widget build(BuildContext context) { | |
final value = int.tryParse(_editingValue.text); | |
String first = '00'; | |
String decimal = '00'; | |
if (value != null && value > 100) { | |
first = _formatNumber((value ~/ 100).toString()); | |
decimal = _formatNumber((value % 100).toString()); | |
} else if (value != null) { | |
decimal = _formatNumber(value.toString()); | |
} | |
return Scaffold( | |
body: GestureDetector( | |
behavior: HitTestBehavior.opaque, | |
onTap: requestKeyboard, | |
child: Center( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text( | |
first, | |
style: TextStyle(fontSize: 40, color: Colors.black), | |
), | |
Padding( | |
padding: const EdgeInsets.only(bottom: 15.0), | |
child: Text( | |
decimal, | |
style: TextStyle(fontSize: 20, color: Colors.black), | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
@override | |
void connectionClosed() {} | |
@override | |
AutofillScope get currentAutofillScope => throw UnimplementedError(); | |
@override | |
TextEditingValue get currentTextEditingValue => _editingValue; | |
@override | |
void performAction(TextInputAction action) { | |
_focusNode.unfocus(); | |
} | |
@override | |
void updateEditingValue(TextEditingValue value) { | |
setState(() { | |
_editingValue = value; | |
if (value.text.length == 12) { | |
print('maximum length'); | |
} | |
}); | |
} | |
@override | |
void performPrivateCommand(String action, Map<String, dynamic> data) {} | |
@override | |
void showAutocorrectionPromptRect(int start, int end) {} | |
@override | |
void updateFloatingCursor(RawFloatingCursorPoint point) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment