Skip to main content

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

A spiritual continuation of flutter_platform_widgets library

example/lib/main.dart

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:material_ui/material_ui.dart' show ThemeMode;
import 'package:multi_value_listenable_builder_typed/multi_value_listenable_builder_typed.dart';
import 'package:platform_adaptive_widgets/platform_adaptive_widgets.dart';

import 'app/platform_scope.dart';
import 'app/theme_scope.dart';
import 'features/core/data/constants/const_theme.dart';
import 'features/root/views/root_tabs_view.dart';

void main() => runApp(const _ExampleApp());

/// Navigator entry point: `PlatformApp` + a scaffold-managed [RootTabsView]. Owns the app-wide theme
/// mode and platform override, publishing them via [ThemeScope] / [PlatformScope] so the About tab can
/// flip the appearance and the rendered platform.
class const _ExampleApp() extends StatefulWidget {
  @override
  State<_ExampleApp> createState() => _ExampleAppState();
}

class _ExampleAppState() extends State<_ExampleApp> {
  final _themeModeNotifier = ValueNotifier(ThemeMode.system);
  final _platformNotifier = ValueNotifier<TargetPlatform?>(null);

  @override
  void dispose() {
    if (kDebugMode) debugDefaultTargetPlatformOverride = null;
    _themeModeNotifier.dispose();
    _platformNotifier.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) => DualValueListenableBuilder<ThemeMode, TargetPlatform?>(
    firstListenable: _themeModeNotifier,
    secondListenable: _platformNotifier,
    builder: (_, themeMode, platformOverride, _) {
      // Flip the whole app's rendered platform above PlatformApp. The setter throws outside debug,
      // where a build only holds the device's own look anyway.
      if (kDebugMode) debugDefaultTargetPlatformOverride = platformOverride;

      return PlatformApp(
        title: 'Platform Adaptive Widgets',
        materialAppData: MaterialAppData(
          theme: ConstTheme.materialLightThemeData,
          darkTheme: ConstTheme.materialDarkThemeData,
          themeMode: themeMode,
        ),
        cupertinoAppData: CupertinoAppData(
          theme: ConstTheme.cupertinoThemeData(_cupertinoBrightnessFor(themeMode)),
        ),
        builder: (_, child) => ThemeScope(
          notifier: _themeModeNotifier,
          child: PlatformScope(notifier: _platformNotifier, child: child!),
        ),
        home: const RootTabsView(),
      );
    },
  );

  /// Cupertino has no `themeMode`. Map it to an explicit brightness, or `null` to follow the device
  /// (the `system` case).
  Brightness? _cupertinoBrightnessFor(ThemeMode themeMode) => switch (themeMode) {
    ThemeMode.system => null,
    ThemeMode.light => Brightness.light,
    ThemeMode.dark => Brightness.dark,
  };
}
4
likes
160
points
1.55k
downloads
screenshot

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A spiritual continuation of flutter_platform_widgets library

Homepage
Repository (GitHub)
View/report issues

Topics

#adaptive #widget #material-design #material3 #cupertino

License

MIT (license)

Dependencies

cupertino_ui, flutter, material_ui, minted_chronology

More

Packages that depend on platform_adaptive_widgets