Dependency
flutter pub add http, provider
Bash
๋ณต์ฌ
Entity ์ถ๊ฐ
Response ๋ฅผ ํตํด ๋ฐ์์ฌ Entity๋ฅผ ๋ฏธ๋ฆฌ ์ ์ธํ๋ค
Promotion.dart
class Promotion {
final String brandName;
final String brandLogoURL;
final int boltPrice;
final int boltEffectiveness;
final int discountRate;
final int priceMax;
final int discountMax;
final String description;
Promotion({
this.brandName,
this.brandLogoURL,
this.boltPrice,
this.boltEffectiveness,
this.discountRate,
this.priceMax,
this.discountMax,
this.description
});
factory Promotion.fromJson(Map<String, dynamic> json) {
return Promotion(
brandName: json['id'],
brandLogoURL: json["brandLogoURL"],
boltPrice: json["boltPrice"],
boltEffectiveness: json['boltEffectiveness'],
discountRate: json['discountRate'],
priceMax: json['priceMax'],
discountMax: json['discountMax'],
description: json['description'],
);
}
}
Dart
๋ณต์ฌ
Provider ๊ตฌํ
๊ฐ์ฅ ํธ๋ ๋(?)์ธ Provider ํจํด์ผ๋ก ๊ตฌํํ๊ณ ์ ํฉ๋๋ค.
์ด ๋ถ๋ถ์์ ์ด์๊ฐ ์๋ ๋ถ๋ถ์ ๋ณดํต ์๋ import ๋ฅผ ์ง์ํ์ง๋ง http ๋ชจ๋์ ๊ฒฝ์ฐ ์๋ import ๋์ง ์๋ ๋ถ๋ถ์ด ์์์ต๋๋ค. ๊ทธ๋ฐ ๊ฒฝ์ฐ
import 'package:http/http.dart' as http;
Dart
๋ณต์ฌ
์ง์ import ํด์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
PromotionProvider.dart
import 'dart:convert';
import 'package:chai_booster/Entity/Promotion.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class PromotionProvider with ChangeNotifier {
List<Promotion> _promotions = [];
List<Promotion> getPromotionList() {
_fetchPromotion();
return _promotions;
}
// Action
void _fetchPromotion() async {
final response = await http.get(Uri.parse('http://localhost:5001/chai-booster/asia-northeast3/api/booster/v1/promotions'));
final List<Promotion> parsedResposne = jsonDecode(response.body).
map<Promotion>((json) => Promotion.fromJson(json)).toList();
_promotions.clear();
_promotions.addAll(parsedResposne);
notifyListeners();
}
}
Dart
๋ณต์ฌ
HomePage.dart
import 'package:chai_booster/Pages/Tabs/BrandAlarmSettingPage.dart';
import 'package:chai_booster/Pages/Tabs/MorePage.dart';
import 'package:chai_booster/Pages/Tabs/PromotionsPage.dart';
import 'package:chai_booster/Providers/Auth/AuthProvider.dart';
import 'package:chai_booster/Providers/Promotion/PromotionProvider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
class HomePage extends StatefulWidget {
HomePage({
Key key, this.title
}) : super(key: key);
final String title;
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
AuthProvider authProvider;
int _currentIndex = 0;
Widget build(BuildContext context) {
authProvider = Provider.of<AuthProvider>(context);
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: [
for (final tabItem in TabNavigationItem.items) tabItem.page,
],
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (int index) => setState(() => _currentIndex = index),
items: [
for (final tabItem in TabNavigationItem.items)
BottomNavigationBarItem(
icon: tabItem.icon,
label: tabItem.title,
)
],
),
);
}
}
class TabNavigationItem {
final Widget page;
final String title;
final Icon icon;
TabNavigationItem({
this.page,
this.title,
this.icon,
});
static List<TabNavigationItem> get items => [
TabNavigationItem(
page: ChangeNotifierProvider<PromotionProvider>(
create: (context) => PromotionProvider(),
child: PromotionPage(),
),
icon: Icon(Icons.money),
title: "ํํ",
),
TabNavigationItem(
page: BrandAlarmSettingPage(),
icon: Icon(Icons.alarm),
title: "์๋ฆผ",
),
TabNavigationItem(
page: MorePage(),
icon: Icon(Icons.more),
title: "๋๋ณด๊ธฐ",
),
];
}
Dart
๋ณต์ฌ
์ฝ๋๊ฐ ๊ธธ๋ค. ํ์ง๋ง ์ค์ํ ๋ถ๋ถ์ ์๋ ๋ถ๋ถ์ด๋ค.
static List<TabNavigationItem> get items => [
TabNavigationItem(
page: ChangeNotifierProvider<PromotionProvider>(
create: (context) => PromotionProvider(),
child: PromotionPage(),
),
icon: Icon(Icons.money),
title: "ํํ",
),
Dart
๋ณต์ฌ
PromotionPage ์์ state ๋ฅผ ์ฌ์ฉํ ์์ ์ด๊ธฐ ๋๋ฌธ์, PromotionPage ๋ฅผ ChangeNotifierProvider๋ก ๊ฐ์ธ์ค๋ค.
PromotionPage.dart
import 'package:chai_booster/Entity/Promotion.dart';
import 'package:chai_booster/Pages/HomePage.dart';
import 'package:chai_booster/Providers/Promotion/PromotionProvider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class PromotionPage extends StatefulWidget {
PromotionPage({
Key key, this.title
}) : super(key: key);
final String title;
_PromotionPageState createState() => _PromotionPageState();
}
class _PromotionPageState extends State<PromotionPage> {
Future<Promotion> promotions;
Widget build(BuildContext context) {
final promotionProvider = Provider.of<PromotionProvider>(context);
promotionProvider.getPromotionList();
return Scaffold(
appBar: AppBar(
title: Text('ํํ'),
centerTitle: false,
),
body: Center(
child: SizedBox(
child: ElevatedButton(
onPressed: () => {
},
child: Text('ํํ ๋ชฉ๋ก'),
),
width: double.infinity,
height: 44,
),
),
);
}
}
Dart
๋ณต์ฌ
์ค์ ์ด์ํ ์๋น์ค์์๋ ์ด๋ ๊ฒ ํ๋ฉด ์๋๋ค.
๋ฌธ์ ๊ฐ ๋๋ ๋ถ๋ถ์
promotionProvider.getPromotionList();
Dart
๋ณต์ฌ
์?
build ํจ์ ์์์๋ค๋ ๋ป์, widget ์ด ๊ทธ๋ ค์ง๋๋ง๋ค API request๊ฐ ๋ฐ์ํฉ๋๋ค. ๋ถํ์ํ ๊ณผ๋ํ ์์ฒญ์ด ๋ฐ์ํ๊ณ ๊ทธ์ ๋ฐ๋ผ ์ฑ์ด ๋ฒ๋ฒ
์ผ ์ ์์ต๋๋ค.
API Request ๋ฅผ ๋ฐ์ํ๋ ์์ , ๋ก์ง
isLoaded ๋ผ๋ ๋ณ์... ํ ... ์ฐ์ฐํ๋ค. reload ํ ํ์ด๋ฐ์ด ๋ช
ํํ์ง ์๊ธฐ ๋๋ฌธ