#
whycq
2025-03-03 6a90c5bde0facc8330ce4c7c7d89292717b7ac65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
 
import '../../api/dio_config.dart';
import '../../theme/theme_controller.dart';
 
class NetworkSettings extends StatefulWidget {
  const NetworkSettings({super.key});
 
  @override
  State<NetworkSettings> createState() => _NetworkSettingsState();
}
 
class _NetworkSettingsState extends State<NetworkSettings> {
  final ThemeController themeController = Get.find();
  String _ip = '';
  int _port = 0;
  String _url = '';
 
  Future<void> _saveSettings() async {
    await SettingService.saveNetworkSettings(_ip, _port, _url);
    await DioConfig.updateBaseURL();
    Get.back();
  }
 
  Future<void> _loadSettings() async {
    final settings = await SettingService.getNetworkSettings();
    setState(() {
      _ip = settings['ip'];
      _port = settings['port'];
      _url = settings['url'];
    });
  }
 
  @override
  void initState() {
    super.initState();
    _loadSettings();
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("网络设置"),
        backgroundColor: themeController.appBarColor,
        leading: IconButton(
            onPressed: () {
              Navigator.pop(context);
            },
            icon: Icon(Icons.arrow_back_ios)),
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        child: Container(
          child: ListView(
            children: [
              TextField(
                autofocus: true,
                decoration: InputDecoration(
                  labelText: "Ip Address",
                ),
                controller: TextEditingController(text: _ip),
                onChanged: (value) {
                  _ip = value;
                },
              ),
              TextField(
                autofocus: true,
                decoration: InputDecoration(
                  labelText: "Port",
                ),
                controller: TextEditingController(text: _port.toString()),
                onChanged: (value) {
                  _port = int.parse(value);
                },
              ),
              TextField(
                autofocus: true,
                decoration: InputDecoration(
                  labelText: "Url",
                ),
                controller: TextEditingController(text: _url),
                onChanged: (value) {
                  _url = value;
                },
              ),
              ElevatedButton(
                  onPressed: _saveSettings, child: Text('Save Settings')),
            ],
          ),
        ),
      ),
    );
  }
}
 
class SettingService {
  static Future<void> saveNetworkSettings(
      String ip, int port, String url) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('network_ip', ip);
    await prefs.setInt('network_port', port);
    await prefs.setString('network_url', url);
  }
 
  static Future<Map<String, dynamic>> getNetworkSettings() async {
    final prefs = await SharedPreferences.getInstance();
    String ip = prefs.getString('network_ip') ?? '127.0.0.1';
    int port = prefs.getInt('network_port') ?? 8080;
    String url = prefs.getString('network_url') ?? 'wms';
    return {'ip': ip, 'port': port, 'url': url};
  }
}