Last active
May 22, 2020 17:45
-
-
Save harkairt/62fad889d58a61ceefde8d05e24d0c1b to your computer and use it in GitHub Desktop.
selection issue
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'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
var controller = TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(''), | |
), | |
body: Builder( | |
builder: (context) { | |
return TextField( | |
controller: controller, | |
keyboardType: TextInputType.number, | |
onChanged: (value) { | |
debugPrint('onChange called with: $value'); | |
var validatedValue = value.replaceAll(RegExp('[^0-9]'), ''); | |
controller.text = validatedValue; | |
controller.selection = TextSelection.collapsed(offset: validatedValue.length); | |
debugPrint('cursor position set at: ${validatedValue.length}'); | |
}, | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment