car_license_plate 2.0.1
car_license_plate: ^2.0.1 copied to clipboard
Realistic, scalable licence plate widgets and validators for 20 countries: Iran, EU, UK, USA, Japan, China, India, Brazil and more.
import 'package:car_license_plate/car_license_plate.dart';
import 'package:flutter/material.dart';
void main() => runApp(const ExampleApp());
class ExampleApp extends StatefulWidget {
const ExampleApp({super.key});
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
ThemeMode _mode = ThemeMode.system;
@override
Widget build(BuildContext context) {
const seed = Color(0xFF1F3C9C);
return MaterialApp(
title: 'car_license_plate',
debugShowCheckedModeBanner: false,
themeMode: _mode,
theme: ThemeData(colorSchemeSeed: seed),
darkTheme: ThemeData(colorSchemeSeed: seed, brightness: Brightness.dark),
home: HomePage(
onToggleTheme: () => setState(() {
_mode = _mode == ThemeMode.dark ? ThemeMode.light : ThemeMode.dark;
}),
),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key, required this.onToggleTheme});
final VoidCallback onToggleTheme;
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _tab = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Licence plates'),
actions: [
IconButton(
tooltip: 'Toggle dark mode',
icon: const Icon(Icons.brightness_6),
onPressed: widget.onToggleTheme,
),
],
),
body: const [GalleryPage(), TryItPage(), IranInputPage()][_tab],
bottomNavigationBar: NavigationBar(
selectedIndex: _tab,
onDestinationSelected: (i) => setState(() => _tab = i),
destinations: const [
NavigationDestination(icon: Icon(Icons.grid_view), label: 'Gallery'),
NavigationDestination(icon: Icon(Icons.edit), label: 'Try it'),
NavigationDestination(
icon: Icon(Icons.keyboard),
label: 'Iran input',
),
],
),
);
}
}
/// Every design shipped by the package.
List<(String, PlateSpec)> galleryPlates() => [
(
'Iran',
const IranPlate(
twoDigits: '12',
letter: 'ب',
threeDigits: '345',
provinceCode: '67',
),
),
('Germany', const GermanPlate('B MW 1234', inspectionYear: 2026)),
('France', const FrenchPlate('AB-123-CD')),
('Italy', const ItalianPlate('AB 123 CD', year: '24', province: 'RM')),
('Spain', SpanishPlate('1234 BCD')),
('Netherlands', DutchPlate('GBB-12-X')),
('United Kingdom · rear', const UkPlate('AB12 CDE')),
(
'United Kingdom · front',
const UkPlate('AB12 CDE', rear: false, band: UkBand.unionFlag),
),
('USA · California', UsPlate('123ABC1', style: UsPlateStyle.california)),
('USA · Texas', UsPlate('BCD-1234', style: UsPlateStyle.texas)),
('USA · Florida', UsPlate('ABC 123', style: UsPlateStyle.florida)),
('USA · New York', UsPlate('KLM-1234', style: UsPlateStyle.newYork)),
(
'Japan',
const JapanesePlate(
region: '品川',
classNumber: '330',
hiragana: 'さ',
serial: '1234',
),
),
('China', const ChinesePlate('京A·12345')),
(
'China · new energy',
const ChinesePlate('粤B·D12345', type: ChinesePlateType.newEnergy),
),
('South Korea', const KoreanPlate('123가 4567')),
('India', const IndianPlate('MH 12 AB 1234')),
('Indonesia', const IndonesianPlate('B 1234 XYZ', expiry: '08.27')),
('Brazil', const BrazilPlate('ABC1D23')),
('Russia', const RussianPlate('А123ВС77')),
('Turkey', TurkishPlate('34 ABC 123')),
('Ukraine', UkrainianPlate('АА 1234 ВВ')),
('Saudi Arabia', SaudiPlate(digits: '1234', letters: 'ABJ')),
('UAE · Dubai', DubaiPlate(code: 'A', number: '12345')),
('Australia · NSW', const NswPlate('AB 12 CD')),
];
class GalleryPage extends StatelessWidget {
const GalleryPage({super.key});
@override
Widget build(BuildContext context) {
final plates = galleryPlates();
return LicensePlateTheme(
elevation: 3,
child: GridView.builder(
padding: const EdgeInsets.all(16),
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 420,
mainAxisExtent: 150,
crossAxisSpacing: 16,
mainAxisSpacing: 8,
),
itemCount: plates.length,
itemBuilder: (context, i) {
final (label, plate) = plates[i];
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(label, style: Theme.of(context).textTheme.labelLarge),
const SizedBox(height: 6),
Expanded(
child: Align(
alignment: Alignment.topLeft,
child: LicensePlate(plate),
),
),
],
);
},
),
);
}
}
class TryItPage extends StatefulWidget {
const TryItPage({super.key});
@override
State<TryItPage> createState() => _TryItPageState();
}
class _TryItPageState extends State<TryItPage> {
String _country = 'DE';
late final TextEditingController _text = TextEditingController(
text: PlateRegistry.format('DE')!.example,
);
@override
void dispose() {
_text.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final format = PlateRegistry.format(_country)!;
final plate = PlateRegistry.create(_country, _text.text);
final canonical = format.format(_text.text);
return ListView(
padding: const EdgeInsets.all(16),
children: [
DropdownButtonFormField<String>(
initialValue: _country,
decoration: const InputDecoration(labelText: 'Country'),
items: [
for (final c in PlateRegistry.countryCodes)
DropdownMenuItem(
value: c,
child: Text('$c · ${PlateRegistry.format(c)!.name}'),
),
],
onChanged: (c) => setState(() {
_country = c!;
_text.text = PlateRegistry.format(c)!.example;
}),
),
const SizedBox(height: 12),
TextField(
controller: _text,
decoration: InputDecoration(
labelText: 'Registration',
helperText: 'Example: ${format.example}',
errorText: canonical == null ? 'Not a valid ${format.name}' : null,
),
onChanged: (_) => setState(() {}),
),
const SizedBox(height: 24),
Center(
child: plate == null
? const Text('Cannot draw this text for the selected country')
: LicensePlate(plate, width: 360, elevation: 4),
),
const SizedBox(height: 16),
if (canonical != null)
Center(child: Text('Canonical form: $canonical')),
],
);
}
}
class IranInputPage extends StatefulWidget {
const IranInputPage({super.key});
@override
State<IranInputPage> createState() => _IranInputPageState();
}
class _IranInputPageState extends State<IranInputPage> {
String two = '12';
String letter = 'ب';
String three = '345';
String city = '67';
bool persian = true;
@override
Widget build(BuildContext context) {
final plate = IranPlate(
twoDigits: two,
letter: letter,
threeDigits: three,
provinceCode: city,
);
return ListView(
padding: const EdgeInsets.all(16),
children: [
Center(
child: CarLicensePlate(
isInputType: true,
twoNumberSection: two,
alphabetSection: letter,
threeNumberSection: three,
cityNumber: city,
usePersianNumbers: persian,
onTwoNumberChanged: (v) => setState(() => two = v),
onThreeNumberChanged: (v) => setState(() => three = v),
onCityNumberChanged: (v) => setState(() => city = v),
onAlphabetTap: () async {
final l = await showIranPlateLetterPicker(context);
if (l != null) setState(() => letter = l);
},
),
),
SwitchListTile(
title: const Text('Persian digits'),
value: persian,
onChanged: (v) => setState(() => persian = v),
),
ListTile(
title: Text(plate.isValid ? 'Valid plate' : 'Incomplete or invalid'),
subtitle: Text(plate.registration),
leading: Icon(plate.isValid ? Icons.check_circle : Icons.error),
),
],
);
}
}