amplify_api 0.0.1-dev.6 copy "amplify_api: ^0.0.1-dev.6" to clipboard
amplify_api: ^0.0.1-dev.6 copied to clipboard

outdated

The Amplify Flutter API category plugin.

example/lib/main.dart

/*
 * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import 'dart:typed_data';

import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_api_example/graphql_api_view.dart';
import 'package:amplify_api_example/rest_api_view.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:flutter/material.dart';
import 'amplifyconfiguration.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isAmplifyConfigured = false;
  bool _showRestApiView = true;

  Amplify amplify = Amplify();
  AmplifyAPI apiRest;

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

  void _configureAmplify() async {
    apiRest = AmplifyAPI();

    await amplify.addPlugin(apiPlugins: [apiRest]);
    await amplify.configure(amplifyconfig);
    setState(() {
      _isAmplifyConfigured = true;
    });
  }

  void _onRestApiViewButtonClick() {
    setState(() {
      _showRestApiView = true;
    });
  }

  void _onGraphQlApiViewButtonClick() {
    setState(() {
      _showRestApiView = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
              title: Padding(
                  padding: EdgeInsets.all(10.0),
                  child: Row(children: [
                    RaisedButton(
                        child: Text("Rest API"),
                        onPressed: _onRestApiViewButtonClick),
                    RaisedButton(
                        child: Text("GraphQL API"),
                        onPressed: _onGraphQlApiViewButtonClick)
                  ]))),
          body: Padding(
              padding: EdgeInsets.all(10.0),
              child: _showRestApiView == true
                  ? RestApiView()
                  : GraphQLApiView(isAmplifyConfigured: _isAmplifyConfigured))),
    );
  }
}