Created
September 12, 2024 17:36
-
-
Save bt1159/c26b9b57e337089f517bf4049a81b03d to your computer and use it in GitHub Desktop.
Provider.create isn't re-triggered during StatelesWidget.build()
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'; | |
import 'package:provider/provider.dart'; | |
import 'package:flutter/scheduler.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: CounterUpper(), | |
), | |
), | |
); | |
} | |
} | |
class _CounterUpperState extends State<CounterUpper> | |
with TickerProviderStateMixin { | |
Ticker? ticker; | |
double animationValue = 0; | |
@override | |
void initState() { | |
print('Running _CenterUpperState.initState()'); | |
super.initState(); | |
ticker = Ticker((Duration elapsed) { | |
if (elapsed.inSeconds - animationValue > 1) { | |
setState(() { | |
print( | |
'Running _CenterUpperState.setState() with animationValue: $animationValue and elapsed.inSeconds: ${elapsed.inSeconds}'); | |
animationValue = elapsed.inSeconds.toDouble(); | |
}); | |
} | |
if (elapsed.inSeconds > 5) { | |
ticker?.stop(); | |
} | |
}); | |
ticker!.start(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
print( | |
'Running _CenterUpperState.build() with animationValue: $animationValue'); | |
return ProviderHolder(animationValue: animationValue); | |
} | |
} | |
class ProviderHolder extends StatelessWidget { | |
const ProviderHolder({super.key, required this.animationValue}); | |
final double animationValue; | |
@override | |
Widget build(BuildContext context) { | |
print( | |
'Running ProviderHolder.build() with animationValue: $animationValue'); | |
return Provider<ValueWrapper>( | |
create: (_) { | |
print('Running Provider.create() with animationValue: $animationValue'); | |
return ValueWrapper(animationValue); | |
}, | |
child: ContentHolder(), | |
); | |
} | |
} | |
class ContentHolder extends StatelessWidget { | |
const ContentHolder({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
final double providerValue = | |
Provider.of<ValueWrapper>(context, listen: true).value; | |
print('Running ContentHolder.build() with providerValue: $providerValue'); | |
return Text(providerValue.toString()); | |
} | |
} | |
class ValueWrapper { | |
const ValueWrapper(this.value); | |
final double value; | |
} | |
class CounterUpper extends StatefulWidget { | |
const CounterUpper({super.key}); | |
@override | |
_CounterUpperState createState() => _CounterUpperState(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment