flutter-rp-example/lib/widgets/scan_flow/select_context_screen.dart
Nathan SOULIER fff5617757 First commit
2025-03-24 10:12:56 +01:00

119 lines
4.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:dart_core_sdk/proj.pbgrpc.dart';
import 'package:sampleapp/globals.dart';
import 'package:sampleapp/widgets/components/reflex_alert.dart';
import 'package:sampleapp/widgets/components/reflex_button.dart';
import 'package:sampleapp/widgets/components/reflex_circular_progress.dart';
import 'package:sampleapp/widgets/components/reflex_dropdown_button_form_field.dart';
import '../../locator.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class SelectContext extends StatefulWidget {
final void Function(String projectID) onContextSelected;
const SelectContext({super.key, required this.onContextSelected});
@override
_SelectContextState createState() => _SelectContextState();
}
class _SelectContextState extends State<SelectContext> {
final projectClient = locator.get<ProjectServiceClient>();
List<MyContext> contexts = [];
List<Project> projectList = [];
Project? selectedProject;
bool isLoading = true;
@override
void initState() {
super.initState();
_loadContexts();
}
Future<void> _loadContexts() async {
projectClient.getMyUIContext(GetMyContextRequest()).then((value) {
contexts = value.myContexts;
projectList = contexts.first.projects.map((idName) => Project(iD: idName.iD, name: idName.name)).toList();
projectList.sort((a, b) => a.name.compareTo(b.name));
selectedProject = projectList.isNotEmpty ? projectList.first : null;
}).whenComplete(() => setState(() {
isLoading = false;
}));
}
Widget _buildContextDropdowns() {
return ListView(shrinkWrap: true, children: [
ReflexDropdownButtonFormField<String>(
label: AppLocalizations.of(context)!.organization,
value: contexts.isNotEmpty ? contexts[0].organisation.iD : '',
items: contexts.map<DropdownMenuItem<String>>((MyContext c) {
return DropdownMenuItem<String>(
value: c.organisation.iD,
child: Text(c.organisation.name),
);
}).toList(),
// On change, update the project list
onChanged: (value) {
setState(() {
projectList = contexts.firstWhere((element) => element.organisation.iD == value).projects.map((idName) => Project(iD: idName.iD, name: idName.name)).toList();
projectList.sort((a, b) => a.name.compareTo(b.name));
selectedProject = projectList.isNotEmpty ? projectList.first : null;
});
},
),
ReflexDropdownButtonFormField<String>(
label: AppLocalizations.of(context)!.project,
value: projectList.isNotEmpty ? projectList[0].iD : '',
items: projectList.map<DropdownMenuItem<String>>((Project p) {
return DropdownMenuItem<String>(
value: p.iD,
child: Text(p.name),
);
}).toList(),
// On change, update the selected project
onChanged: (value) {
setState(() {
selectedProject =
projectList.firstWhere((element) => element.iD == value);
});
},
),
]);
}
bool _isButtonDisabled() {
return selectedProject == null;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: [
!isLoading
? _buildContextDropdowns()
: const ReflexCircularProgress(),
const SizedBox(height: 20),
ReflexAlert(
icon: Icons.info_outline_rounded,
text:
AppLocalizations.of(context)!.selectContext
),
const SizedBox(height: 20),
Align(
alignment: Alignment.centerRight,
child: ReflexButton(
text: AppLocalizations.of(context)!.start,
onPressed: _isButtonDisabled()
? null
: () {
widget.onContextSelected(selectedProject!.iD);
},
))
],
)));
}
}