Last active
April 26, 2023 09:45
-
-
Save burhanrashid52/84487ac58d3bea117170305ca120a8e2 to your computer and use it in GitHub Desktop.
This file contains 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'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: const Scaffold( | |
body: Center( | |
child: MyWidget(), | |
), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatefulWidget { | |
const MyWidget({Key? key}) : super(key: key); | |
@override | |
State<MyWidget> createState() => _MyWidgetState(); | |
} | |
class _MyWidgetState extends State<MyWidget> { | |
double lerp = 0; | |
Color buttonColor = Colors.blue[100]!; | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
StatefulBuilder( | |
builder: (context, setBuilderState) { | |
return InexpensiveWidget( | |
color: Color.lerp(buttonColor, Colors.black, lerp)!, | |
onPressed: () { | |
setBuilderState(() { | |
lerp += 0.1; | |
if (lerp > 1) { | |
lerp = 0; | |
} | |
}); | |
if (lerp == 0.5) { | |
setState(() {}); | |
} | |
}, | |
); | |
}, | |
), | |
// Pretend `const` is not feasible here | |
// ignore: prefer_const_constructors | |
ExpensiveWidget(), | |
MaterialButton( | |
child: const Text("Open Dialog"), | |
onPressed: () => showAlertDialog(context), | |
) | |
], | |
), | |
); | |
} | |
} | |
class InexpensiveWidget extends StatelessWidget { | |
const InexpensiveWidget({ | |
Key? key, | |
required this.color, | |
required this.onPressed, | |
}) : super(key: key); | |
final VoidCallback onPressed; | |
final Color color; | |
@override | |
Widget build(BuildContext context) { | |
print(" building InexpensiveWidget"); | |
return TextButton( | |
onPressed: onPressed, | |
style: ButtonStyle(backgroundColor: MaterialStateProperty.all(color)), | |
child: const Text("I rebuild often"), | |
); | |
} | |
} | |
Future<void> showAlertDialog(BuildContext context) { | |
return showDialog<void>( | |
context: context, | |
builder: (BuildContext context) { | |
int count = 0; | |
return AlertDialog( | |
content: Column( | |
children: [ | |
StatefulBuilder( | |
builder: (BuildContext context, StateSetter setState) { | |
return MaterialButton( | |
child: Text('Inside Increment $count'), | |
onPressed: () { | |
setState(() { | |
count++; | |
}); | |
}, | |
); | |
}, | |
), | |
MaterialButton( | |
child: Text('Outside Increment $count'), | |
onPressed: () { | |
count++; | |
}, | |
) | |
], | |
), | |
); | |
}, | |
); | |
} | |
class ExpensiveWidget extends StatelessWidget { | |
const ExpensiveWidget({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
print('Painstakingly building ExpensiveWidget'); | |
return const Text("I'm expensive!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment