mirror of https://github.com/flutter/samples.git
`simplistic_calculator`: Breaking apart into a bunch of steps (#1033)
parent
6d7ff34d62
commit
3cfdc5927e
@ -1 +1,6 @@
|
||||
include: ../analysis_options.yaml
|
||||
|
||||
# Files under typer/ are partially completed files, and often invalid
|
||||
analyzer:
|
||||
exclude:
|
||||
- typer/**
|
||||
|
@ -0,0 +1,33 @@
|
||||
// Copyright 2022 The Flutter team. 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(
|
||||
const CalculatorApp(),
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
runApp(
|
||||
const CalculatorApp(),
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const CalculatorApp(),
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const CalculatorApp(),
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
final String buffer;
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
final String buffer;
|
||||
final String error;
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
this.buffer,
|
||||
this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine(CalculatorState state) : super(state);
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {}
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends StatelessWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'Hello GDSC!',
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: const Center(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: Center(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
style: TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: Center(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: Center(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
children: [
|
||||
Text(
|
||||
state.buffer,
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
columnSizes: [1.fr],
|
||||
rowSizes: [1.fr],
|
||||
children: [
|
||||
Text(
|
||||
state.buffer,
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
Text(
|
||||
state.buffer,
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: Text(
|
||||
state.buffer,
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalcButton extends ConsumerWidget {}
|
@ -0,0 +1,120 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
// TODO: implement build
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton();
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
child: null,
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () {},
|
||||
child: null,
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () {},
|
||||
child: Text(),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () {},
|
||||
child: Text(),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () {},
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () {},
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () {},
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,132 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () {},
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {}
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,145 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,153 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,158 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,163 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,168 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,173 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,178 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,183 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,188 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,193 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,198 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,203 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,208 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,217 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,219 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return SizedBox(
|
||||
child: OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,219 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return SizedBox.expand(
|
||||
child: OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,222 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(label),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,225 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,226 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,227 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,228 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,232 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: OutlinedButton(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,232 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,233 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,234 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,235 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,236 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,238 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,239 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,240 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,241 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,242 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,243 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,251 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display
|
||||
seven eight nine
|
||||
four five six
|
||||
one two three
|
||||
zero point equals
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,251 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,256 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,261 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,266 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,271 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'divide',
|
||||
op: (engine) => engine.addToBuffer('/'),
|
||||
label: '/',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,272 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:math_expressions/math_expressions.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'divide',
|
||||
op: (engine) => engine.addToBuffer('/'),
|
||||
label: '/',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,274 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:math_expressions/math_expressions.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void evaluate() {}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'divide',
|
||||
op: (engine) => engine.addToBuffer('/'),
|
||||
label: '/',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,282 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:math_expressions/math_expressions.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void evaluate() {
|
||||
try {} catch (err) {
|
||||
state = state.copyWith(
|
||||
error: err.toString(),
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'divide',
|
||||
op: (engine) => engine.addToBuffer('/'),
|
||||
label: '/',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,287 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:math_expressions/math_expressions.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void evaluate() {
|
||||
try {
|
||||
final parser = Parser();
|
||||
final cm = ContextModel();
|
||||
final exp = parser.parse(state.buffer);
|
||||
final result = exp.evaluate(EvaluationType.REAL, cm) as double;
|
||||
} catch (err) {
|
||||
state = state.copyWith(
|
||||
error: err.toString(),
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'divide',
|
||||
op: (engine) => engine.addToBuffer('/'),
|
||||
label: '/',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,300 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:math_expressions/math_expressions.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void evaluate() {
|
||||
try {
|
||||
final parser = Parser();
|
||||
final cm = ContextModel();
|
||||
final exp = parser.parse(state.buffer);
|
||||
final result = exp.evaluate(EvaluationType.REAL, cm) as double;
|
||||
|
||||
if (result.isInfinite) {
|
||||
state = state.copyWith(
|
||||
error: 'Result is Infinite',
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
} else if (result.isNaN) {
|
||||
state = state.copyWith(
|
||||
error: 'Result is Not a Number',
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
} catch (err) {
|
||||
state = state.copyWith(
|
||||
error: err.toString(),
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'divide',
|
||||
op: (engine) => engine.addToBuffer('/'),
|
||||
label: '/',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,309 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:math_expressions/math_expressions.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void evaluate() {
|
||||
try {
|
||||
final parser = Parser();
|
||||
final cm = ContextModel();
|
||||
final exp = parser.parse(state.buffer);
|
||||
final result = exp.evaluate(EvaluationType.REAL, cm) as double;
|
||||
|
||||
if (result.isInfinite) {
|
||||
state = state.copyWith(
|
||||
error: 'Result is Infinite',
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
} else if (result.isNaN) {
|
||||
state = state.copyWith(
|
||||
error: 'Result is Not a Number',
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
} else {
|
||||
final resultStr = result.ceil() == result
|
||||
? result.toInt().toString()
|
||||
: result.toString();
|
||||
state = state.copyWith(
|
||||
buffer: resultStr,
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
state = state.copyWith(
|
||||
error: err.toString(),
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) {},
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'divide',
|
||||
op: (engine) => engine.addToBuffer('/'),
|
||||
label: '/',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,309 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:math_expressions/math_expressions.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void evaluate() {
|
||||
try {
|
||||
final parser = Parser();
|
||||
final cm = ContextModel();
|
||||
final exp = parser.parse(state.buffer);
|
||||
final result = exp.evaluate(EvaluationType.REAL, cm) as double;
|
||||
|
||||
if (result.isInfinite) {
|
||||
state = state.copyWith(
|
||||
error: 'Result is Infinite',
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
} else if (result.isNaN) {
|
||||
state = state.copyWith(
|
||||
error: 'Result is Not a Number',
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
} else {
|
||||
final resultStr = result.ceil() == result
|
||||
? result.toInt().toString()
|
||||
: result.toString();
|
||||
state = state.copyWith(
|
||||
buffer: resultStr,
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
state = state.copyWith(
|
||||
error: err.toString(),
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) => engine.evaluate(),
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'divide',
|
||||
op: (engine) => engine.addToBuffer('/'),
|
||||
label: '/',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,319 @@
|
||||
// Copyright 2022 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:math_expressions/math_expressions.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
|
||||
void main() {
|
||||
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setWindowTitle('Simplistic Calculator');
|
||||
}
|
||||
|
||||
runApp(
|
||||
const ProviderScope(
|
||||
child: CalculatorApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class CalculatorState {
|
||||
const CalculatorState({
|
||||
required this.buffer,
|
||||
required this.mode,
|
||||
required this.error,
|
||||
});
|
||||
|
||||
final String buffer;
|
||||
final CalculatorEngineMode mode;
|
||||
final String error;
|
||||
|
||||
CalculatorState copyWith({
|
||||
String? buffer,
|
||||
CalculatorEngineMode? mode,
|
||||
String? error,
|
||||
}) =>
|
||||
CalculatorState(
|
||||
buffer: buffer ?? this.buffer,
|
||||
mode: mode ?? this.mode,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
|
||||
enum CalculatorEngineMode { input, result }
|
||||
|
||||
class CalculatorEngine extends StateNotifier<CalculatorState> {
|
||||
CalculatorEngine()
|
||||
: super(
|
||||
const CalculatorState(
|
||||
buffer: '0',
|
||||
mode: CalculatorEngineMode.result,
|
||||
error: '',
|
||||
),
|
||||
);
|
||||
|
||||
void addToBuffer(String str) {
|
||||
if (state.mode == CalculatorEngineMode.result) {
|
||||
state = state.copyWith(
|
||||
buffer: str,
|
||||
mode: CalculatorEngineMode.input,
|
||||
error: '',
|
||||
);
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
buffer: state.buffer + str,
|
||||
error: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void evaluate() {
|
||||
try {
|
||||
final parser = Parser();
|
||||
final cm = ContextModel();
|
||||
final exp = parser.parse(state.buffer);
|
||||
final result = exp.evaluate(EvaluationType.REAL, cm) as double;
|
||||
|
||||
if (result.isInfinite) {
|
||||
state = state.copyWith(
|
||||
error: 'Result is Infinite',
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
} else if (result.isNaN) {
|
||||
state = state.copyWith(
|
||||
error: 'Result is Not a Number',
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
} else {
|
||||
final resultStr = result.ceil() == result
|
||||
? result.toInt().toString()
|
||||
: result.toString();
|
||||
state = state.copyWith(
|
||||
buffer: resultStr,
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
state = state.copyWith(
|
||||
error: err.toString(),
|
||||
buffer: '',
|
||||
mode: CalculatorEngineMode.result,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final calculatorStateProvider =
|
||||
StateNotifierProvider<CalculatorEngine, CalculatorState>(
|
||||
(_) => CalculatorEngine());
|
||||
|
||||
class ButtonDefinition {
|
||||
const ButtonDefinition({
|
||||
required this.areaName,
|
||||
required this.label,
|
||||
required this.op,
|
||||
this.type = CalcButtonType.outlined,
|
||||
});
|
||||
|
||||
final String areaName;
|
||||
final String label;
|
||||
final CalculatorEngineCallback op;
|
||||
final CalcButtonType type;
|
||||
}
|
||||
|
||||
final buttonDefinitions = <ButtonDefinition>[
|
||||
ButtonDefinition(
|
||||
areaName: 'seven',
|
||||
op: (engine) => engine.addToBuffer('7'),
|
||||
label: '7',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'eight',
|
||||
op: (engine) => engine.addToBuffer('8'),
|
||||
label: '8',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'nine',
|
||||
op: (engine) => engine.addToBuffer('9'),
|
||||
label: '9',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'four',
|
||||
op: (engine) => engine.addToBuffer('4'),
|
||||
label: '4',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'five',
|
||||
op: (engine) => engine.addToBuffer('5'),
|
||||
label: '5',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'six',
|
||||
op: (engine) => engine.addToBuffer('6'),
|
||||
label: '6',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'one',
|
||||
op: (engine) => engine.addToBuffer('1'),
|
||||
label: '1',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'two',
|
||||
op: (engine) => engine.addToBuffer('2'),
|
||||
label: '2',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'three',
|
||||
op: (engine) => engine.addToBuffer('3'),
|
||||
label: '3',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'zero',
|
||||
op: (engine) => engine.addToBuffer('0'),
|
||||
label: '0',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'point',
|
||||
op: (engine) => engine.addToBuffer('.'),
|
||||
label: '.',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'equals',
|
||||
op: (engine) => engine.evaluate(),
|
||||
label: '=',
|
||||
type: CalcButtonType.elevated,
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'plus',
|
||||
op: (engine) => engine.addToBuffer('+'),
|
||||
label: '+',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'minus',
|
||||
op: (engine) => engine.addToBuffer('-'),
|
||||
label: '-',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'multiply',
|
||||
op: (engine) => engine.addToBuffer('*'),
|
||||
label: '*',
|
||||
),
|
||||
ButtonDefinition(
|
||||
areaName: 'divide',
|
||||
op: (engine) => engine.addToBuffer('/'),
|
||||
label: '/',
|
||||
),
|
||||
];
|
||||
|
||||
class CalculatorApp extends ConsumerWidget {
|
||||
const CalculatorApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(calculatorStateProvider);
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Scaffold(
|
||||
body: Container(
|
||||
color: Colors.white,
|
||||
child: LayoutGrid(
|
||||
areas: '''
|
||||
display display display display
|
||||
seven eight nine divide
|
||||
four five six multiply
|
||||
one two three minus
|
||||
zero point equals plus
|
||||
''',
|
||||
columnSizes: [1.fr, 1.fr, 1.fr, 1.fr],
|
||||
rowSizes: [
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
1.fr,
|
||||
],
|
||||
children: [
|
||||
NamedAreaGridPlacement(
|
||||
areaName: 'display',
|
||||
child: SizedBox.expand(
|
||||
child: state.error.isEmpty
|
||||
? Text(
|
||||
state.buffer,
|
||||
textAlign: TextAlign.end,
|
||||
style: const TextStyle(fontSize: 80),
|
||||
)
|
||||
: Text(
|
||||
state.error,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
fontSize: 40,
|
||||
color: Colors.red,
|
||||
),
|
||||
maxLines: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
...buttonDefinitions.map(
|
||||
(definition) => NamedAreaGridPlacement(
|
||||
areaName: definition.areaName,
|
||||
child: CalcButton(
|
||||
label: definition.label,
|
||||
op: definition.op,
|
||||
type: definition.type,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef CalculatorEngineCallback = void Function(CalculatorEngine engine);
|
||||
enum CalcButtonType { outlined, elevated }
|
||||
|
||||
class CalcButton extends ConsumerWidget {
|
||||
const CalcButton({
|
||||
Key? key,
|
||||
required this.op,
|
||||
required this.label,
|
||||
required this.type,
|
||||
}) : super(key: key);
|
||||
|
||||
final CalculatorEngineCallback op;
|
||||
final String label;
|
||||
final CalcButtonType type;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final buttonConstructor = type == CalcButtonType.elevated
|
||||
? ElevatedButton.new
|
||||
: OutlinedButton.new;
|
||||
|
||||
return SizedBox.expand(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: buttonConstructor(
|
||||
onPressed: () => op(ref.read(calculatorStateProvider.notifier)),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 40, color: Colors.black54),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,378 @@
|
||||
[
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_01.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_02.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_03.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_04.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_05.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_06.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_07.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_08.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_09.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_10.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_11.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_12.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_13.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_14.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_15.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_16.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_17.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_18.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_19.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_20.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_21.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_22.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_23.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_24.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_25.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_26.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_27.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_28.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_29.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_30.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_31.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_32.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_33.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_34.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_35.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_36.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_37.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_38.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_39.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_40.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_41.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_42.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_43.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_44.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_45.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_46.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_47.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_48.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_49.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_50.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_51.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_52.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_53.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_54.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_55.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_56.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_57.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_58.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_59.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_60.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_61.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_62.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_63.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_64.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_65.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_66.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_67.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_68.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_69.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_70.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_71.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_72.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_73.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_74.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_75.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_76.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_77.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_78.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_79.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_80.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_81.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_82.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_83.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_84.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_85.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_86.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_87.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_88.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_89.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_90.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_91.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_92.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_93.dart"
|
||||
},
|
||||
{
|
||||
"file":"lib/main.dart",
|
||||
"content":"typer/main_94.dart"
|
||||
}
|
||||
]
|
Loading…
Reference in new issue