bus_stoppable_widget 0.0.3 copy "bus_stoppable_widget: ^0.0.3" to clipboard
bus_stoppable_widget: ^0.0.3 copied to clipboard

A minimal event stoppable widget.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:bus_stoppable_widget/bus_stoppable_widget.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const ExampleApp());
}

class EventList {
  static const String exampleSwitch = 'example-switch';
}

class ExampleApp extends BusStoppableWidget {
  const ExampleApp({super.key});

  @override
  BusStoppableState<ExampleApp> createState() => _ExampleAppState();
}

class _ExampleAppState extends BusStoppableState<ExampleApp> {
  final Color backgroundColorOn = Colors.green;
  final Color backgroundColorOff = Colors.red;
  final Color textColor = Colors.white;
  bool status = false;

  @override
  void initState() {
    super.initState();
    // SUBSCRIBE WHEN YOU NEED IT
    subscribe(EventList.exampleSwitch, exampleSwitchCallback);
    // UNSUBSCRIBE WHEN YOU NEED IT
    // unsubscribe(EventList.exampleSwitch;
  }

  @override
  Widget build(BuildContext context) {
    Color backgroundColor = status ? backgroundColorOn : backgroundColorOff;
    String text = status ? 'ON' : 'OFF';

    return MaterialApp(
      home: Scaffold(
        body: GestureDetector(
          // PUBLISH WHEN YOU NEED IT
          onTap: () => publish(EventList.exampleSwitch),
          child: Container(
            width: double.maxFinite,
            height: double.maxFinite,
            color: backgroundColor,
            child: Center(
              child: Text(
                text,
                style: TextStyle(
                  color: textColor,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  void exampleSwitchCallback(_) {
    setState(() {
      status = !status;
    });
  }
}
2
likes
145
points
39
downloads

Publisher

unverified uploader

Weekly Downloads

A minimal event stoppable widget.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on bus_stoppable_widget