Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flutter向けのパッケージ 作ってpubで公開した話

Flutter向けのパッケージ 作ってpubで公開した話

Flutter勉強会 in 福岡 #2 https://flat-lab.connpass.com/event/101141/ でLTさせていただいた内容です

Necco Michael

October 10, 2018
Tweet

More Decks by Necco Michael

Other Decks in Programming

Transcript

  1. @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text('ૉͷStreamBuilder'),

    ), body: StreamBuilder( stream: widget.stream, builder: (BuildContext context, AsyncSnapshot<String> snapshot) { // ΋͠ΤϥʔͳΒΞΠίϯΛදࣔʢ௨৴Τϥʔ࣌ͳͲʣ if (snapshot.hasError) { return Icon(Icons.error_outline); } // σʔλ͕·ͩಧ͍͍ͯͳ͍ؒ͸ɺۭനΛදࣔʢॳճඳը࣌ͳͲʣ if (!snapshot.hasData) { return Container(); } // ʻຊ໋ʼσʔλ͕औΕͨΒը໘ʹදࣔ return Center( child: Text( snapshot.data, style: TextStyle(fontSize: 30.0), )); }, ), );
  2. @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text('ૉͷStreamBuilder'),

    ), body: StreamBuilder( stream: widget.stream, builder: (BuildContext context, AsyncSnapshot<String> snapshot) { // ΋͠ΤϥʔͳΒΞΠίϯΛදࣔʢ௨৴Τϥʔ࣌ͳͲʣ if (snapshot.hasError) { return Icon(Icons.error_outline); } // σʔλ͕·ͩಧ͍͍ͯͳ͍ؒ͸ɺۭനΛදࣔʢॳճඳը࣌ͳͲʣ if (!snapshot.hasData) { return Container(); } // ʻຊ໋ʼσʔλ͕औΕͨΒը໘ʹදࣔ return Center( child: Text( snapshot.data, style: TextStyle(fontSize: 30.0), )); }, ), ); ඳըʹඞཁͳσʔλ 4USJOH ͸ɺ TOBQTIPUEBUBͷதʹೖ͍ͬͯΔ TOBQTIPUIBT&SSPSΛΈͯɺ Τϥʔ͕ൃੜͨ͠ͱ͖͸ԿΛඳը͢Δ͔෼ذ͢Δ TOBQTIPUIBT%BUBΛΈͯɺ σʔλ͕·ۭͩͷͱ͖͸ԿΛඳը͢Δ͔෼ذ͢Δ ˡ࣮͸͕͜͜ϝΠϯͰ࣮૷͠ͳ͚Ε͹͍͚ͳ͍෦෼ ˞ࡾ߲ԋࢉࢠͰॻ͍ͯΔέʔε΋Α͘Έ·͢ʢݸਓతʹ͸ಡΈͮΒ͍ʣ
  3. @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text('ૉͷStreamBuilder'),

    ), body: StreamBuilder( stream: widget.stream, builder: (BuildContext context, AsyncSnapshot<String> snapshot) { // ΋͠ΤϥʔͳΒΞΠίϯΛදࣔʢ௨৴Τϥʔ࣌ͳͲʣ if (snapshot.hasError) { return Icon(Icons.error_outline); } // σʔλ͕·ͩಧ͍͍ͯͳ͍ؒ͸ɺۭനΛදࣔʢॳճඳը࣌ͳͲʣ if (!snapshot.hasData) { return Container(); } // ʻຊ໋ʼσʔλ͕औΕͨΒը໘ʹදࣔ return Center( child: Text( snapshot.data, style: TextStyle(fontSize: 30.0), )); }, ), ); *G TOBQTIPUIBT%BUB  *G TOBQTIPUIBT%BUB  *G TOBQTIPUIBT%BUB  *G TOBQTIPUIBT%BUB  *G TOBQTIPUIBT%BUB  *G TOBQTIPUIBT%BUB  *G TOBQTIPUIBT%BUB  *G TOBQTIPUIBT%BUB  *G TOBQTIPUIBT%BUB  Զ͸൑ఆࣜʹόάΛ࡞ΓࠐΉʂ ઈରʹͩʂ
  4. @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text('ૉͷStreamBuilder'),

    ), body: StreamBuilder( stream: widget.stream, builder: (BuildContext context, AsyncSnapshot<String> snapshot) { // ΋͠ΤϥʔͳΒΞΠίϯΛදࣔʢ௨৴Τϥʔ࣌ͳͲʣ if (snapshot.hasError) { return Icon(Icons.error_outline); } // σʔλ͕·ͩಧ͍͍ͯͳ͍ؒ͸ɺۭനΛදࣔʢॳճඳը࣌ͳͲʣ if (!snapshot.hasData) { return Container(); } // ʻຊ໋ʼσʔλ͕औΕͨΒը໘ʹදࣔ return Center( child: Text( snapshot.data, style: TextStyle(fontSize: 30.0), )); }, ), ); Զ͸൑ఆࣜʹόάΛ࡞ΓࠐΉʂ ઈରʹͩʂ
  5. @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text('ૉͷStreamBuilder'),

    ), body: StreamBuilder( stream: widget.stream, builder: (BuildContext context, AsyncSnapshot<String> snapshot) { // ΋͠ΤϥʔͳΒΞΠίϯΛදࣔʢ௨৴Τϥʔ࣌ͳͲʣ if (snapshot.hasError) { return Icon(Icons.error_outline); } // σʔλ͕·ͩಧ͍͍ͯͳ͍ؒ͸ɺۭനΛදࣔʢॳճඳը࣌ͳͲʣ if (!snapshot.hasData) { return Container(); } // ʻຊ໋ʼσʔλ͕औΕͨΒը໘ʹදࣔ return Center( child: Text( snapshot.data, style: TextStyle(fontSize: 30.0), )); }, ), ); Τϥʔ࣌ʹද͍ࣔͨ͠8JEHFU ۭന࣌ʹද͍ࣔͨ͠8JEHFU ਖ਼ৗ࣌ʹද͍ࣔͨ͠8JEHFU ͦΕͧΕࢦఆͨ͠Βྑ͘Ͷʁ
  6. @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text('SafetyStreamBuilder'),

    ), body: SafetyStreamBuilder( stream: widget.stream, blankWidget: Container(), errorWidget: Icon(Icons.error_outline), builder: (BuildContext context, String message) => Center( child: Text( message, style: TextStyle(fontSize: 30.0), ), ), ), );
  7. @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text('SafetyStreamBuilder'),

    ), body: SafetyStreamBuilder( stream: widget.stream, blankWidget: Container(), errorWidget: Icon(Icons.error_outline), builder: (BuildContext context, String message) => Center( child: Text( message, style: TextStyle(fontSize: 30.0), ), ), ), ); ˡσʔλ͕·ͩແ͍࣌ʹඳը͍ͨ͠8JEHFU ˡΤϥʔ࣌ʹඳը͍ͨ͠8JEHFU ˢσʔλऔಘ׬ྃޙʹඳը͍ͨ͠8JEHFU
  8. @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text('SafetyStreamBuilder'),

    ), body: SafetyStreamBuilder( stream: widget.stream, blankWidget: Container(), errorWidget: Icon(Icons.error_outline), builder: (BuildContext context, String message) => Center( child: Text( message, style: TextStyle(fontSize: 30.0), ), ), ), ); JGจ͕ফ͑ͨʂ ਖ਼ৗܥʹूதͰ͖Δʂ όάΛ࡞Γͨͯ͘΋࡞Εͳ͍ʂ
  9. @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text('SafetyStreamBuilder'),

    ), body: SafetyStreamBuilder( stream: widget.stream, builder: (BuildContext context, String message) => Center( child: Text( message, style: TextStyle(fontSize: 30.0), ), ), ), ); CMBOL8JEHFUͱFSSPS8JEHFU͸ লུ͢Δ͜ͱ΋Մೳ ˞ॳظ஋͸ͦΕͧΕۭനͱ ʂ ϚʔΫ
  10. library safety_stream_builder; import 'dart:async'; import 'package:flutter/material.dart'; typedef Widget AsyncWidgetSafetyBuilder<T>(BuildContext context,

    T data); typedef Widget BuildHandler(BuildContext context); class SafetyStreamBuilder<T> extends StreamBuilder<T> { static BuildHandler _buildErrorWidget = _defaultBuildErrorWidget; static BuildHandler _buildBlankWidget = _defaultBuildBlankWidget; final AsyncWidgetSafetyBuilder<T> _builder; final Widget blankWidget; final Widget errorWidget; factory SafetyStreamBuilder({ @required AsyncWidgetSafetyBuilder<T> builder, Key key, T initialData, Stream<T> stream, Widget blankWidget, Widget errorWidget, }) => SafetyStreamBuilder<T>._( builder: builder, unusedBuilder: (_, __) => null, // dummy key: key, initialData: initialData, stream: stream, blankWidget: blankWidget, errorWidget: errorWidget, ); SafetyStreamBuilder._({ @required AsyncWidgetSafetyBuilder<T> builder, @required AsyncWidgetBuilder<T> unusedBuilder, Key key, T initialData, Stream<T> stream, this.blankWidget, this.errorWidget, }) : _builder = builder, super( key: key, initialData: initialData, stream: stream, builder: unusedBuilder, ); @override Widget build(BuildContext context, AsyncSnapshot<T> snapshot) { if (snapshot.hasError) { return errorWidget != null ? errorWidget : _buildErrorWidget(context); } if (!snapshot.hasData) { return blankWidget != null ? blankWidget : _buildBlankWidget(context); } return _builder(context, snapshot.data); } static Widget _defaultBuildErrorWidget(BuildContext _) => const Icon(Icons.error_outline); static Widget _defaultBuildBlankWidget(BuildContext _) => Container(); static void handleError({@required BuildHandler build}) => _buildErrorWidget = build; static void handleBlank({@required BuildHandler build}) => _buildBlankWidget = build; } ᶃϑΝΠϧʹίʔυΛॻ͘
  11. name: safety_stream_builder description: A new flutter package project. version: 0.0.1

    author: kuronekomichael <[email protected]> homepage: https://github.com/kuronekomichael/SafetyStreamBuilder environment: sdk: ">=2.0.0-dev.68.0 <3.0.0" dependencies: flutter: sdk: flutter dev_dependencies: flutter_test: sdk: flutter flutter: ᶄQVCTQFDZBNMʹɺ BVUIPSͱIPNFQBHFΛॻ͘
  12. ## v0.0.1 * Initial Release * No API Document ᶅ$)"/(&-0(NEʹɺ

    ϦϦʔεϊʔτΛॻ͘ ʢͳͯ͘΋ΠέΔ͚Ͳڐͤͳ͍ͷͰॻ͘ʣ
  13. Pub needs your authorization to upload packages on your behalf.

    In a web browser, go to https:// accounts.google.com/o/oauth2/auth? access_type=offline&approval_prompt=force&re sponse_type=code&client_id=123456789- somethingsomethinganything.apps.googleuserco ntent.com&redirect_uri=http%3A%2F%2Flocalhos t%3A54569&scope=https%3A%2F%2Fwww.googleapis .com%2Fauth%2Fuserinfo.email Then click "Allow access". Waiting for your authorization... (PPHMFೝূ