Created
January 3, 2021 13:58
-
-
Save shinyaoguri/7a9b4b7de1cc46904e5bc9780da01c26 to your computer and use it in GitHub Desktop.
This file contains hidden or 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_bloc/flutter_bloc.dart'; | |
class CounterCubit extends Cubit<int> { | |
CounterCubit() : super(0); | |
void increment() => { emit(state + 1) }; | |
void decrement() => { emit(state - 1) }; | |
void reset() => { emit(0) }; | |
} |
This file contains hidden or 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:kitchensink/counter_cubit.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
// 加筆修正 | |
home: BlocProvider<CounterCubit>( | |
create: (_) => CounterCubit(), | |
child: MyHomePage(title: 'Flutter Demo Home Page'), | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
// カウントダウン | |
void _decrementCounter() { | |
setState(() { | |
_counter--; | |
}); | |
} | |
// リセット | |
void _resetCounter() { | |
setState(() { | |
_counter = 0; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: BlocBuilder<CounterCubit, int>(builder: (_, count) { | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'$count', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
], | |
), | |
); | |
}), | |
floatingActionButton: Column( | |
mainAxisAlignment: MainAxisAlignment.end, | |
crossAxisAlignment: CrossAxisAlignment.end, | |
children: [ | |
FloatingActionButton( | |
// 修正 | |
onPressed: () => context.read<CounterCubit>().increment(), | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
SizedBox(height: 5.0), | |
FloatingActionButton( | |
onPressed: () => context.read<CounterCubit>().decrement(), | |
tooltip: 'Decrement', | |
child: Icon(Icons.remove), | |
), | |
SizedBox(height: 5.0), | |
FloatingActionButton( | |
onPressed: () => context.read<CounterCubit>().reset(), | |
tooltip: 'Reset', | |
child: Icon(Icons.refresh), | |
) | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment