Skip to content

Commit

Permalink
✨ feat(LoginHome.dart): 添加移除服务器地址的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
YangChengxxyy committed Feb 15, 2022
1 parent 0236320 commit 57625a3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 55 deletions.
116 changes: 62 additions & 54 deletions lib/app/LoginHome.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:dio/dio.dart';
import 'package:direct_select_flutter/direct_select_container.dart';
import 'package:direct_select_flutter/direct_select_item.dart';
import 'package:direct_select_flutter/direct_select_list.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

Expand Down Expand Up @@ -86,7 +85,7 @@ class _LoginBodyState extends State<LoginBody> {
);
}

var _cities = <SelectItem>[
var _urls = <SelectItem>[
new SelectItem(title: HttpUtil.dio.options.baseUrl, icon: Icon(Icons.http)),
];

Expand All @@ -98,15 +97,15 @@ class _LoginBodyState extends State<LoginBody> {

void _initUrls() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String>? urls = prefs.getStringList(otherUrlsKey);
List<String>? urls = prefs.getStringList(urlsKey);
int? index = prefs.getInt(selectedIndexKey);
if (urls != null) {
urls.forEach((e) {
_cities.add(new SelectItem(title: e, icon: Icon(Icons.http)));
_urls.add(new SelectItem(title: e, icon: Icon(Icons.http)));
});
}
setState(() {
_cities.add(new SelectItem(
_urls.add(new SelectItem(
title: "Add", icon: Icon(Icons.add), selectType: SelectType.add));
if (index != null) {
_urlSelectedIndex = index;
Expand Down Expand Up @@ -147,49 +146,53 @@ class _LoginBodyState extends State<LoginBody> {
children: [
Expanded(
child: DirectSelectList<SelectItem>(
values: _cities,
defaultItemIndex: _urlSelectedIndex,
itemBuilder: (SelectItem value) =>
_getDropDownMenuItem(value),
focusedItemDecoration: _getDslDecoration(),
onItemSelectedListener: (item, index, context) async {
switch (item.selectType) {
case SelectType.add:
String? url = await _showAddUrl();
if (url != null) {
setState(() {
_cities.insert(
_cities.length - 1,
new SelectItem(
title: url,
icon: Icon(Icons.http),
),
);
_urlSelectedIndex = _cities.length - 2;
});
_addUrl(url);
values: _urls,
defaultItemIndex: _urlSelectedIndex,
itemBuilder: (SelectItem value) =>
_getDropDownMenuItem(value),
focusedItemDecoration: _getDslDecoration(),
onItemSelectedListener:
(item, index, context) async {
switch (item.selectType) {
case SelectType.add:
String? url = await _showAddUrl();
if (url != null) {
setState(() {
_urls.insert(
_urls.length - 1,
new SelectItem(
title: url,
icon: Icon(Icons.http),
),
);
_urlSelectedIndex = _urls.length - 2;
});
_addUrl(url);
_selectUrl(_urlSelectedIndex);
} else {
setState(() {
_urlSelectedIndex = _urlSelectedIndex;
});
}
break;
case SelectType.none:
_baseUrl = item.title;
HttpUtil.dio.options.baseUrl = _baseUrl;
_selectUrl(index);
} else {
setState(() {
_urlSelectedIndex = _urlSelectedIndex;
_urlSelectedIndex = index;
});
}
break;
case SelectType.none:
_baseUrl = item.title;
HttpUtil.dio.options.baseUrl = _baseUrl;
_selectUrl(index);
break;
}
},
onUserTappedListener: () {
debugPrint("usertap");
},
),
break;
}
debugPrint(_urlSelectedIndex.toString());
}),
),
IconButton(
icon: Icon(Icons.close),
onPressed: () => _removeUrl(_urlSelectedIndex),
Offstage(
offstage: _urls.length == 2 || _urlSelectedIndex == 0,
child: IconButton(
icon: Icon(Icons.close),
onPressed: () => _removeUrl(),
),
),
],
),
Expand Down Expand Up @@ -331,14 +334,14 @@ class _LoginBodyState extends State<LoginBody> {

void _addUrl(String url) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String>? urls = prefs.getStringList(otherUrlsKey);
List<String>? urls = prefs.getStringList(urlsKey);
if (urls != null) {
urls.add(url);
} else {
urls = <String>[];
urls.add(url);
}
prefs.setStringList(otherUrlsKey, urls);
prefs.setStringList(urlsKey, urls);
}

void _selectUrl(int index) async {
Expand All @@ -347,7 +350,7 @@ class _LoginBodyState extends State<LoginBody> {
}

Future<String?> _showAddUrl() {
final _newUrlController = new TextEditingController();
final _newUrlController = new TextEditingController(text: "http://");

final GlobalKey<FormState> _formKey2 = GlobalKey<FormState>();

Expand Down Expand Up @@ -394,15 +397,20 @@ class _LoginBodyState extends State<LoginBody> {
);
}

_removeUrl(int urlSelectedIndex) async {
_removeUrl() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String>? urls = prefs.getStringList(otherUrlsKey);
int? index = prefs.getInt(selectedIndexKey);
if (urls != null) {
urls.forEach((e) {
_cities.add(new SelectItem(title: e, icon: Icon(Icons.http)));
});
setState(() {
_urls.removeAt(_urlSelectedIndex);
if (_urlSelectedIndex == _urls.length - 1) {
_urlSelectedIndex--;
}
});
await prefs.setInt(selectedIndexKey, _urlSelectedIndex);
var urls = <String>[];
for (int i = 1; i < _urls.length - 1; i++) {
urls.add(_urls[i].title);
}
await prefs.setStringList(urlsKey, urls);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/GlobalSetting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ final String isLoginKey = "isLogin";
final String isRememberKey = "isRemember";
final String usernameKey = "username";
final String passwordKey = "password";
final String otherUrlsKey = "otherUrls";
final String urlsKey = "otherUrls";
final String selectedIndexKey = "selectedIndex";

final double paddingNum = 10;

0 comments on commit 57625a3

Please sign in to comment.