Created
June 22, 2020 06:56
-
-
Save alexmercerind/2f769c04563407e030390478688672d1 to your computer and use it in GitHub Desktop.
Flutter Stateful Widget
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(new Application()); | |
} | |
class Counter extends StatefulWidget { | |
final String name; | |
Counter(this.name); | |
_CounterState createState() => _CounterState(); | |
} | |
class _CounterState extends State<Counter> { | |
int counterValue = 0; | |
void incrementCounter() { | |
setState(() => counterValue++); | |
print('(' + MediaQuery.of(context).size.width.toString() + ', ' + MediaQuery.of(context).size.height.toString() + ')'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return( | |
Container( | |
padding: EdgeInsets.all(10), | |
margin: EdgeInsets.all(10), | |
decoration: BoxDecoration(border: Border.all(width: 0.5, color: Colors.blueAccent)), | |
alignment: Alignment.center, | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
mainAxisSize: MainAxisSize.max, | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
Text('${widget.name} : $counterValue', style: TextStyle(fontSize: 18)), | |
IconButton(enableFeedback: true, icon: Icon(Icons.add), onPressed: incrementCounter, color: Colors.blueAccent) | |
], | |
), | |
) | |
); | |
} | |
} | |
class Application extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: "Hello World!", | |
theme: ThemeData(primarySwatch: Colors.blue), | |
home: Scaffold( | |
appBar: AppBar(title: Text("Hello World!")), | |
body: Counter("The current counter value") | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment