mn_loc 1.0.4
mn_loc: ^1.0.4 copied to clipboard
A Flutter package for 3-level dependent dropdowns (District -> Subdivision -> Circle) loaded directly from JSON data.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:mn_loc/mn_loc.dart';
import 'package:provider/provider.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<LocationDropdownProvider>(
create: (_) => LocationDropdownProvider()..initialize(),
),
],
child: const MnLocExampleApp(),
),
);
}
class MnLocExampleApp extends StatelessWidget {
const MnLocExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'mn_loc Package Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
useMaterial3: true,
),
home: const ExampleHomePage(),
);
}
}
class ExampleHomePage extends StatelessWidget {
const ExampleHomePage({super.key});
@override
Widget build(BuildContext context) {
final provider = context.watch<LocationDropdownProvider>();
return Scaffold(
appBar: AppBar(
title: const Text('mn_loc Package Example'),
actions: [
IconButton(
tooltip: 'Pre-select Initial Values',
icon: const Icon(Icons.edit_location_alt_rounded),
onPressed: () {
// Pre-select location by IDs (District -> Subdivision -> Circle)
context.read<LocationDropdownProvider>().setInitialSelection(
districtId: '1',
subdivisionId: '1',
circleId: '1',
);
},
),
IconButton(
tooltip: 'Reset Selections',
icon: const Icon(Icons.refresh_rounded),
onPressed: () {
context.read<LocationDropdownProvider>().resetSelections();
},
),
],
),
body: provider.isLoading
? const Center(child: CircularProgressIndicator())
: SingleChildScrollView(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Card(
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: [
DistrictDropdown(
label: 'District',
hint: 'Select District...',
),
SizedBox(height: 16),
SubdivisionDropdown(
label: 'Subdivision',
hint: 'Select Subdivision...',
disabledHint: 'Select District first',
),
SizedBox(height: 16),
CircleDropdown(
label: 'Circle',
hint: 'Select Circle...',
disabledHint: 'Select Subdivision first',
),
],
),
),
),
const SizedBox(height: 20),
const LocationSummaryCard(),
const SizedBox(height: 24),
ElevatedButton.icon(
icon: const Icon(Icons.check_circle_outline),
label: const Text('Submit Selection'),
onPressed: () {
final district = provider.selectedDistrict;
final subdivision = provider.selectedSubdivision;
final circle = provider.selectedCircle;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Selected:\n'
'District: ${district?.name ?? "None"}\n'
'Subdivision: ${subdivision?.name ?? "None"}\n'
'Circle: ${circle?.name ?? "None"}',
),
),
);
},
),
],
),
),
);
}
}