Created
July 3, 2021 16:30
-
-
Save gazialankus/2639cd386209dc80531637bec2d71b0b 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'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
print('MyHomePage is rebuilt with $_counter'); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
FixedTextWidget(), | |
CountDisplayWidget(counter: _counter), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} | |
class FixedTextWidget extends StatelessWidget { | |
const FixedTextWidget({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
print('FixedText is rebuilt'); | |
return Text( | |
'You have pushed the button this many times:', | |
); | |
} | |
} | |
class CountDisplayWidget extends StatelessWidget { | |
const CountDisplayWidget({ | |
Key? key, | |
required int counter, | |
}) : _counter = counter, super(key: key); | |
final int _counter; | |
@override | |
Widget build(BuildContext context) { | |
print('CountDisplay is rebuilt with $_counter'); | |
return Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headline4, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment