Skip to content

Instantly share code, notes, and snippets.

@metatronz
Created April 16, 2020 17:01
Show Gist options
  • Save metatronz/c80af6a36f4d6a29041ef09534a6b8e5 to your computer and use it in GitHub Desktop.
Save metatronz/c80af6a36f4d6a29041ef09534a6b8e5 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final GlobalKey<_CounterWidgetState> _key = GlobalKey();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Desktop',
home: Scaffold(
backgroundColor: Colors.blueGrey,
appBar: AppBar(
title: Text('I am Apollo'),
centerTitle: true,
backgroundColor: Colors.blueGrey[900],
),
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
'Tap "-" to decrement',
style: TextStyle(color: Colors.white54),
),
Card(
color: Colors.white54,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
padding: const EdgeInsets.all(0.0),
icon: Icon(Icons.remove),
onPressed: () {
_key.currentState.pressRemove();
}),
CounterWidget(
key: _key,
),
IconButton(
padding: const EdgeInsets.all(0.0),
icon: Icon(Icons.add),
onPressed: () {
_key.currentState.pressPlus();
}),
],
),
),
Text('Tap "+" to increment',
style: TextStyle(color: Colors.white54)),
]),
),
),
);
}
}
class CounterWidget extends StatefulWidget {
CounterWidget({Key key}) : super(key: key);
@override
_CounterWidgetState createState() => _CounterWidgetState();
}
class _CounterWidgetState extends State<CounterWidget> {
int counter = 50;
@override
Widget build(BuildContext context) {
return Text('$counter');
}
pressRemove() => setState(() {
counter--;
});
pressPlus() => setState(() {
counter++;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment