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.

75 lines
2.1KB

  1. import 'package:farm_tpf/data/repository/repository.dart';
  2. import 'package:farm_tpf/presentation/custom_widgets/widget_search.dart';
  3. import 'package:farm_tpf/presentation/screens/control_device/widget_device_list.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_bloc/flutter_bloc.dart';
  6. import 'bloc/device_bloc.dart';
  7. class ControlDeviceScreen extends StatefulWidget {
  8. @override
  9. _ControlDeviceScreenState createState() => _ControlDeviceScreenState();
  10. }
  11. class _ControlDeviceScreenState extends State<ControlDeviceScreen> {
  12. BuildContext _blocContext;
  13. DeviceBloc _deviceBloc = DeviceBloc(repository: Repository());
  14. @override
  15. void initState() {
  16. super.initState();
  17. _deviceBloc.add(OpenScreen());
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. _blocContext = context;
  22. return Scaffold(
  23. appBar: AppBar(
  24. centerTitle: true,
  25. title: Text("Điều khiển thiết bị"),
  26. ),
  27. body: BlocProvider<DeviceBloc>(
  28. create: (context) =>
  29. DeviceBloc(repository: Repository())..add(OpenScreen()),
  30. child: _buildContent()));
  31. }
  32. Widget _buildContent() {
  33. return Column(
  34. children: [
  35. SearchWidget(searchPressed: (value) {
  36. FocusScope.of(context).requestFocus(FocusNode());
  37. _deviceBloc.add(OnSearch(query: value));
  38. }),
  39. Expanded(
  40. child: BlocBuilder<DeviceBloc, DeviceState>(
  41. cubit: _deviceBloc,
  42. builder: (context, state) {
  43. if (state is DisplayDevice) {
  44. if (state.loading) {
  45. return Container(
  46. child: Center(
  47. child: CircularProgressIndicator(),
  48. ),
  49. );
  50. }
  51. if (state.devices != null) {
  52. return Container(
  53. child: WidgetDeviceList(
  54. devices: state.devices,
  55. deviceBloc: _deviceBloc,
  56. ),
  57. );
  58. }
  59. return Container();
  60. } else {
  61. return Container();
  62. }
  63. },
  64. ))
  65. ],
  66. );
  67. }
  68. }