forked from flutter/flutter
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes flutter#112676. Fixes flutter#97015. Fixes flutter#62107. Fixes flutter#38740. Fixes flutter#31911. Fixes flutter#29958. Fixes flutter#18108. Fixes flutter#17160. Fixes flutter#14243. Fixes flutter#3354.
- Loading branch information
Showing
23 changed files
with
644 additions
and
191 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
examples/api/lib/widgets/transitions/listenable_builder.2.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Flutter code sample for a [ChangeNotifier] with a [ListenableBuilder]. | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
void main() { runApp(const ListenableBuilderExample()); } | ||
|
||
class CounterModel with ChangeNotifier { | ||
int _count = 0; | ||
int get count => _count; | ||
|
||
void increment() { | ||
_count += 1; | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
class ListenableBuilderExample extends StatefulWidget { | ||
const ListenableBuilderExample({super.key}); | ||
|
||
@override | ||
State<ListenableBuilderExample> createState() => _ListenableBuilderExampleState(); | ||
} | ||
|
||
class _ListenableBuilderExampleState extends State<ListenableBuilderExample> { | ||
final CounterModel _counter = CounterModel(); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
appBar: AppBar(title: const Text('ListenableBuilder Example')), | ||
body: CounterBody(counterNotifier: _counter), | ||
floatingActionButton: FloatingActionButton( | ||
onPressed: _counter.increment, | ||
child: const Icon(Icons.add), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class CounterBody extends StatelessWidget { | ||
const CounterBody({super.key, required this.counterNotifier}); | ||
|
||
final CounterModel counterNotifier; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
const Text('Current counter value:'), | ||
// Thanks to the ListenableBuilder, only the widget displaying the | ||
// current count is rebuilt when counterValueNotifier notifies its | ||
// listeners. The Text widget above and CounterBody itself aren't | ||
// rebuilt. | ||
ListenableBuilder( | ||
listenable: counterNotifier, | ||
builder: (BuildContext context, Widget? child) { | ||
return Text('${counterNotifier.count}'); | ||
}, | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
examples/api/lib/widgets/transitions/listenable_builder.3.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Flutter code sample for a [ChangeNotifier] with a [ListenableBuilder]. | ||
|
||
import 'dart:math' as math; | ||
import 'package:flutter/material.dart'; | ||
|
||
void main() { runApp(const ListenableBuilderExample()); } | ||
|
||
class ListModel with ChangeNotifier { | ||
final List<int> _values = <int>[]; | ||
List<int> get values => _values.toList(); // O(N), makes a new copy each time. | ||
|
||
void add(int value) { | ||
_values.add(value); | ||
notifyListeners(); | ||
} | ||
} | ||
|
||
class ListenableBuilderExample extends StatefulWidget { | ||
const ListenableBuilderExample({super.key}); | ||
|
||
@override | ||
State<ListenableBuilderExample> createState() => _ListenableBuilderExampleState(); | ||
} | ||
|
||
class _ListenableBuilderExampleState extends State<ListenableBuilderExample> { | ||
final ListModel _listNotifier = ListModel(); | ||
final math.Random _random = math.Random(0); // fixed seed for reproducability | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
appBar: AppBar(title: const Text('ListenableBuilder Example')), | ||
body: ListBody(listNotifier: _listNotifier), | ||
floatingActionButton: FloatingActionButton( | ||
onPressed: () => _listNotifier.add(_random.nextInt(1 << 32)), // 1 << 32 is the maximum supported value | ||
child: const Icon(Icons.add), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class ListBody extends StatelessWidget { | ||
const ListBody({super.key, required this.listNotifier}); | ||
|
||
final ListModel listNotifier; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Center( | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.stretch, | ||
children: <Widget>[ | ||
const Text('Current values:'), | ||
Expanded( | ||
child: ListenableBuilder( | ||
listenable: listNotifier, | ||
builder: (BuildContext context, Widget? child) { | ||
// We rebuild the ListView each time the list changes, | ||
// so that the framework knows to update the rendering. | ||
final List<int> values = listNotifier.values; // copy the list | ||
return ListView.builder( | ||
itemBuilder: (BuildContext context, int index) => ListTile( | ||
title: Text('${values[index]}'), | ||
), | ||
itemCount: values.length, | ||
); | ||
}, | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.