Skip to content

Instantly share code, notes, and snippets.

@quenbyako
Created June 2, 2020 16:11
Show Gist options
  • Save quenbyako/185cd1f53b0f3d9a4fc074ef97c7284e to your computer and use it in GitHub Desktop.
Save quenbyako/185cd1f53b0f3d9a4fc074ef97c7284e to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:kong_api/api.dart' as api;
import 'package:flutter_chips_input/flutter_chips_input.dart';
import '../flushbars/flushbars.dart' as flushbars;
class CreateRoutePopup extends StatefulWidget {
final _createRouteKey = GlobalKey<FormState>();
var _newRoute = api.Route();
String serviceId;
CreateRoutePopup({
this.serviceId,
}) {
_newRoute.service = api.RouteService();
_newRoute.service.id = serviceId;
}
@override
_CreateRoutePopupState createState() => _CreateRoutePopupState(
obj: _newRoute,
key: _createRouteKey,
);
}
class _CreateRoutePopupState extends State<CreateRoutePopup> {
GlobalKey<FormState> key;
api.Route obj;
String _protocolValue;
FocusNode focus;
_CreateRoutePopupState({
this.obj,
this.key,
});
@override
void initState() {
super.initState();
focus = FocusNode();
}
@override
void dispose() {
focus.dispose();
super.dispose();
}
@override
Widget build(BuildContext ctx) {
return AlertDialog(
title: Text('Добавить роут'),
content: Container(
width: MediaQuery.of(ctx).size.width < 700
? MediaQuery.of(ctx).size.width - 50
: 700,
height: MediaQuery.of(ctx).size.height < 500
? MediaQuery.of(ctx).size.width - 50
: 500,
child: Form(
key: key,
child: ListView(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: "Название",
),
onSaved: (newValue) => obj.name = newValue,
autocorrect: false,
focusNode: focus,
),
TextFormField(
decoration: InputDecoration(
labelText: "Название",
),
onSaved: (newValue) => obj.name = newValue,
autocorrect: false,
focusNode: focus,
),
ChipsInput<String>(
decoration: InputDecoration(
labelText: "Пути",
),
key: ObjectKey(obj.paths),
chipBuilder: (context, state, path) {
return InputChip(
key: ObjectKey(path),
label: Text(path),
onDeleted: () => state.deleteChip(path),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
);
},
suggestionBuilder: (context, state, data) {
return ListTile(
key: ObjectKey(data),
title: Text(data),
onTap: () => state.selectSuggestion(data),
);
},
onChanged: (data) {
obj.paths = data;
},
findSuggestions: (String query) {
if (query.length != 0) {
return <String>[query];
}
return <String>[];
},
),
ChipsInput<String>(
decoration: InputDecoration(
labelText: "Протоколы",
),
key: ObjectKey(obj.protocols),
chipBuilder: (context, state, protocol) {
return InputChip(
key: ObjectKey(protocol),
label: Text(protocol),
onDeleted: () => state.deleteChip(protocol),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
);
},
suggestionBuilder: (context, state, data) {
return ListTile(
key: ObjectKey(data),
title: Text(data),
onTap: () => state.selectSuggestion(data),
);
},
onChanged: (data) {
obj.paths = data;
},
findSuggestions: (String query) {
if (query.length != 0) {
return <String>[query];
}
return <String>[];
},
),
],
),
),
),
actions: <Widget>[
FlatButton(
child: Text('Отмена'),
onPressed: () => Navigator.of(ctx).pop(),
),
RaisedButton(
color: Colors.blue,
child: Text('Создать'),
// CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.white)),
onPressed: () {
if (key.currentState.validate()) {
key.currentState.save();
if (obj.protocols.length == 0) {
obj.protocols.add('http');
}
api.RouteObjectApi()
.routesPost(
'noplugin',
route: obj,
)
.then((_) {
Navigator.of(ctx).pop();
flushbars.showSuccess(
ctx, 'Готово! Роут ' + obj.name + ' создан!');
}).catchError((e) {
var err = e as api.ApiException;
print('ERROR! ' + err.code.toString());
print('message: ' + err.message);
flushbars.showError(ctx, err.code.toString());
});
}
},
),
],
);
}
void _onFocusChange(){
debugPrint("Focus: "+focus.hasFocus.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment