selector 3.0.0 copy "selector: ^3.0.0" to clipboard
selector: ^3.0.0 copied to clipboard

Multi type supported platform selector to write functional programming.

example/lib/main.dart

import 'dart:math';

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('platform selector')),
        body: Builder(builder: (context) => _buildBody(context)),
      ),
    );
  }

  _buildBody(BuildContext context) => Column(
        children: [
          TextButton(
              child: Text('selector'),
              onPressed: () => _showSnackBar(context, _selector())),
          TextButton(
              child: Text('deviceSelector'),
              onPressed: () => _showSnackBar(context, _deviceSelector())),
          TextButton(
              child: Text('parameter as function'),
              onPressed: () =>
                  _showSnackBar(context, _functionParameter().toString())),
          TextButton(
              child: Text('androidOrElse'),
              onPressed: () => _showSnackBar(context, _androidOrElse())),
          TextButton(
              child: Text('iosOrElse()'),
              onPressed: () => _showSnackBar(context, _iosOrElse())),
        ],
      );

  _showSnackBar(BuildContext context, String message) =>
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text(message)));

  String _selector() => selector<String>(
        android: 'hello Android',
        ios: 'hello iOS',
        fuchsia: 'hello Fuchsia',
        linux: 'hello Linux',
        mac: 'hello MacOS',
        windows: 'hello Windows',
      );

  _deviceSelector() => deviceSelector<String>(
        mobile: 'hello Mobile',
        desktop: 'hello Desktop',
        web: 'hello Web',
      );

  _functionParameter() => selector<Function(num, num)>(
        android: (a, b) => a + b,
        ios: (a, b) => a * b,
        windows: (a, b) => a / b,
        linux: (a, b) => pow(a, b),
        mac: (a, b) => sqrt(a) * b,
        fuchsia: (a, b) => a - b,
      )(1, 2);

  _androidOrElse() => androidOrElse<String>('android-admob-id', 'ios-admob-id');

  _iosOrElse() => iosOrElse<String>('ios-admob-id', 'android-admob-id');
}
21
likes
160
points
41
downloads

Publisher

unverified uploader

Weekly Downloads

Multi type supported platform selector to write functional programming.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on selector