Files
fcs/lib/pages/market/market_editor.dart

190 lines
5.3 KiB
Dart
Raw Normal View History

2020-10-07 02:33:06 +06:30
import 'package:fcs/domain/entities/market.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/market/model/market_model.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/input_text.dart';
2024-01-25 17:40:35 +06:30
import 'package:fcs/pages/widgets/local_app_bar.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
2020-09-16 02:29:50 +06:30
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
typedef void FindCallBack();
class MarketEditor extends StatefulWidget {
const MarketEditor();
@override
_MarketEditorState createState() => _MarketEditorState();
}
class _MarketEditorState extends State<MarketEditor> {
TextEditingController _marketNameCtl = new TextEditingController();
final _formKey = GlobalKey<FormState>();
2020-09-16 02:29:50 +06:30
bool _isLoading = false;
List<String> markets = [];
@override
void initState() {
super.initState();
}
List<Widget> showMarkets(BuildContext context, List<Market> markets) {
return markets.map((p) {
return new ListTile(
title: new Row(
children: <Widget>[
Expanded(
child: new Text(
2021-09-10 15:22:11 +06:30
p.name ?? "",
2020-09-16 02:29:50 +06:30
style: TextStyle(
fontSize: 15.0,
),
),
),
Spacer(),
IconButton(
icon: Icon(Icons.remove, color: primaryColor),
onPressed: () => _remove(p),
)
],
));
}).toList();
}
@override
Widget build(BuildContext context) {
List<Market> markets = Provider.of<MarketModel>(context).markets;
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
2024-01-25 17:40:35 +06:30
appBar: LocalAppBar(
labelKey: 'market.edit.title',
backgroundColor: Colors.white,
labelColor: primaryColor,
arrowColor: primaryColor),
2020-09-16 02:29:50 +06:30
body: Padding(
padding: const EdgeInsets.only(left: 12.0, right: 12),
child: ListView(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.only(right: 18.0),
child: IconButton(
icon: Icon(Icons.add, color: primaryColor),
onPressed: () => _showDialog(context),
),
),
],
),
Column(
children: showMarkets(context, markets),
),
SizedBox(
height: 10,
)
],
),
),
));
}
2024-01-23 16:28:08 +06:30
_remove(Market? market) {
2020-09-16 02:29:50 +06:30
if (market == null) {
showMsgDialog(context, "Esrror", "Invalid market!");
return;
}
showConfirmDialog(
context, "market.remove.confirm", () => _removeMarket(market));
}
_removeMarket(Market market) async {
setState(() {
_isLoading = true;
});
MarketModel marketModel = Provider.of<MarketModel>(context, listen: false);
try {
2021-09-10 15:22:11 +06:30
await marketModel.deleteMarket(market.id!);
2020-09-16 02:29:50 +06:30
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
_add() async {
if (_marketNameCtl.text == "") {
showMsgDialog(context, "Esrror", "Invalid market name!");
return;
}
setState(() {
_isLoading = true;
});
MarketModel marketModel = Provider.of<MarketModel>(context, listen: false);
try {
await marketModel.addMarket(_marketNameCtl.text);
_marketNameCtl.text = "";
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
_showDialog(BuildContext context) async {
await showDialog<String>(
context: context,
2021-09-10 12:00:08 +06:30
builder: (BuildContext context) {
return Form(
key: _formKey,
child: new AlertDialog(
contentPadding: const EdgeInsets.all(16.0),
content: new Row(
children: <Widget>[
new Expanded(
child: InputText(
labelTextKey: "market.edit.name",
controller: _marketNameCtl,
autoFocus: true,
validator: (value) {
if (value!.isEmpty) {
return "Enter market name";
}
return null;
},
),
)
],
),
actions: <Widget>[
new TextButton(
child: LocalText(context, "btn.cancel", color: primaryColor),
onPressed: () {
Navigator.pop(context);
}),
new TextButton(
child: LocalText(
context,
"btn.save",
color: primaryColor,
),
onPressed: () {
if (!_formKey.currentState!.validate()) return;
Navigator.pop(context);
_add();
})
2021-09-10 12:00:08 +06:30
],
),
);
},
2020-09-16 02:29:50 +06:30
);
}
}