Skip to content

Instantly share code, notes, and snippets.

@macoshita
Created February 21, 2020 06:53
Show Gist options
  • Save macoshita/b35443f3234f8ae4bb168d01be00c30b to your computer and use it in GitHub Desktop.
Save macoshita/b35443f3234f8ae4bb168d01be00c30b to your computer and use it in GitHub Desktop.
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
final _counter = Lsn(MyState(0));
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
ValueListenableBuilder<MyState>(
valueListenable: _counter,
builder: (_context, state, _child) => Text(
'${state.count}',
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _counter.increment,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class MyState {
final int count;
MyState(this.count);
// 以下は Equatable でもっと省略できる
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is MyState &&
runtimeType == other.runtimeType &&
count == other.count;
@override
int get hashCode => count.hashCode;
}
class Lsn extends ValueNotifier<MyState> {
Lsn(value) : super(value);
void increment() {
value = MyState(value.count + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment