Last active
April 6, 2024 07:41
-
-
Save junsuk5/10119ea8931203abbe7777c556fe2136 to your computer and use it in GitHub Desktop.
InheritedWidget 샘플
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp( | |
ChangeNotifierProvider<MyViewModel>( | |
value: MyViewModel(), | |
child: const MyApp(), | |
), | |
); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const MyViewModelTest(), | |
); | |
} | |
} | |
class ChangeNotifierProvider<T extends ChangeNotifier> extends InheritedWidget { | |
final T value; | |
const ChangeNotifierProvider({ | |
super.key, | |
required super.child, | |
required this.value, | |
}); | |
static ChangeNotifierProvider<T> of<T extends ChangeNotifier>( | |
BuildContext context) { | |
return context | |
.dependOnInheritedWidgetOfExactType<ChangeNotifierProvider<T>>()!; | |
} | |
@override | |
bool updateShouldNotify(ChangeNotifierProvider oldWidget) { | |
return value != oldWidget.value; | |
} | |
} | |
class MyViewModel with ChangeNotifier { | |
int _count = 0; | |
int get count => _count; | |
void increment() { | |
_count++; | |
notifyListeners(); | |
} | |
} | |
class MyViewModelTest extends StatefulWidget { | |
const MyViewModelTest({super.key}); | |
@override | |
State<MyViewModelTest> createState() => _MyViewModelTestState(); | |
} | |
class _MyViewModelTestState extends State<MyViewModelTest> { | |
@override | |
Widget build(BuildContext context) { | |
final viewModel = ChangeNotifierProvider.of<MyViewModel>(context).value; | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(viewModel.count.toString()), | |
), | |
body: Center( | |
child: Column( | |
children: [ | |
ElevatedButton( | |
onPressed: () { | |
setState(() { | |
viewModel.increment(); | |
}); | |
}, | |
child: const Text('+'), | |
), | |
ElevatedButton( | |
onPressed: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute(builder: (context) => const NextScreen()), | |
); | |
}, | |
child: const Text('Next'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class NextScreen extends StatefulWidget { | |
const NextScreen({super.key}); | |
@override | |
State<NextScreen> createState() => _NextScreenState(); | |
} | |
class _NextScreenState extends State<NextScreen> { | |
@override | |
Widget build(BuildContext context) { | |
final viewModel = ChangeNotifierProvider.of<MyViewModel>(context).value; | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(viewModel.count.toString()), | |
), | |
body: Center( | |
child: Column( | |
children: [ | |
ElevatedButton( | |
onPressed: () { | |
setState(() { | |
viewModel.increment(); | |
}); | |
}, | |
child: const Text('+'), | |
), | |
ElevatedButton( | |
onPressed: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute(builder: (context) => const NextScreen()), | |
); | |
}, | |
child: const Text('Next'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment