Skip to content

Instantly share code, notes, and snippets.

View zoomable.dart
import 'package:flutter/widgets.dart';
class Zoomable extends StatefulWidget {
final Widget child;
const Zoomable({Key key, this.child}) : super(key: key);
@override
_ZoomableState createState() => _ZoomableState();
}
View rebuilding_optmized.dart
// Output
// flutter: Rebuilding name
// flutter: Rebuilding age
// flutter: Rebuilding age
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class MyPage extends StatefulWidget {
@override
View age_widget.dart
class AgeWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Selector<MyNotifier, int>(
selector: (_, notifier) {
final state = notifier.state;
return state is SuccessState ? state.age : 0;
},
builder: (_, age, __) {
return Text(age.toString());
View rebuilding.dart
// Output
// flutter: Rebuilding name
// flutter: Rebuilding age
// flutter: Rebuilding name
// flutter: Rebuilding age
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class MyPage extends StatefulWidget {
View home_page.dart
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: _incrementCounter,
child: Text('Increment Counter'),
),
),
@BaranMichal25
BaranMichal25 / app.dart
Created May 2, 2020
Hive Preferences App
View app.dart
class App extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
@override
void dispose() {
Hive.close();
super.dispose();
@BaranMichal25
BaranMichal25 / main.dart
Created May 2, 2020
Hive Preferences main.dart
View main.dart
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hive_preferences/app.dart';
void main() async {
await Hive.initFlutter();
runApp(App());
}
@BaranMichal25
BaranMichal25 / pubspec.yaml
Created May 2, 2020
Hive Preferences dependencies
View pubspec.yaml
dependencies:
hive: 1.4.1+1
hive_flutter: 0.3.0+2
flutter:
sdk: flutter
View preferences.dart
import 'package:hive/hive.dart';
class Preferences {
static const _preferencesBox = '_preferencesBox';
static const _counterKey = '_counterKey';
final Box<dynamic> _box;
Preferences._(this._box);
// This doesn't have to be a singleton.
View implicit_casts_example.dart
void getReadyForRuntimeError() {
final list = filterEven([1, 2, 3, 4, 5, 6]);
print(list);
}
List<int> filterEven(List<int> numbers) {
return numbers.where((item) => item % 2 == 0);
}