You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

218 lines
6.9 KiB

// This file is part of Hamlet Social.
//
// Hamlet Social is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Hamlet Social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Hamlet Social. If not, see <https://www.gnu.org/licenses/>.
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:direct_select/direct_select.dart';
import 'package:hamlet_social/data/instance.dart';
import 'package:country_codes/country_codes.dart';
import 'package:hamlet_social/endpoints/unauthenticated/available_instances.dart';
class InstanceSelector extends StatefulWidget {
final List<HamletInstance> _hamletInstances;
InstanceSelector(this._hamletInstances);
@override
State<StatefulWidget> createState() => _InstanceSelectorState();
}
class _InstanceSelectorState extends State<InstanceSelector> {
var _isCustomInstance = false;
final _formKey = GlobalKey<FormState>();
List<Widget> _buildCountryList() {
if (this.widget._hamletInstances.isEmpty) {
return [Text("None")];
}
final res = this
.widget
._hamletInstances
.map((i) => i.country.localizedName)
.toSet()
.toList();
res.sort();
return res.map((c) => Text(c)).toList();
}
int _selectedCountryIndex = 0;
List<Widget> _buildStateList() {
if (this.widget._hamletInstances.isEmpty) {
return [Text("None")];
}
final res = this
.widget
._hamletInstances
.where((i) =>
i.country.localizedName ==
(_buildCountryList()[_selectedCountryIndex] as Text).data)
.map((i) => i.state)
.where((s) => s != null)
.toSet()
.toList();
return res.map((s) => Text(s)).toList();
}
int _selectedStateIndex = 0;
List<Widget> _buildCityList() {
if (this.widget._hamletInstances.isEmpty) {
return [Text("None")];
}
final res = this
.widget
._hamletInstances
.where((i) =>
i.country.localizedName ==
(_buildCountryList()[_selectedCountryIndex] as Text).data &&
(_buildStateList().isEmpty ||
i.state ==
(_buildStateList()[_selectedStateIndex] as Text).data))
.map((i) => i.city)
.toSet()
.toList();
res.sort();
return res.map((c) => Text(c)).toList();
}
int _selectedCityIndex = 0;
void _toggleIsCustomInstance() {
setState(() {
this._isCustomInstance = !this._isCustomInstance;
});
}
bool _selectedCountryHasStates() => _buildStateList().isNotEmpty;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
!_isCustomInstance
? Column(
children: [
Padding(
padding: const EdgeInsets.only(left: 0),
child: Text("Country"),
),
DirectSelect(
itemExtent: 35,
selectedIndex: _selectedCountryIndex,
child: _buildCountryList()[_selectedCountryIndex],
onSelectedItemChanged: (index) {
setState(() {
_selectedCountryIndex = index;
_selectedStateIndex = 0;
_selectedCityIndex = 0;
});
},
items: _buildCountryList(),
),
_selectedCountryHasStates()
? Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 10),
child: Text("State"),
),
DirectSelect(
itemExtent: 35,
selectedIndex: _selectedStateIndex,
child: _buildStateList()[_selectedStateIndex],
onSelectedItemChanged: (index) {
setState(() {
_selectedStateIndex = index;
_selectedCityIndex = 0;
});
},
items: _buildStateList(),
),
],
)
: SizedBox.shrink(),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Text("City"),
),
DirectSelect(
itemExtent: 35,
selectedIndex: _selectedCityIndex,
child: _buildCityList()[_selectedCityIndex],
onSelectedItemChanged: (index) {
setState(() {
_selectedCityIndex = index;
});
},
items: _buildCityList(),
),
],
)
: Column(
children: <Widget>[TextFormField()],
),
GestureDetector(
onTap: this._toggleIsCustomInstance,
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Text(_isCustomInstance
? "Connect to a Hamlet instance instead"
: "Connect to a custom instance instead"),
),
),
],
),
);
}
}
// class _CountryFormField extends FormField<String> {
// _CountryFormField({
// Key key,
// String initialValue = "United States of America",
// this.decoration = const InputDecoration(),
// onSaved,
// validator,
// this.onChanged,
// this.constraints = const BoxConstraints(),
// }) : assert(decoration != null),
// assert(initialValue != null),
// assert(constraints != null),
// super(
// key: key,
// onSaved: onSaved,
// initialValue: initialValue,
// validator: validator,
// builder: (FormFieldState field) {});
// final ValueChanged onChanged;
// final InputDecoration decoration;
// final BoxConstraints constraints;
// }
// class _StateFormField extends FormField<String> {}
// class _CityFormField extends FormField<String> {}