store 여러 개 사용
class Store1 extends ChangeNotifier {
var name = 'john kim';
}
class Store2 extends ChangeNotifier {
var follower = 0;
follower 변경함수들~~
}
store가 많은 경우 store 등록하는 법
MultiProvider(
providers: [
ChangeNotifierProvider(create: (c) => Store1()),
ChangeNotifierProvider(create: (c) => Store2()),
],
child: MaterialApp( 어쩌구 ),
),
Provider 사용 시 get 요청
두 가지 방법
- Profile의 initState안에서 get요청 날리고 get요청이 완료되면 state수정함수를 작동시키기
- Profile의 initState안에서 state수정함수를 작동시켜서 state수정함수 안에서 get요청을 날리
- store 안에 보관하는 법
- profileImage라는 state를 만들기
- get요청해서 그 결과를 profileImage 라는 state에 넣어주는 getData() 함수를 만들기
- class Store1 extends ChangeNotifier { var profileImage = []; getData() async { var result = await http.get(Uri.parse('https://codingapple1.github.io/app/profile.json')); var result2 = jsonDecode(result.body); profileImage = result2; notifyListeners(); } }
- 실행하기
- ElevatedButton( onPressed: (){ context.read<Store1>().getData(); }, child: Text('사진가져오기') )
전체 코드
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import './style.dart' as style;
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter/rendering.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
import 'package:provider/provider.dart';
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider( create: (c) => Store1()),
ChangeNotifierProvider( create: (c) => Store2()),
],
child: MaterialApp(
theme: style.theme,
home: MyApp()
),
));
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var tab = 0; // 1. 현재 상태 저장
var list = [1, 2, 3];
var map = {'name':'john', 'age':20};
var data = [];
var userImage; // 유저가 삽입한 이미지 저장 공간
var userContent; // 유저가 입력한 글 저장 공간
saveData() async {
var storage = await SharedPreferences.getInstance();
var map = {'age' : 20};
storage.setString('map', jsonEncode(map));
var result = storage.getString('map') ?? '업는데요';
print(jsonDecode(result)['age']);
}
addMyData() {
var myData = {
'id': data.length,
'image': userImage,
'likes': 5,
'date': 'July 25',
'content': userContent,
'liked': false,
'user': 'John Kim'
};
setState(() {
data.insert(0, myData);
});
}
setUserContent(a) {
setState(() {
userContent = a;
});
}
addData(a) {
setState(() {
data.add(a);
});
}
getData() async {
var result = await http.get(Uri.parse('https://codingapple1.github.io/app/data.json'));
var result2 = jsonDecode(result.body);
setState(() {
data = result2;
});
}
@override
void initState() {
super.initState();
saveData();
getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Instagram'),
actions: [
IconButton(
icon: Icon(Icons.add_box_outlined),
onPressed: () async {
var picker = ImagePicker();
var image = await picker.pickImage(source: ImageSource.gallery);
if(image != null) {
setState(() {
userImage = File(image.path);
});
}
Navigator.push(context,
MaterialPageRoute(builder: (c) => Upload( userImage : userImage, setUserContent : setUserContent, addMyData : addMyData) )
);
},
iconSize: 30,
)
]),
body: [Home(data : data, addData : addData), Text('샵페이지')][tab], // 2. state에 따라 tab 보이는 상태 변경
bottomNavigationBar: BottomNavigationBar(
showSelectedLabels: false,
showUnselectedLabels: false,
onTap: (i){
setState(() {
tab = i;
});
},
items: [
BottomNavigationBarItem(icon: Icon(Icons.home_outlined), label: '홈'),
BottomNavigationBarItem(icon: Icon(Icons.shopping_bag_outlined), label: '샵')
],
),
);
}
}
class Home extends StatefulWidget {
const Home({Key? key, this.data, this.addData}) : super(key: key);
final data, addData;
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
var scroll = ScrollController();
getMore() async {
var result = await http.get(Uri.parse('https://codingapple1.github.io/app/more1.json'));
var result2 = jsonDecode(result.body);
widget.addData(result2);
}
@override
void initState() {
super.initState();
scroll.addListener(() { // 스크롤바 높이 측정 코드
if(scroll.position.pixels == scroll.position.maxScrollExtent) {
getMore();
}
});
}
@override
Widget build(BuildContext context) {
if (widget.data.isNotEmpty) {
return ListView.builder(
itemCount: widget.data.length,
controller: scroll,
itemBuilder: (c, i) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
widget.data[i]['image'].runtimeType == String ? Image.network(widget.data[i]['image']) : Image.file(widget.data[i]['image']),
Container(
constraints: BoxConstraints(maxWidth: 600),
padding: EdgeInsets.all(20),
width: double.infinity,
child: Column(
children: [
GestureDetector(
child: Text(widget.data[i]['user']),
onTap: () {
Navigator.push(context,
PageRouteBuilder(pageBuilder: (c, a1, a2) => Profile(),
transitionsBuilder: (c, a1, a2, child) =>
FadeTransition(opacity: a1, child: child)
)
);
},
),
Text('좋아요 ${widget.data[i]['likes'].toString()}'),
Text(widget.data[i]['date']),
Text(widget.data[i]['content'])
],
),
)
],
);
}
);
} else {
return Text('로딩중임');
}
}
}
class Upload extends StatelessWidget {
const Upload({Key? key, this.userImage, this.setUserContent, this.addMyData}) : super(key: key);
final userImage, setUserContent, addMyData;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(actions: [
IconButton(onPressed: () {
addMyData();
}, icon: Icon(Icons.send))
],),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconButton(
onPressed: (){
Navigator.pop(context);
},
icon: Icon(Icons.close)
),
Image.file(userImage),
Text('이미지업로드화면'),
TextField(onChanged: (text) { // text는 유저가 입력한 글
setUserContent(text); // TextField()에 입력값 변할 때마다 onChanged 안의 함수가 실행됨
},),
],
)
);
}
}
class Store2 extends ChangeNotifier {
var name = 'john kim';
changeName() {
name = 'john park';
notifyListeners(); // 재렌더링
}
}
class Store1 extends ChangeNotifier { // state 보관함
var follower = 0;
var friend = false;
var profileImage = [];
getData() async {
var result = await http.get(Uri.parse('https://codingapple1.github.io/app/profile.json'));
var result2 = jsonDecode(result.body);
profileImage = result2;
notifyListeners();
}
addFollower() {
if(friend == false) {
friend = true;
follower++;
} else {
friend = false;
follower--;
}
notifyListeners();
}
}
class Profile extends StatelessWidget {
const Profile({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(context.watch<Store2>().name),),
body: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
CircleAvatar(radius: 30, backgroundColor: Colors.grey,),
Text('팔로우 ${context.watch<Store1>().follower}명'),
ElevatedButton(onPressed: () {
context.read<Store1>().addFollower();
}, child: Text('팔로우')),
ElevatedButton(onPressed: () {
context.read<Store1>().getData();
}, child: Text('사진 가져오기')),
]
)
);
}
}
'Web & Android > Flutter' 카테고리의 다른 글
[Flutter] notification 알림 주는 법 (1) | 2023.09.23 |
---|---|
[Flutter] GridView, CustomScrollView 프로필 페이지 만들기 (0) | 2023.09.23 |
[Flutter] 친구 팔로우 하기 (1) | 2023.09.23 |
[Flutter] Provider : 3-step 다른 방법 (0) | 2023.09.23 |
[Flutter] 커스텀 페이지 전환 애니메이션 (0) | 2023.09.23 |