|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:woocommerce_api/woocommerce_api.dart'; |
| 3 | + |
| 4 | +void main() => runApp(MyApp()); |
| 5 | + |
| 6 | +class MyApp extends StatelessWidget { |
| 7 | + @override |
| 8 | + Widget build(BuildContext context) { |
| 9 | + return MaterialApp( |
| 10 | + title: 'Flutter Demo', |
| 11 | + theme: ThemeData( |
| 12 | + primarySwatch: Colors.blue, |
| 13 | + ), |
| 14 | + home: MyHomePage(title: 'WooCommerce API Demo'), |
| 15 | + ); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class MyHomePage extends StatefulWidget { |
| 20 | + MyHomePage({Key key, this.title}) : super(key: key); |
| 21 | + |
| 22 | + final String title; |
| 23 | + |
| 24 | + @override |
| 25 | + _MyHomePageState createState() => _MyHomePageState(); |
| 26 | +} |
| 27 | + |
| 28 | +class _MyHomePageState extends State<MyHomePage> { |
| 29 | + Future _getProducts() async { |
| 30 | + // Initialize the API |
| 31 | + WooCommerceAPI wooCommerceAPI = WooCommerceAPI( |
| 32 | + url: "https://www.yourwebsite.com", |
| 33 | + consumerKey: "ck_your_consumer_key", |
| 34 | + consumerSecret: "cs_your_consumer_secret"); |
| 35 | + |
| 36 | + // Get data using the "products" endpoint |
| 37 | + var products = await wooCommerceAPI.getAsync("products"); |
| 38 | + return products; |
| 39 | + } |
| 40 | + |
| 41 | + @override |
| 42 | + Widget build(BuildContext context) { |
| 43 | + return Scaffold( |
| 44 | + appBar: AppBar( |
| 45 | + title: Text(widget.title), |
| 46 | + ), |
| 47 | + body: FutureBuilder( |
| 48 | + future: _getProducts(), |
| 49 | + builder: (BuildContext context, AsyncSnapshot snapshot) { |
| 50 | + if (snapshot.hasData) { |
| 51 | + // Create a list of products |
| 52 | + return ListView.builder( |
| 53 | + itemCount: snapshot.data.length, |
| 54 | + itemBuilder: (BuildContext context, int index) { |
| 55 | + return ListTile( |
| 56 | + leading: CircleAvatar( |
| 57 | + child: |
| 58 | + Image.network(snapshot.data[index]["images"][0]["src"]), |
| 59 | + ), |
| 60 | + title: Text(snapshot.data[index]["name"]), |
| 61 | + subtitle: |
| 62 | + Text("Buy now for \$ " + snapshot.data[index]["price"]), |
| 63 | + ); |
| 64 | + }, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // Show a circular progress indicator while loading products |
| 69 | + return Center( |
| 70 | + child: CircularProgressIndicator(), |
| 71 | + ); |
| 72 | + }, |
| 73 | + ), |
| 74 | + ); |
| 75 | + } |
| 76 | +} |
0 commit comments