Flutter缅甸行政区划插件myanmar_administrative_areas的使用
Flutter缅甸行政区划插件myanmar_administrative_areas的使用
Myanmar Administrative Areas - မြန်မာနိုင်ငံ၏ အုပ်ချုပ်ရေးနယ်မြေဒေသများ
参考资料:
包括了所有级别的行政区划:省、邦、直辖区、县、镇区、城镇、乡村。
使用方法
1. 导入包
在你的 pubspec.yaml
文件中添加依赖:
dependencies:
flutter:
sdk: flutter
myanmar_administrative_areas: ^1.0.0
然后运行 flutter pub get
来获取依赖包。
2. 基本示例
示例 1: 选择省份和城镇
import 'package:flutter/material.dart';
import 'package:myanmar_administrative_areas/myanmar_administrative_areas.dart';
void main() {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
home: MainApp(),
));
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("使用方法"),
backgroundColor: ThemeData.light().primaryColor,
foregroundColor: Colors.white,
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(64.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
Expanded(
child: FilledButton(
onPressed: () => Navigator.of(context)
.push(_createPageRoute(const VillageExample())),
child: const Text("选择乡村"))),
],
),
const SizedBox(
height: 16.0,
),
Row(
children: [
Expanded(
child: FilledButton(
onPressed: () => Navigator.of(context)
.push(_createPageRoute(const WardExample())),
child: const Text("选择城镇")),
),
],
)
],
),
),
),
);
}
PageRouteBuilder _createPageRoute(Widget page) {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => page,
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(1.0, 0.0);
const end = Offset.zero;
const curve = Curves.easeInOut;
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
var offsetAnimation = animation.drive(tween);
return SlideTransition(
position: offsetAnimation,
child: child,
);
},
);
}
}
class VillageExample extends StatefulWidget {
const VillageExample({super.key});
@override
State<VillageExample> createState() => _VillageExampleState();
}
class _VillageExampleState extends State<VillageExample> {
String? selectedValue1;
String? selectedValue2;
String? selectedValue3;
List<String> items2 = [];
List<String> items3 = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('选择乡村'),
),
body: Padding(
padding: const EdgeInsets.all(32.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
buildDropdown1(),
const SizedBox(height: 16),
items2.isNotEmpty ? buildDropdown2() : Container(),
const SizedBox(height: 16),
items3.isNotEmpty ? buildDropdown3() : Container(),
const SizedBox(
height: 30.0,
),
Row(
children: [
Expanded(
child: FilledButton(
onPressed: () {},
child: const Text("完成")),
),
const SizedBox(
height: 50.0,
),
],
),
(selectedValue1 != null && selectedValue2 != null && selectedValue3 != null)
? Text(
"$selectedValue1\n$selectedValue2 乡镇 \n$selectedValue3 乡村",
style: const TextStyle(fontSize: 20.0),
textAlign: TextAlign.center,
)
: Container()
],
),
),
);
}
Widget buildDropdown1() {
return DropdownButtonFormField<String>(
value: selectedValue1,
hint: const Text('选择省份'),
onChanged: (String? value) {
setState(() {
selectedValue1 = value;
selectedValue3 = null;
selectedValue2 = null;
items2 = townshipsAsDivisionMy[selectedValue1] ?? [];
items3 = [];
});
},
items: divisionMy.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text(item),
);
}).toList(),
);
}
Widget buildDropdown2() {
return DropdownButtonFormField<String>(
value: selectedValue2,
hint: const Text('选择乡镇'),
onChanged: (String? value) {
setState(() {
selectedValue2 = value;
selectedValue3 = null;
items3 = villagesAsTownshipMy[selectedValue2] ?? [];
});
},
items: items2.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text("$item 乡镇"),
);
}).toList(),
);
}
Widget buildDropdown3() {
return DropdownButtonFormField<String>(
value: selectedValue3,
hint: const Text('选择乡村'),
onChanged: (String? value) {
setState(() {
selectedValue3 = value;
});
},
items: items3.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text("$item 乡村"),
);
}).toList(),
);
}
}
class WardExample extends StatefulWidget {
const WardExample({super.key});
@override
State<WardExample> createState() => _WardExampleState();
}
class _WardExampleState extends State<WardExample> {
String? selectedValue1;
String? selectedValue2;
String? selectedValue3;
String? selectedValue4;
List<String> items2 = [];
List<String> items3 = [];
List<String> items4 = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('选择城镇'),
),
body: Padding(
padding: const EdgeInsets.all(32.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
buildDropdown1(),
const SizedBox(height: 16),
items2.isNotEmpty ? buildDropdown2() : Container(),
const SizedBox(height: 16),
items3.isNotEmpty ? buildDropdown3() : Container(),
const SizedBox(height: 16),
items4.isNotEmpty ? buildDropdown4() : Container(),
const SizedBox(
height: 30.0,
),
Row(
children: [
Expanded(
child: FilledButton(
onPressed: () {},
child: const Text("完成")),
),
const SizedBox(
height: 50.0,
),
],
),
(selectedValue1 != null && selectedValue2 != null && selectedValue3 != null && selectedValue4 != null)
? Text(
"$selectedValue1\n$selectedValue2 乡镇 \n$selectedValue3 城镇 \n $selectedValue4 街区",
style: const TextStyle(fontSize: 20.0),
textAlign: TextAlign.center,
)
: Container()
],
),
),
);
}
Widget buildDropdown1() {
return DropdownButtonFormField<String>(
value: selectedValue1,
hint: const Text('选择省份'),
onChanged: (String? value) {
setState(() {
selectedValue1 = value;
items2 = townshipsAsDivisionMy[selectedValue1] ?? [];
items3 = [];
items4 = [];
selectedValue2 = null;
selectedValue3 = null;
selectedValue4 = null;
});
},
items: divisionMy.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text(item),
);
}).toList(),
);
}
Widget buildDropdown2() {
return DropdownButtonFormField<String>(
value: selectedValue2,
hint: const Text('选择乡镇'),
onChanged: (String? value) {
setState(() {
selectedValue2 = value;
selectedValue3 = null;
items3 = townsAsTownshipMy[selectedValue2] ?? [];
});
},
items: items2.isNotEmpty
? items2.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text("$item 乡镇"),
);
}).toList()
: [''].map((e) {
return const DropdownMenuItem<String>(
value: '',
child: Text(''),
);
}).toList(),
);
}
Widget buildDropdown3() {
return DropdownButtonFormField<String>(
value: selectedValue3,
hint: const Text('选择城镇'),
onChanged: (String? value) {
setState(() {
selectedValue3 = value;
selectedValue4 = null;
items4 = wardsMy[selectedValue3] ?? [];
});
},
items: items3.isNotEmpty
? items3.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text("$item 城镇"),
);
}).toList()
: [''].map((e) {
return const DropdownMenuItem<String>(
value: '',
child: Text(''),
);
}).toList(),
);
}
Widget buildDropdown4() {
return DropdownButtonFormField<String>(
value: selectedValue4,
hint: const Text('选择街区'),
onChanged: (String? value) {
setState(() {
selectedValue4 = value!;
});
},
items: items4.isNotEmpty
? items4.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text("$item 街区"),
);
}).toList()
: [''].map((e) {
return const DropdownMenuItem<String>(
value: '',
child: Text(''),
);
}).toList(),
);
}
}
说明
-
选择省份:
DropdownButtonFormField<String>( value: selectedValue1, hint: const Text('选择省份'), onChanged: (String? value) { setState(() { selectedValue1 = value; selectedValue3 = null; selectedValue2 = null; items2 = townshipsAsDivisionMy[selectedValue1] ?? []; items3 = []; }); }, items: divisionMy.map((String item) { return DropdownMenuItem<String>( value: item, child: Text(item), ); }).toList(), );
-
选择乡镇:
DropdownButtonFormField<String>( value: selectedValue2, hint: const Text('选择乡镇'), onChanged: (String? value) { setState(() { selectedValue2 = value; selectedValue3 = null; items3 = villagesAsTownshipMy[selectedValue2] ?? []; }); }, items: items2.map((String item) { return DropdownMenuItem<String>( value: item, child: Text("$item 乡镇"), ); }).toList(), );
-
选择乡村:
DropdownButtonFormField<String>( value: selectedValue3, hint: const Text('选择乡村'), onChanged: (String? value) { setState(() { selectedValue3 = value; }); }, items: items3.map((String item) { return DropdownMenuItem<String>( value: item, child: Text("$item 乡村"), ); }).toList(), );
更多关于Flutter缅甸行政区划插件myanmar_administrative_areas的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter缅甸行政区划插件myanmar_administrative_areas的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
myanmar_administrative_areas
是一个用于获取缅甸行政区划信息的 Flutter 插件。它可以帮助开发者轻松地获取缅甸的省、市、区等行政区划数据。以下是如何使用该插件的基本步骤:
1. 添加依赖项
首先,你需要在 pubspec.yaml
文件中添加 myanmar_administrative_areas
插件的依赖项。
dependencies:
flutter:
sdk: flutter
myanmar_administrative_areas: ^1.0.0 # 使用最新版本号
然后运行 flutter pub get
来获取依赖项。
2. 导入插件
在你的 Dart 文件中导入插件:
import 'package:myanmar_administrative_areas/myanmar_administrative_areas.dart';
3. 使用插件
你可以使用 MyanmarAdministrativeAreas
类来获取缅甸的行政区划信息。以下是一些常见的使用示例:
获取所有省/邦
List<Region> regions = MyanmarAdministrativeAreas.getAllRegions();
for (var region in regions) {
print(region.name); // 打印省/邦的名称
}
获取某个省/邦下的所有市/镇
List<Township> townships = MyanmarAdministrativeAreas.getTownshipsByRegionId(regionId);
for (var township in townships) {
print(township.name); // 打印市/镇的名称
}
获取某个市/镇下的所有区/村
List<Ward> wards = MyanmarAdministrativeAreas.getWardsByTownshipId(townshipId);
for (var ward in wards) {
print(ward.name); // 打印区/村的名称
}
4. 示例代码
以下是一个完整的示例,展示如何获取并显示缅甸的行政区划信息:
import 'package:flutter/material.dart';
import 'package:myanmar_administrative_areas/myanmar_administrative_areas.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('缅甸行政区划'),
),
body: MyanmarAdministrativeAreasExample(),
),
);
}
}
class MyanmarAdministrativeAreasExample extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
// 获取所有省/邦
List<Region> regions = MyanmarAdministrativeAreas.getAllRegions();
return ListView.builder(
itemCount: regions.length,
itemBuilder: (context, index) {
Region region = regions[index];
return ExpansionTile(
title: Text(region.name),
children: [
// 获取该省/邦下的所有市/镇
...MyanmarAdministrativeAreas.getTownshipsByRegionId(region.id).map((township) {
return ListTile(
title: Text(township.name),
subtitle: Text('ID: ${township.id}'),
);
}).toList(),
],
);
},
);
}
}