SHARE
TWEET

Untitled

a guest Feb 20th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3.  
  4. void main() => runApp(MyApp());
  5.  
  6. class Item {
  7.   Item({this.key, this.value});
  8.  
  9.   final int key;
  10.   final int value;
  11. }
  12.  
  13. class Cone extends InheritedWidget {
  14.   Cone({Key key, Widget child, this.items}) : super(key: key, child: child);
  15.  
  16.   final List<Item> items;
  17.   final StreamController<List<Item>> streamController = StreamController();
  18.   Stream<List<Item>> get itemStream => streamController.stream;
  19.  
  20.  
  21.   void addItem({int key, int value}) {
  22.     print('old items length: ${items.length}');
  23.     items.add(Item(key: key, value: value));
  24.     print('new items length: ${items.length}');
  25.     streamController.sink.add(items);
  26.   }
  27.  
  28.   static Cone of(BuildContext context) {
  29.     return context.dependOnInheritedWidgetOfExactType<Cone>();
  30.   }
  31.  
  32.   void dispose() {
  33.     streamController.close();
  34.   }
  35.  
  36.   @override
  37.   bool updateShouldNotify(a) => true;
  38. }
  39.  
  40. class MyApp extends StatelessWidget {
  41.   @override
  42.   Widget build(BuildContext context) {
  43.     return Cone(
  44.       items: [
  45.         Item(key: 0, value: 0),
  46.         Item(key: 1, value: 3),
  47.       ],
  48.       child: MaterialApp(
  49.         debugShowCheckedModeBanner: false,
  50.         home: MyHomePage(),
  51.       ),
  52.     );
  53.   }
  54. }
  55.  
  56. class MyHomePage extends StatelessWidget {
  57.   @override
  58.   Widget build(BuildContext context) {
  59.     return Scaffold(
  60.       appBar: AppBar(
  61.         title: Text('focus test'),
  62.         actions: [
  63.           IconButton(
  64.             icon: Icon(Icons.add),
  65.             onPressed: () => Cone.of(context).addItem(key: 2, value: 5),
  66.           ),
  67.         ],
  68.       ),
  69.       body: Padding(
  70.         padding: EdgeInsets.all(8.0),
  71.         child: StreamBuilder<List<Item>>(
  72.           stream: Cone.of(context).itemStream,
  73.           builder: (context, snap) {
  74.             print('builder: ${snap.hasData}');
  75.             if (!snap.hasData) return Container();
  76.  
  77.             return ListView(
  78.                 children: snap.data
  79.                     .map(
  80.                       (item) => TextField(
  81.                         textInputAction: TextInputAction.next,
  82.                         onSubmitted: (_) => FocusScope.of(context).nextFocus(),
  83.                       ),
  84.                     )
  85.                     .toList());
  86.           },
  87.         ),
  88.       ),
  89.     );
  90.   }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top