Created with <3 with dartpad.dev.
Created
July 25, 2023 14:04
-
-
Save Pacane/18fcbbb49a83d65bfca6f12605d1e1ca to your computer and use it in GitHub Desktop.
flying-glimmer-1968
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'package:provider/provider.dart'; | |
class SimpleCounter extends Cubit<int> { | |
SimpleCounter(super.initialState); | |
void increment() => emit(state + 1); | |
void decrement() => emit(state - 1); | |
} | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: Provider<SimpleCounter>( | |
create: (_) => SimpleCounter(0), | |
child: const MyHomePage(), | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
const MyHomePage({super.key}); | |
@override | |
Widget build(BuildContext context) => const MyScaffold(); | |
} | |
class MyScaffold extends StatelessWidget { | |
const MyScaffold({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
final count = context.watch<SimpleCounter>().state; | |
return Scaffold( | |
appBar: AppBar(title: const Text('My app')), | |
body: Center(child: Text(count.toString())), | |
floatingActionButton: Row( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
FloatingActionButton( | |
onPressed: () => context.read<SimpleCounter>().decrement(), | |
tooltip: 'Left', | |
child: const Icon(Icons.remove), | |
), | |
FloatingActionButton( | |
onPressed: () => context.read<SimpleCounter>().increment(), | |
tooltip: 'Right', | |
child: const Icon(Icons.add), | |
), | |
], | |
), | |
); | |
} | |
} | |
class MyScaffold2 extends StatelessWidget { | |
const MyScaffold2({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return BlocBuilder<SimpleCounter, int>(builder: (context, count) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('My app')), | |
body: Center(child: Text(count.toString())), | |
floatingActionButton: Row( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
FloatingActionButton( | |
onPressed: () => context.read<SimpleCounter>().decrement(), | |
tooltip: 'Left', | |
child: const Icon(Icons.remove), | |
), | |
FloatingActionButton( | |
onPressed: () => context.read<SimpleCounter>().increment(), | |
tooltip: 'Right', | |
child: const Icon(Icons.add), | |
), | |
], | |
), | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment