Skip to content

Instantly share code, notes, and snippets.

@imrhk
Created April 22, 2023 15:23
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 imrhk/dfa7fe31725ed7f5b44221760d1e4360 to your computer and use it in GitHub Desktop.
Save imrhk/dfa7fe31725ed7f5b44221760d1e4360 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:pattern_formatter/pattern_formatter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Hello World'),
),
body: const Center(child: CustomFormWidget()),
),
));
}
class CustomFormWidget extends StatefulWidget {
const CustomFormWidget({super.key});
@override
State<CustomFormWidget> createState() => _CustomFormWidgetState();
}
const _supportedCurrency = {'₹': 100000, '\$': 10000, '€': 800, '£': 600};
class _CustomFormWidgetState extends State<CustomFormWidget> {
var _selectedCurrency = _supportedCurrency.entries.first.key;
@override
Widget build(BuildContext context) {
final maxInput = ThousandsFormatter()
.formatEditUpdate(
const TextEditingValue(text: ''),
TextEditingValue(
text: _supportedCurrency[_selectedCurrency].toString()))
.text;
final dropdown = DropdownButton<String>(
items: {'₹', '\$', '€', '£'}
.map<DropdownMenuItem<String>>(
(e) => DropdownMenuItem(
value: e,
child: CircleAvatar(
backgroundColor: Colors.black,
child: Text(
e,
style: const TextStyle(color: Colors.white, fontSize: 24),
),
)),
)
.toList(),
value: _selectedCurrency,
icon: const Icon(
Icons.keyboard_arrow_down,
color: Colors.black,
size: 32,
),
onChanged: (value) {
if (value != null) {
setState(() {
_selectedCurrency = value;
});
}
},
);
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Enter amount',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.w700),
),
const SizedBox(
height: 20,
),
FractionallySizedBox(
widthFactor: 0.8,
child: TextFormField(
maxLines: 1,
decoration: InputDecoration(
prefix: dropdown,
contentPadding: const EdgeInsets.only(bottom: 10, right: 48),
),
style: const TextStyle(fontSize: 32),
textAlign: TextAlign.center,
inputFormatters: [
ThousandsFormatter(),
],
validator: (value) {
final newValue = value?.replaceAll(RegExp(r'^[0-9]'), '');
final intValue = int.tryParse(newValue ?? '0');
if (intValue != null &&
intValue <= _supportedCurrency[_selectedCurrency]!) {
return null;
}
return 'Invalid Input';
},
),
),
const SizedBox(
height: 10,
),
Text(
'Maximum of $maxInput',
style: const TextStyle(fontSize: 22),
)
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment