Skip to main content

smart_arch 0.1.0 copy "smart_arch: ^0.1.0" to clipboard
smart_arch: ^0.1.0 copied to clipboard

Smart Flutter architecture and application infrastructure toolkit with state-agnostic core primitives.

example/lib/main.dart

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

void main() {
  runApp(const SmartArchExampleApp());
}

/// The main example application for [smart_arch].
class SmartArchExampleApp extends StatelessWidget {
  /// Creates a new [SmartArchExampleApp].
  const SmartArchExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'smart_arch example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const ExamplePage(),
    );
  }
}

/// A simple page demonstrating how to use [SmartApi] and [SmartState].
class ExamplePage extends StatefulWidget {
  /// Creates a new [ExamplePage].
  const ExamplePage({super.key});

  @override
  State<ExamplePage> createState() => _ExamplePageState();
}

class _ExamplePageState extends State<ExamplePage> {
  late final SmartApi api;
  SmartState<String> state = const SmartState.initial();

  @override
  void initState() {
    super.initState();

    api = SmartApi.create(baseUrl: 'https://jsonplaceholder.typicode.com');
  }

  Future<void> loadUser() async {
    setState(() {
      state = const SmartState.loading();
    });

    final result = await api.get<String>(
      '/users/1',
      parser: (json) => json['name'] as String,
    );

    setState(() {
      state = result.when(
        success: (data) => SmartState.success(data),
        failure: SmartState.failure,
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('smart_arch v0.1.0')),
      body: Center(
        child: switch (state.status) {
          SmartStatus.initial => const Text('Press the button'),
          SmartStatus.loading => const CircularProgressIndicator(),
          SmartStatus.success => Text(
            'User: ${state.data}',
            style: Theme.of(context).textTheme.headlineSmall,
          ),
          SmartStatus.failure => Text(state.error?.message ?? 'Unknown error'),
          SmartStatus.empty => const Text('No data'),
        },
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: state.isLoading ? null : loadUser,
        icon: const Icon(Icons.cloud_download),
        label: const Text('Load User'),
      ),
    );
  }
}
4
likes
150
points
158
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Smart Flutter architecture and application infrastructure toolkit with state-agnostic core primitives.

Repository (GitHub)
View/report issues

Topics

#flutter #architecture #networking #state-management #clean-architecture

License

unknown (license)

Dependencies

dio, flutter

More

Packages that depend on smart_arch