Skip to content

Instantly share code, notes, and snippets.

@wisnuwiry
Created November 10, 2021 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wisnuwiry/4ac3f2a77b277deebd6ff76a37e007aa to your computer and use it in GitHub Desktop.
Save wisnuwiry/4ac3f2a77b277deebd6ff76a37e007aa to your computer and use it in GitHub Desktop.
Flutter Example Input Auto Uppercase
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Please Input Text:'),
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input Label',
),
inputFormatters: [UpperCaseTextFormatter()],
)
],
),
),
);
}
}
class UpperCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: newValue.text.toUpperCase(),
selection: newValue.selection,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment