Fault Description Became a Dropdown Menu
parent
29466092b0
commit
655f96049c
@ -0,0 +1,70 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:test_sa/controllers/api_routes/api_manager.dart';
|
||||
import 'package:test_sa/controllers/api_routes/urls.dart';
|
||||
import 'package:test_sa/models/user.dart';
|
||||
|
||||
import '../../../../../models/fault_description.dart';
|
||||
|
||||
class ServiceRequestFaultDescriptionProvider extends ChangeNotifier {
|
||||
//reset provider data
|
||||
void reset() {
|
||||
_items = null;
|
||||
_stateCode = null;
|
||||
}
|
||||
|
||||
// state code of current request to defied error message
|
||||
// like 400 customer request failed
|
||||
// 500 service not available
|
||||
int _stateCode;
|
||||
int get stateCode => _stateCode;
|
||||
|
||||
// contain user data
|
||||
// when user not login or register _user = null
|
||||
List<FaultDescription> _items;
|
||||
List<FaultDescription> get items => _items;
|
||||
|
||||
// when categories in-process _loading = true
|
||||
// done _loading = true
|
||||
// failed _loading = false
|
||||
bool _loading;
|
||||
bool get isLoading => _loading;
|
||||
set isLoading(bool isLoading) {
|
||||
_loading = isLoading;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// return -2 if request in progress
|
||||
/// return -1 if error happen when sending request
|
||||
/// return state code if request complete may be 200, 404 or 403
|
||||
/// for more details check http state manager
|
||||
/// lib\controllers\http_status_manger\http_status_manger.dart
|
||||
Future<int> getData({String host, User user, String requestId}) async {
|
||||
if (_loading == true) return -2;
|
||||
_loading = true;
|
||||
notifyListeners();
|
||||
Response response;
|
||||
try {
|
||||
response = await ApiManager.instance.get(
|
||||
URLs.getServiceReportFaultDescription + "?callId=$requestId",
|
||||
);
|
||||
_stateCode = response.statusCode;
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
// client's request was successfully received
|
||||
List listJson = json.decode(response.body)["data"]['asset']['modelDefinition']['modelDefRelatedDefects'];
|
||||
_items = listJson.map((type) => FaultDescription.fromJson(type)).toList();
|
||||
}
|
||||
_loading = false;
|
||||
notifyListeners();
|
||||
return response.statusCode;
|
||||
} catch (error) {
|
||||
_loading = false;
|
||||
_stateCode = -1;
|
||||
notifyListeners();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
class FaultDescription {
|
||||
FaultDescription({
|
||||
this.id,
|
||||
this.defectName,
|
||||
this.workPerformed,
|
||||
this.estimatedTime,});
|
||||
|
||||
FaultDescription.fromJson(dynamic json) {
|
||||
id = json['id'];
|
||||
defectName = json['defectName'];
|
||||
workPerformed = json['workPerformed'];
|
||||
estimatedTime = json['estimatedTime'];
|
||||
}
|
||||
num id;
|
||||
dynamic defectName;
|
||||
String workPerformed;
|
||||
String estimatedTime;
|
||||
FaultDescription copyWith({ num id,
|
||||
dynamic defectName,
|
||||
String workPerformed,
|
||||
String estimatedTime,
|
||||
}) => FaultDescription( id: id ?? this.id,
|
||||
defectName: defectName ?? this.defectName,
|
||||
workPerformed: workPerformed ?? this.workPerformed,
|
||||
estimatedTime: estimatedTime ?? this.estimatedTime,
|
||||
);
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['id'] = id;
|
||||
map['defectName'] = defectName;
|
||||
map['workPerformed'] = workPerformed;
|
||||
map['estimatedTime'] = estimatedTime;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,110 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:test_sa/models/fault_description.dart';
|
||||
import 'package:test_sa/views/app_style/colors.dart';
|
||||
import 'package:test_sa/views/app_style/sizing.dart';
|
||||
|
||||
class FaultDescriptionMenu extends StatefulWidget {
|
||||
final List<FaultDescription> statuses;
|
||||
final FaultDescription initialStatus;
|
||||
final Function(FaultDescription) onSelect;
|
||||
|
||||
const FaultDescriptionMenu({Key key, this.statuses, this.onSelect, this.initialStatus}) : super(key: key);
|
||||
|
||||
@override
|
||||
_SingleStatusMenuState createState() => _SingleStatusMenuState();
|
||||
}
|
||||
|
||||
class _SingleStatusMenuState extends State<FaultDescriptionMenu> {
|
||||
FaultDescription _selectedStatus;
|
||||
|
||||
@override
|
||||
void setState(VoidCallback fn) {
|
||||
if (mounted) super.setState(fn);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant FaultDescriptionMenu oldWidget) {
|
||||
if (widget.initialStatus != null) {
|
||||
final result = widget.statuses?.where((element) {
|
||||
return element == widget.initialStatus;
|
||||
});
|
||||
if (result.isNotEmpty) {
|
||||
_selectedStatus = result.first;
|
||||
} else {
|
||||
_selectedStatus = null;
|
||||
}
|
||||
if ((widget.initialStatus?.id ?? "") != (_selectedStatus?.id ?? "")) {
|
||||
widget.onSelect(_selectedStatus);
|
||||
}
|
||||
} else {
|
||||
_selectedStatus = null;
|
||||
}
|
||||
super.didUpdateWidget(oldWidget);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
if (widget.initialStatus != null) {
|
||||
final result = widget.statuses?.where((element) {
|
||||
return element == widget.initialStatus;
|
||||
});
|
||||
if (result.isNotEmpty) _selectedStatus = result.first;
|
||||
if (widget.initialStatus.id != _selectedStatus?.id) {
|
||||
widget.onSelect(_selectedStatus);
|
||||
}
|
||||
}
|
||||
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
decoration: BoxDecoration(
|
||||
color: AColors.inputFieldBackgroundColor,
|
||||
border: Border.all(
|
||||
color: Color(0xffefefef),
|
||||
),
|
||||
borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
|
||||
// boxShadow: const [
|
||||
// AppStyle.boxShadow
|
||||
// ]
|
||||
),
|
||||
child: DropdownButton<FaultDescription>(
|
||||
value: _selectedStatus,
|
||||
iconSize: 24,
|
||||
icon: const Icon(Icons.keyboard_arrow_down_rounded),
|
||||
elevation: 0,
|
||||
isExpanded: true,
|
||||
hint: Text(
|
||||
"Select",
|
||||
style: Theme.of(context).textTheme.subtitle1,
|
||||
),
|
||||
style: TextStyle(color: Theme.of(context).primaryColor),
|
||||
underline: SizedBox.shrink(),
|
||||
onChanged: (FaultDescription newValue) {
|
||||
setState(() {
|
||||
_selectedStatus = newValue;
|
||||
});
|
||||
widget.onSelect(newValue);
|
||||
},
|
||||
items: widget.statuses.map<DropdownMenuItem<FaultDescription>>(
|
||||
(FaultDescription value) {
|
||||
return DropdownMenuItem<FaultDescription>(
|
||||
value: value,
|
||||
child: Text(
|
||||
value.defectName,
|
||||
style: Theme.of(context).textTheme.subtitle1.copyWith(
|
||||
color: Theme.of(context).primaryColor,
|
||||
fontSize: 11,
|
||||
//fontWeight: FontWeight.bold
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_fault_description_provider.dart';
|
||||
import 'package:test_sa/controllers/providers/api/user_provider.dart';
|
||||
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
|
||||
import 'package:test_sa/models/fault_description.dart';
|
||||
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
|
||||
import 'package:test_sa/views/widgets/status/report/fault_desc_menu.dart';
|
||||
|
||||
import '../../app_text_form_field.dart';
|
||||
|
||||
class ServiceReportFaultDescription extends StatelessWidget {
|
||||
final String requestId;
|
||||
final Function(FaultDescription) onSelect;
|
||||
final FaultDescription initialValue;
|
||||
|
||||
const ServiceReportFaultDescription({Key key, this.requestId, this.onSelect, this.initialValue}) : super(key: key);
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
SettingProvider settingProvider = Provider.of<SettingProvider>(context);
|
||||
UserProvider userProvider = Provider.of<UserProvider>(context);
|
||||
ServiceRequestFaultDescriptionProvider menuProvider = Provider.of<ServiceRequestFaultDescriptionProvider>(context);
|
||||
return LoadingManager(
|
||||
isLoading: menuProvider.isLoading,
|
||||
isFailedLoading: menuProvider.items == null,
|
||||
stateCode: menuProvider.stateCode,
|
||||
onRefresh: () async {
|
||||
menuProvider.reset();
|
||||
await menuProvider.getData(user: userProvider.user, host: settingProvider.host, requestId: requestId);
|
||||
},
|
||||
child: menuProvider.items.isEmpty
|
||||
? const ATextFormField(
|
||||
initialValue: "NULL",
|
||||
enable: false,
|
||||
)
|
||||
: FaultDescriptionMenu(
|
||||
initialStatus: initialValue,
|
||||
statuses: menuProvider.items,
|
||||
onSelect: onSelect,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue