stability_sdk 0.1.2 copy "stability_sdk: ^0.1.2" to clipboard
stability_sdk: ^0.1.2 copied to clipboard

An implementation of Stability AI SDK in Dart. Stability AI is a solution studio dedicated to innovating ideas.

example/main.dart

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

class SingleImagePage extends StatefulWidget {
  const SingleImagePage({super.key});

  @override
  State<SingleImagePage> createState() => _SingleImagePageState();
}

class _SingleImagePageState extends State<SingleImagePage> {
  late StabilityApiClient client;
  late TextEditingController queryController;
  String? image;
  late bool hasInput;
  late bool isLoading;

  @override
  void initState() {
    hasInput = false;
    isLoading = false;
    client = StabilityApiClient.init(dotenv.get('STABILITY_API_KEY'));
    queryController = TextEditingController();
    queryController.addListener(() {
      if (queryController.text != null && queryController.text.isNotEmpty) {
        setState(() {
          hasInput = true;
        });
      } else {
        setState(() {
          hasInput = false;
        });
      }
    });

    super.initState();
  }

  Future<void> generateImage(String prompt) async {
    setState(() {
      isLoading = true;
      image = null;
    });

    final request = RequestBuilder(prompt)
        .setHeight(512)
        .setWidth(512)
        .setEngineType(EngineType.inpainting_v2_0)
        .setSampleCount(1)
        .build();

    client.generate(request).listen((answer) {
      if (answer.artifacts?.isNotEmpty == true) {
        setState(() {
          image = answer.artifacts?.first.getImage();
          isLoading = false;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Generate Image'),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              if (image != null && !isLoading)
                CachedMemoryImage(
                  base64: image,
                  uniqueKey: image.toString(),
                ),
              if (isLoading) const CircularProgressIndicator(),
              const SizedBox(height: 32),
              TextField(
                controller: queryController,
              ),
              const SizedBox(height: 32),
              ElevatedButton(
                onPressed: hasInput
                    ? () {
                        generateImage(queryController.text);
                      }
                    : null,
                child: const Text('Generate'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
17
likes
130
points
51
downloads

Publisher

verified publisherjoshuamdeguzman.com

Weekly Downloads

An implementation of Stability AI SDK in Dart. Stability AI is a solution studio dedicated to innovating ideas.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

fixnum, flutter, grpc, protobuf

More

Packages that depend on stability_sdk