|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:phone_numbers_parser/phone_numbers_parser.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + runApp(const MyApp()); |
| 6 | +} |
| 7 | + |
| 8 | +class MyApp extends StatelessWidget { |
| 9 | + const MyApp({super.key}); |
| 10 | + |
| 11 | + @override |
| 12 | + Widget build(BuildContext context) { |
| 13 | + return MaterialApp( |
| 14 | + title: 'Flutter Demo', |
| 15 | + theme: ThemeData( |
| 16 | + colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
| 17 | + useMaterial3: true, |
| 18 | + ), |
| 19 | + home: const MyHomePage(), |
| 20 | + ); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class MyHomePage extends StatefulWidget { |
| 25 | + const MyHomePage({super.key}); |
| 26 | + |
| 27 | + @override |
| 28 | + State<MyHomePage> createState() => _MyHomePageState(); |
| 29 | +} |
| 30 | + |
| 31 | +class _MyHomePageState extends State<MyHomePage> { |
| 32 | + PhoneNumber? phoneNumber = PhoneNumber.parse('+16505551234'); |
| 33 | + |
| 34 | + @override |
| 35 | + Widget build(BuildContext context) { |
| 36 | + final phoneNumber = this.phoneNumber; |
| 37 | + return Scaffold( |
| 38 | + body: Center( |
| 39 | + child: ConstrainedBox( |
| 40 | + constraints: const BoxConstraints(maxWidth: 600), |
| 41 | + child: Column( |
| 42 | + children: [ |
| 43 | + const SizedBox(height: 48), |
| 44 | + Text( |
| 45 | + 'Try a phone number to see the parsing result below', |
| 46 | + style: Theme.of(context).textTheme.titleMedium, |
| 47 | + ), |
| 48 | + const SizedBox(height: 12), |
| 49 | + TextFormField( |
| 50 | + initialValue: phoneNumber?.international, |
| 51 | + decoration: const InputDecoration( |
| 52 | + label: Text('Phone number'), |
| 53 | + ), |
| 54 | + onChanged: (value) { |
| 55 | + setState(() => this.phoneNumber = PhoneNumber.parse(value)); |
| 56 | + }, |
| 57 | + ), |
| 58 | + const SizedBox(height: 12), |
| 59 | + ListTile( |
| 60 | + title: const Text('international'), |
| 61 | + trailing: phoneNumber != null |
| 62 | + ? Text(phoneNumber.international) |
| 63 | + : const Text('-'), |
| 64 | + ), |
| 65 | + ListTile( |
| 66 | + title: const Text('Formatted national'), |
| 67 | + trailing: phoneNumber != null |
| 68 | + ? Text(phoneNumber.formatNsn()) |
| 69 | + : const Text('-'), |
| 70 | + ), |
| 71 | + ListTile( |
| 72 | + title: const Text('Iso code'), |
| 73 | + trailing: phoneNumber != null |
| 74 | + ? Text(phoneNumber.isoCode.name) |
| 75 | + : const Text('-'), |
| 76 | + ), |
| 77 | + ListTile( |
| 78 | + title: const Text('Country Dial Code'), |
| 79 | + trailing: phoneNumber != null |
| 80 | + ? Text(phoneNumber.countryCode) |
| 81 | + : const Text('-'), |
| 82 | + ), |
| 83 | + ListTile( |
| 84 | + title: const Text('Is Valid'), |
| 85 | + trailing: phoneNumber != null |
| 86 | + ? Text(phoneNumber.isValid().toString()) |
| 87 | + : const Text('-'), |
| 88 | + ), |
| 89 | + ListTile( |
| 90 | + title: const Text('Is Valid Mobile'), |
| 91 | + trailing: phoneNumber != null |
| 92 | + ? Text(phoneNumber |
| 93 | + .isValid(type: PhoneNumberType.mobile) |
| 94 | + .toString()) |
| 95 | + : const Text('-'), |
| 96 | + ), |
| 97 | + ListTile( |
| 98 | + title: const Text('Is Valid Fixed Line'), |
| 99 | + trailing: phoneNumber != null |
| 100 | + ? Text(phoneNumber |
| 101 | + .isValid(type: PhoneNumberType.fixedLine) |
| 102 | + .toString()) |
| 103 | + : const Text('-'), |
| 104 | + ), |
| 105 | + ListTile( |
| 106 | + title: const Text('Is Valid Voip'), |
| 107 | + trailing: phoneNumber != null |
| 108 | + ? Text(phoneNumber |
| 109 | + .isValid(type: PhoneNumberType.voip) |
| 110 | + .toString()) |
| 111 | + : const Text('-'), |
| 112 | + ), |
| 113 | + ], |
| 114 | + ), |
| 115 | + )), |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments