Skip to main content

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

PlatformiOSweb

Application foundation for Flutter apps: fragment navigation with optional go_router sync, view models, token handling and common widgets.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutterx_application/flutterx_application.dart';
import 'package:flutterx_live_data/flutterx_live_data.dart';
import 'package:go_router/go_router.dart';

const double _m8 = 8;
const double _m16 = 16;

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

/// Two screens: fragments navigated without a router, and fragments synchronized with the location of go_router.
class ExampleApp extends StatelessWidget {
  static final GoRouter _router = GoRouter(
    routes: [
      GoRoute(path: '/', builder: (context, state) => const _PlainPage()),
      GoRoute(path: '/routed', builder: (context, state) => const _RoutedPage()),
    ],
  );

  /// Creates the app.
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) => ViewModelScope(
    child: MaterialApp.router(title: 'flutterx_application example', routerConfig: _router),
  );
}

/// A view model that counts, tracking its slow increment as a task.
class _CounterViewModel extends ViewModel with TaskViewModel {
  /// The current count.
  final MutableLiveData<int> count = MutableLiveData(value: 0);

  _CounterViewModel(super.provider);

  /// The view model of the closest scope.
  static _CounterViewModel of(BuildContext context) => context.viewModel(_CounterViewModel.new);

  /// Increments the count after a short delay, tracked as the loading task of the view model.
  Future<void> increment() async {
    await Future<void>.delayed(const Duration(milliseconds: 800)).notify(this);
    count.value++;
  }
}

/// A fragment showing its name and the arguments it was opened with.
class _LabelFragment extends Fragment {
  static const FragmentKey first = FragmentKey('first');
  static const FragmentKey second = FragmentKey('second');
  static const FragmentKey third = FragmentKey('third');

  final String label;

  const _LabelFragment({required super.key, required this.label});

  @override
  FragmentState<_LabelFragment> createState() => _LabelFragmentState();
}

class _LabelFragmentState extends FragmentState<_LabelFragment> {
  @override
  Widget build(BuildContext context) => Center(
    child: Column(
      mainAxisSize: MainAxisSize.min,
      spacing: _m8,
      children: [
        Text(widget.label, style: Theme.of(context).textTheme.headlineMedium),
        Text('active: $active, args: ${args<RouteArgs?>()}'),
        for (final key in const [_LabelFragment.first, _LabelFragment.second, _LabelFragment.third])
          if (key != widget.key)
            OutlinedButton(
              onPressed: () => manager.navigateToKey(key, args: {'from': widget.key.name}),
              child: Text('Go to ${key.name}'),
            ),
        ActionButton<void>(
          label: 'Back',
          onAction: manager.back,
          enabled: manager.canGoBack,
          buttonBuilder: ActionButton.buttonOutlined,
        ),
      ],
    ),
  );
}

List<Fragment> _fragments() => const [
  _LabelFragment(key: _LabelFragment.first, label: 'First'),
  _LabelFragment(key: _LabelFragment.second, label: 'Second'),
  _LabelFragment(key: _LabelFragment.third, label: 'Third'),
];

/// Fragments navigated by their manager only: the location of the router is left alone.
class _PlainPage extends StatefulWidget {
  const _PlainPage();

  @override
  State<_PlainPage> createState() => _PlainPageState();
}

class _PlainPageState extends State<_PlainPage> {
  final FragmentManager _manager = FragmentManager(key: 'plain');

  @override
  Widget build(BuildContext context) {
    final viewModel = _CounterViewModel.of(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text('Fragments without router sync'),
        flexibleSpace: AppBarLoader(loading: viewModel.loading),
        actions: [TextButton(onPressed: () => context.go('/routed'), child: const Text('Routed'))],
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(_m16),
            child: Row(
              spacing: _m16,
              children: [
                ActionButton<void>(
                  label: 'Increment',
                  onAction: viewModel.increment,
                  buttonBuilder: ActionButton.buttonFilled,
                ),
                LiveDataBuilder<int>(data: viewModel.count, builder: (context, count) => Text('Count: $count')),
              ],
            ),
          ),
          Expanded(
            child: FragmentHost(manager: _manager, syncLocationFragment: false, fragments: _fragments()),
          ),
        ],
      ),
    );
  }
}

/// Fragments whose key is mirrored in the fragment of the location, so a link such as `/routed#second` opens one.
class _RoutedPage extends StatefulWidget {
  const _RoutedPage();

  @override
  State<_RoutedPage> createState() => _RoutedPageState();
}

class _RoutedPageState extends State<_RoutedPage> {
  final FragmentManager _manager = FragmentManager(key: 'routed');

  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      title: const Text('Fragments synced with go_router'),
      actions: [TextButton(onPressed: () => context.go('/'), child: const Text('Plain'))],
    ),
    body: FragmentHost(manager: _manager, fragments: _fragments()),
  );
}
4
likes
140
points
307
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Application foundation for Flutter apps: fragment navigation with optional go_router sync, view models, token handling and common widgets.

Repository (GitLab)
View/report issues

License

BSD-3-Clause (license)

Dependencies

collection, flutter, flutterx_live_data, flutterx_preferences, flutterx_utils, go_router, pointer_interceptor

More

Packages that depend on flutterx_application