Skip to content

Instantly share code, notes, and snippets.

@savekirk
Created July 13, 2023 10:22
Show Gist options
  • Save savekirk/0bb9bf6b70c1ead75ded5555a50e183a to your computer and use it in GitHub Desktop.
Save savekirk/0bb9bf6b70c1ead75ded5555a50e183a to your computer and use it in GitHub Desktop.
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter(int value) {
setState(() {
_counter = value;
});
}
void _isEven() {
final isEven = _counter % 2 == 0 ? "Evennnn" : "that's odddd";
print(isEven);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: CounterDisplay(count: _counter, onIncrement: _incrementCounter),
floatingActionButton: FloatingActionButton(
onPressed: _isEven,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
class CounterDisplay extends StatelessWidget {
const CounterDisplay({required this.count, required this.onIncrement});
final int count;
final ValueSetter<int> onIncrement;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pushed the button this many times:',
),
Text(
'$count',
style: Theme.of(context).textTheme.headlineMedium,
),
TextButton(child: Text("Increment"), onPressed: () => onIncrement(count + 16))
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment