Skip to content

Instantly share code, notes, and snippets.

@iapicca
Last active August 27, 2022 18:59
Show Gist options
  • Select an option

  • Save iapicca/b385102d8de1c636e4c2b12e0a54acda to your computer and use it in GitHub Desktop.

Select an option

Save iapicca/b385102d8de1c636e4c2b12e0a54acda to your computer and use it in GitHub Desktop.
issue_109983 (c)
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: MyHomePage()));
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late final ValueNotifier<List<String>> _notifier;
late final ScrollController _controller;
@override
void initState() {
_notifier = ValueNotifier([])..addListener(_onAdded);
_controller = ScrollController();
super.initState();
}
@override
void dispose() {
_notifier
..removeListener(_onAdded)
..dispose();
_controller.dispose();
super.dispose();
}
void _onAdded() =>
WidgetsBinding.instance.addPostFrameCallback((_) => _controller.animateTo(
_controller.position.maxScrollExtent,
duration: kThemeAnimationDuration,
curve: Curves.easeOut,
));
void _add() =>
_notifier.value = [..._notifier.value, '${_notifier.value.length}'];
@override
Widget build(context) => Scaffold(
appBar: AppBar(title: const Text('AppBar')),
body: LayoutBuilder(
builder: (context, contraints) => ListView(
controller: _controller,
children: [
const Text('header'),
SizedBox(
height: contraints.maxHeight * .9,
child: const ColoredBox(
color: Colors.green,
child: Text('body'),
),
),
const Text('footer'),
MyListView(notifier: _notifier),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _add,
child: const Icon(Icons.add),
),
);
}
class MyListView extends StatelessWidget {
final ValueNotifier<List<String>> notifier;
const MyListView({
required this.notifier,
super.key,
});
@override
Widget build(context) => ValueListenableBuilder<List<String>>(
valueListenable: notifier,
builder: (context, strings, child) => Column(
children: [for (final string in strings) Text(string)],
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment