diff --git a/lib/main.dart b/lib/main.dart index f51dd17..efeb738 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:cpnav/appbar.dart'; import 'package:cpnav/pages/pass_track/controller.dart'; +import 'package:cpnav/pages/pass_track/bottomIcon/iconController.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; @@ -10,6 +11,7 @@ void main() { WidgetsFlutterBinding.ensureInitialized(); SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []); //设置全屏 Get.put(PassTrackController('WXLMB', 'cp_road')); + Get.put(IconController()); runApp(const MyApp()); } diff --git a/lib/pages/pass_track/iconContainer.dart b/lib/pages/pass_track/bottomIcon/iconContainer.dart similarity index 75% rename from lib/pages/pass_track/iconContainer.dart rename to lib/pages/pass_track/bottomIcon/iconContainer.dart index 92d9316..2531356 100644 --- a/lib/pages/pass_track/iconContainer.dart +++ b/lib/pages/pass_track/bottomIcon/iconContainer.dart @@ -4,49 +4,17 @@ import 'package:flutter/widgets.dart'; import 'package:get/get.dart'; import 'package:scence_map/controller.dart'; +import 'iconController.dart'; + ScenceMapController mapController = Get.put(ScenceMapController()); -class IconContainer extends StatefulWidget { - @override - _IconContainerState createState() => _IconContainerState(); -} - -class _IconContainerState extends State { - final Map _iconColors = { - Icons.article: Colors.green, - Icons.speed: const Color.fromARGB(255, 166, 182, 149), - Icons.thermostat: const Color.fromARGB(255, 166, 182, 149), - Icons.zoom_in: const Color.fromARGB(255, 166, 182, 149), - Icons.zoom_out: const Color.fromARGB(255, 166, 182, 149), - Icons.refresh: const Color.fromARGB(255, 166, 182, 149), - Icons.place: const Color.fromARGB(255, 166, 182, 149), - }; - IconData? _selectedIcon; - - void _handleTap1(IconData icon, VoidCallback onTap) { - setState(() { - _selectedIcon = icon; - _iconColors.forEach((key, value) { - _iconColors[key] = key == icon - ? Colors.green // 点击后变浅的颜色 - : const Color.fromARGB(255, 166, 182, 149); // 其他图标恢复原色 - }); - }); - onTap(); - } - - void _handleTap2(IconData icon, VoidCallback onTap) { - setState(() { - _iconColors[icon] = Colors.green; // 点击后变浅的颜色 - }); - onTap(); - Future.delayed(Duration(milliseconds: 200), () { - setState(() { - _iconColors[icon] = const Color.fromARGB(255, 166, 182, 149); // 恢复原色 - }); - }); - } +// class IconContainer extends StatefulWidget { +// @override +// _IconContainerState createState() => _IconContainerState(); +// } +// class _IconContainerState extends State { +class IconContainer extends GetView { @override Widget build(BuildContext context) { var isPortrait = MediaQuery.of(context).orientation == Orientation.portrait; @@ -203,47 +171,51 @@ class _IconContainerState extends State { //前三个单选 Widget _singleChange(IconData icon, VoidCallback onTap) { - return Container( - width: 33, - height: 33, - margin: const EdgeInsets.symmetric(vertical: 2.0), - decoration: BoxDecoration( - color: _iconColors[icon], - border: Border.all( - color: Colors.white, + return Obx(() { + return Container( + width: 33, + height: 33, + margin: const EdgeInsets.symmetric(vertical: 2.0), + decoration: BoxDecoration( + color: controller.iconColors[icon], + border: Border.all( + color: Colors.white, + ), + borderRadius: BorderRadius.circular(5.0), ), - borderRadius: BorderRadius.circular(5.0), - ), - child: InkWell( - child: Icon( - icon, - color: Colors.white, + child: InkWell( + child: Icon( + icon, + color: Colors.white, + ), + onTap: () => controller.handleTap1(icon, onTap), ), - onTap: () => _handleTap1(icon, onTap), - ), - ); + ); + }); } //后三个自动恢复 Widget _automChange(IconData icon, VoidCallback onTap) { - return Container( - width: 33, - height: 33, - margin: const EdgeInsets.symmetric(vertical: 2.0), - decoration: BoxDecoration( - color: _iconColors[icon], - border: Border.all( - color: Colors.white, + return Obx(() { + return Container( + width: 33, + height: 33, + margin: const EdgeInsets.symmetric(vertical: 2.0), + decoration: BoxDecoration( + color: controller.iconColors[icon], + border: Border.all( + color: Colors.white, + ), + borderRadius: BorderRadius.circular(5.0), ), - borderRadius: BorderRadius.circular(5.0), - ), - child: InkWell( - child: Icon( - icon, - color: Colors.white, + child: InkWell( + child: Icon( + icon, + color: Colors.white, + ), + onTap: () => controller.handleTap2(icon, onTap), ), - onTap: () => _handleTap2(icon, onTap), - ), - ); + ); + }); } } diff --git a/lib/pages/pass_track/bottomIcon/iconController.dart b/lib/pages/pass_track/bottomIcon/iconController.dart new file mode 100644 index 0000000..663fbd0 --- /dev/null +++ b/lib/pages/pass_track/bottomIcon/iconController.dart @@ -0,0 +1,41 @@ +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; + +class IconController extends GetxController { + final RxMap iconColors = { + Icons.article: Colors.green, + Icons.speed: const Color.fromARGB(255, 166, 182, 149), + Icons.thermostat: const Color.fromARGB(255, 166, 182, 149), + Icons.zoom_in: const Color.fromARGB(255, 166, 182, 149), + Icons.zoom_out: const Color.fromARGB(255, 166, 182, 149), + Icons.refresh: const Color.fromARGB(255, 166, 182, 149), + Icons.place: const Color.fromARGB(255, 166, 182, 149), + }.obs; + // IconData? _selectedIcon; + late IconData selectedIcon; + @override + void onInit() { + super.onInit(); + handleTap1(Icons.close, () {}); // 初始时,默认第一个图标为选中状态,并触发onTap事件(空函数, onTap) + handleTap2(Icons.close, () {}); + } + + void handleTap1(IconData icon, VoidCallback onTap) { + selectedIcon = icon; + iconColors.forEach((key, value) { + iconColors[key] = key == icon + ? Colors.green // 点击后变浅的颜色 + : const Color.fromARGB(255, 166, 182, 149); // 其他图标恢复原色 + }); + onTap(); + } + + void handleTap2(IconData icon, VoidCallback onTap) { + iconColors[icon] = Colors.green; // 点击后变浅的颜色 + + onTap(); + Future.delayed(Duration(milliseconds: 200), () { + iconColors[icon] = const Color.fromARGB(255, 166, 182, 149); // 恢复原色 + }); + } +} diff --git a/lib/pages/pass_track/view.dart b/lib/pages/pass_track/view.dart index f6320e5..4063e96 100644 --- a/lib/pages/pass_track/view.dart +++ b/lib/pages/pass_track/view.dart @@ -9,7 +9,7 @@ import 'package:scence_map/controller.dart'; import 'package:scence_map/scence_map.dart'; import 'package:syncfusion_flutter_sliders/sliders.dart'; -import 'iconContainer.dart'; +import 'bottomIcon/iconContainer.dart'; import "controller.dart"; import "package:pass_track/pass_track.dart"; import "package:pass_track/pass_track_platform_interface.dart";