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.

87 lines
2.8KB

  1. import 'package:farm_tpf/data/repository/repository.dart';
  2. import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/widget_search.dart';
  4. import 'package:farm_tpf/presentation/screens/control_device/widget_device_list.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. import 'bloc/device_bloc.dart';
  8. class ControlDeviceScreen extends StatefulWidget {
  9. @override
  10. _ControlDeviceScreenState createState() => _ControlDeviceScreenState();
  11. }
  12. class _ControlDeviceScreenState extends State<ControlDeviceScreen> {
  13. BuildContext? _blocContext;
  14. DeviceBloc _deviceBloc = DeviceBloc(repository: Repository());
  15. @override
  16. void initState() {
  17. super.initState();
  18. _deviceBloc.add(OpenScreen());
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. _blocContext = context;
  23. return Scaffold(
  24. backgroundColor: Colors.white,
  25. body: BlocProvider<DeviceBloc>(create: (context) => DeviceBloc(repository: Repository())..add(OpenScreen()), child: _buildContent()));
  26. }
  27. Widget _buildContent() {
  28. return SafeArea(
  29. child: Column(
  30. crossAxisAlignment: CrossAxisAlignment.start,
  31. children: [
  32. SizedBox(
  33. height: 8,
  34. ),
  35. Padding(
  36. padding: const EdgeInsets.all(8.0),
  37. child: Text(
  38. 'Thiết bị',
  39. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
  40. ),
  41. ),
  42. Padding(
  43. padding: const EdgeInsets.all(8.0),
  44. child: Text('Điều khiển thiết bị từ xa', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w200, color: Colors.black54)),
  45. ),
  46. SearchWidget(searchPressed: (value) {
  47. FocusScope.of(context).requestFocus(FocusNode());
  48. _deviceBloc.add(OnSearch(query: value));
  49. }),
  50. Expanded(
  51. child: BlocBuilder<DeviceBloc, DeviceState>(
  52. bloc: _deviceBloc,
  53. builder: (context, state) {
  54. if (state is DisplayDevice) {
  55. if (state.loading ?? false) {
  56. return Container(
  57. child: Center(
  58. child: CircularProgressIndicator(),
  59. ),
  60. );
  61. }
  62. if (state.devices != null) {
  63. return Container(
  64. child: WidgetDeviceList(
  65. devices: state.devices ?? [],
  66. deviceBloc: _deviceBloc,
  67. ),
  68. );
  69. }
  70. return Container();
  71. } else {
  72. return Container();
  73. }
  74. },
  75. ))
  76. ],
  77. ),
  78. );
  79. }
  80. }