Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'dart:async';
- void main() => runApp(MyApp());
- class Item {
- Item({this.key, this.value});
- final int key;
- final int value;
- }
- class Cone extends InheritedWidget {
- Cone({Key key, Widget child, this.items}) : super(key: key, child: child);
- final List<Item> items;
- final StreamController<List<Item>> streamController = StreamController();
- Stream<List<Item>> get itemStream => streamController.stream;
- void addItem({int key, int value}) {
- print('old items length: ${items.length}');
- items.add(Item(key: key, value: value));
- print('new items length: ${items.length}');
- streamController.sink.add(items);
- }
- static Cone of(BuildContext context) {
- return context.dependOnInheritedWidgetOfExactType<Cone>();
- }
- void dispose() {
- streamController.close();
- }
- @override
- bool updateShouldNotify(a) => true;
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Cone(
- items: [
- Item(key: 0, value: 0),
- Item(key: 1, value: 3),
- ],
- child: MaterialApp(
- debugShowCheckedModeBanner: false,
- home: MyHomePage(),
- ),
- );
- }
- }
- class MyHomePage extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('focus test'),
- actions: [
- IconButton(
- icon: Icon(Icons.add),
- onPressed: () => Cone.of(context).addItem(key: 2, value: 5),
- ),
- ],
- ),
- body: Padding(
- padding: EdgeInsets.all(8.0),
- child: StreamBuilder<List<Item>>(
- stream: Cone.of(context).itemStream,
- builder: (context, snap) {
- print('builder: ${snap.hasData}');
- if (!snap.hasData) return Container();
- return ListView(
- children: snap.data
- .map(
- (item) => TextField(
- textInputAction: TextInputAction.next,
- onSubmitted: (_) => FocusScope.of(context).nextFocus(),
- ),
- )
- .toList());
- },
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement