Share
Embed
TabBarの色を変えたかっただけなのに
Flutter Meetup Tokyo #15
FlutterのTabBarの背景色を変たい、と思ったらMaterialやInkの話、そしてTabBarのIndicatorの話まで広がりました。
スライドの最後に紹介しているリンクをこちらにも貼っておきます。
https://qiita.com/mkosuke/items/3e33d71ef4de74364f87
https://qiita.com/mkosuke/items/e506256515179d0f421b
kosuke matsumura
March 18, 2021
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
34 slides
-
About Me • দଜߤ༟(Kosuke Matsumura) • NAVITIME JAPAN Co., Ltd.
ɹAndroid / iOS • JϦʔά(࡚ϑϩϯλʔϨ)αϙʔλʔ • Twitter m.kosuke @kosuke_mtm ɾϓϩμΫτ͕ଟ༷͘ʑͳαʔϏεΛܦݧͰ͖Δʂ ɾ৽نΞϓϦ্ཱͪ͛ϦχϡʔΞϧʹࢀըͰ͖Δνϟϯε͕ଟ͍ -
جຊతͳTabBar @override Widget build(BuildContext context) { return DefaultTabController( length: 2,
child: Scaffold( appBar: AppBar( title: Text('AppBar'), bottom: TabBar( tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), body: TabBarView( children: [ Page1(), Page2(), ], ), ), ); } "QQ#BS 5BC#BS 5BC#BS7JFX -
جຊతͳTabBar @override Widget build(BuildContext context) { return DefaultTabController( length: 2,
child: Scaffold( appBar: AppBar( title: Text('AppBar'), bottom: TabBar( tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), body: TabBarView( children: [ Page1(), Page2(), ], ), ), ); } "QQ#BS 5BC#BS 5BC#BS7JFX -
جຊతͳTabBar @override Widget build(BuildContext context) { return DefaultTabController( length: 2,
child: Scaffold( appBar: AppBar( title: Text('AppBar'), bottom: TabBar( tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), body: TabBarView( children: [ Page1(), Page2(), ], ), ), ); } "QQ#BS 5BC#BS 5BC#BS7JFX -
جຊతͳTabBar @override Widget build(BuildContext context) { return DefaultTabController( length: 2,
child: Scaffold( appBar: AppBar( title: Text('AppBar'), bottom: TabBar( tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), body: TabBarView( children: [ Page1(), Page2(), ], ), ), ); } "QQ#BS 5BC#BS 5BC#BS7JFX -
جຊతͳTabBar @override Widget build(BuildContext context) { return DefaultTabController( length: 2,
child: Scaffold( appBar: AppBar( title: Text('AppBar'), bottom: TabBar( tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), body: TabBarView( children: [ Page1(), Page2(), ], ), ), ); } "QQ#BS 5BC#BS QSJNBSZ$PMPS JOEJDBUPS$PMPS -
جຊతͳTabBar "QQ#BS 5BC#BS QSJNBSZ$PMPS 5BC#BSʹഎܠ৭Λઃఆ͢Δ*'ͳ͍ const TabBar({ Key key, @required
this.tabs, this.controller, this.isScrollable = false, this.indicatorColor, this.indicatorWeight = 2.0, this.indicatorPadding = EdgeInsets.zero, this.indicator, this.indicatorSize, this.labelColor, this.labelStyle, this.labelPadding, this.unselectedLabelColor, this.unselectedLabelStyle, this.dragStartBehavior = DragStartBehavior.s this.mouseCursor, this.onTap, this.physics, }) 5BC#BSͷ৭Λม͍͑ͨʂ ͱ͍͏ͷ͕ࠓճͷओͰ͢ɻ -
TabBarͷഎܠ৭Λม͍͑ͨ $PMPSFE#PYͳͲΛ5BC#BSͷԼʹෑ͘ DefaultTabController( length: 2, child: Scaffold( appBar: AppBar( title:
Text('Title'), bottom: PreferredSize( preferredSize: Size.fromHeight(72), child: ColoredBox( color: Colors.redAccent, child: TabBar( tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), ), ), body: TabBarView( children: [ Page1(), Page2(), ], ), ), $PMPSFE#PYͰ5BC#BSΛғΊ0,ʂ -
TabBarͷഎܠ৭Λม͍͑ͨ $PMPSFE#PYͳͲΛ5BC#BSͷԼʹෑ͘ DefaultTabController( length: 2, child: Scaffold( appBar: AppBar( title:
Text('Title'), bottom: PreferredSize( preferredSize: Size.fromHeight(72), child: ColoredBox( color: Colors.redAccent, child: TabBar( tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), ), ), body: TabBarView( children: [ Page1(), Page2(), ], ), ), 3JQQMF& ff FDU͕ දࣔ͞Εͳ͍ -
Ripple Effectʮഎܠʯ 3JQQMF& ff FDU*OL8FMMΟδΣοτͷද໘Ͱͳ͘ɺ എܠͱͯ͠.BUFSJBMͷ্Ͱඳը͞ΕΔ "QQ#BS .BUFSJBM 5BC#BS *OL8FMM
ɹ*OL8FMM͕λονΠϕϯτΛݕ ɹ3JQQMF& ff FDU ɹ.BUFSJBMΟδΣοτ্Ͱඳը͞ΕΔ -
Ripple Effectʮഎܠʯ ʲ.BUFSJBMͷ্ʹෆಁ໌ͳ8JEHFU͕͋Δ߹ʳ "QQ#BS .BUFSJBM 5BC#BS *OL8FMM ɹ$PMPSFE#PY͕अຐͰ3JQQMF& ff FDU͕ݟ͑ͳ͍
ɹ͜ͷ.BUFSJBMͷ্Ͱ3JQQMF& ff FDU͕ඳը͞ΕΔ λονΠϕϯτ͕*OL8FMM͔Β .BUFSJBM͑ΒΕΔ $PMPSFE#PY -
Ripple Effectޮ͔ͤͭͭɺTabBarͷഎܠ৭Λม͍͑ͨ DefaultTabController( length: 2, child: Scaffold( appBar: AppBar( title:
Text('Title'), bottom: PreferredSize( preferredSize: Size.fromHeight(72), child: ColoredBox( color: Colors.redAccent, child: TabBar( indicatorColor: Colors.amber, tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), ), ), body: TabBarView( children: [ Page1(), Page2(), -
Ripple Effectޮ͔ͤͭͭɺTabBarͷഎܠ৭Λม͍͑ͨ $PMPSFE#PYͷΘΓʹɺ *OLͰ5BC#BSΛғΉ *OLͰഎܠ৭Λࢦఆ͢Δ DefaultTabController( length:
2, child: Scaffold( appBar: AppBar( title: Text('Title'), bottom: PreferredSize( preferredSize: Size.fromHeight(72), child: ColoredBox( color: Colors.redAccent, child: TabBar( indicatorColor: Colors.amber, tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), ), ), body: TabBarView( children: [ Page1(), Page2(), child: Ink( -
Ripple Effectޮ͔ͤͭͭɺTabBarͷഎܠ৭Λม͍͑ͨ "QQ#BS .BUFSJBM 5BC#BS *OL8FMM ɹ͜ͷ.BUFSJBMͷ্Ͱ%FDPSBUJPO͕ඳը͞ΕΔ λονΠϕϯτͷ3JQQMF& ff FDU
มΘΒͣ.BUFSJBMͷ্ *OL ʣ͜ͷ߹ɺ.BUFSJBMͷ্ʹ ෆಁ໌ͳ8JEHFUΛ͓͘͜ͱग़དྷͳ͍ ʲ*OLͷׂʳ -
ʁ DefaultTabController( length: 2, child: Scaffold( appBar: AppBar( title: Text('Title'),
bottom: PreferredSize( preferredSize: Size.fromHeight(72), child: Ink( color: Colors.redAccent, child: TabBar( indicatorColor: Colors.amber, tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), ), ), body: TabBarView( children: [ Page1(), Page2(), ], JOEJDBUPS$PMPSͷԫ৭͕ ө͞Εͣɺന͘ͳ͍ͬͯΔ -
IndicatorColorͷṖ Decoration get _indicator { if (widget.indicator != null) return
widget.indicator; final TabBarTheme tabBarTheme = TabBarTheme.of(context); if (tabBarTheme.indicator != null) return tabBarTheme.indicator; Color color = widget.indicatorColor ?? Theme.of(context).indicatorColor; // ThemeData tries to avoid this by having indicatorColor avoid being the // primaryColor. However, it's possible that the tab bar is on a // Material that isn't the primaryColor. In that case, if the indicator // color ends up matching the material's color, then this overrides it. // When that happens, automatic transitions of the theme will likely look // ugly as the indicator color suddenly snaps to white at one end, but it's // not clear how to avoid that any further. // // The material's color might be null (if it's a transparency). In that case // there's no good way for us to find out what the color is so we don't. if (color.value == Material.of(context).color?.value) color = Colors.white; 5BC#BSͷதͷɺJOEJDBUPSͷॲཧ 'MVUUFSҎલ IUUQTHJUIVCDPN fl VUUFS fl VUUFSCMPCQBDLBHFT fl VUUFSMJCTSDNBUFSJBMUBCTEBSU -
IndicatorColorͷṖ Decoration get _indicator { if (widget.indicator != null) return
widget.indicator; final TabBarTheme tabBarTheme = TabBarTheme.of(context); if (tabBarTheme.indicator != null) return tabBarTheme.indicator; Color color = widget.indicatorColor ?? Theme.of(context).indicatorColor; // ThemeData tries to avoid this by having indicatorColor avoid being the // primaryColor. However, it's possible that the tab bar is on a // Material that isn't the primaryColor. In that case, if the indicator // color ends up matching the material's color, then this overrides it. // When that happens, automatic transitions of the theme will likely look // ugly as the indicator color suddenly snaps to white at one end, but it's // not clear how to avoid that any further. // // The material's color might be null (if it's a transparency). In that case // there's no good way for us to find out what the color is so we don't. if (color.value == Material.of(context).color?.value) color = Colors.white; *OEJDBUPS$PMPS͕QSJNBSZ$PMPSͱҰக͢Δ߹ɺ നͰ্ॻ͖͍ͯ͠Δ 5BC#BSͷதͷɺJOEJDBUPSͷॲཧ 'MVUUFSҎલ -
IndicatorColorͷṖ Decoration get _indicator { if (widget.indicator != null) return
widget.indicator; final TabBarTheme tabBarTheme = TabBarTheme.of(context); if (tabBarTheme.indicator != null) return tabBarTheme.indicator; Color color = widget.indicatorColor ?? Theme.of(context).indicatorColor; // ThemeData tries to avoid this by having indicatorColor avoid being the // primaryColor. However, it's possible that the tab bar is on a // Material that isn't the primaryColor. In that case, if the indicator // color ends up matching the material's color, then this overrides it. // When that happens, automatic transitions of the theme will likely look // ugly as the indicator color suddenly snaps to white at one end, but it's // not clear how to avoid that any further. // // The material's color might be null (if it's a transparency). In that case // there's no good way for us to find out what the color is so we don't. if (color.value == Material.of(context).color?.value) color = Colors.white; *OEJDBUPS$PMPS͕QSJNBSZ$PMPSͱҰக͢Δ߹ɺ നͰ্ॻ͖͍ͯ͠Δ JOEJDBUPSجຊతʹQSJNBSZ$PMPSͷ ্ʹඳը͞ΕΔ͜ͱΛఆ͍ͯ͠ΔͨΊɺ ࣗવͳڍಈ 5BC#BSͷதͷɺJOEJDBUPSͷॲཧ 'MVUUFSҎલ -
IndicatorColorͷṖ Decoration get _indicator { …(ུ) Color color = widget.indicatorColor
?? Theme.of(context).indicatorColor; // ThemeData tries to avoid this by having indicatorColor avoid being the // primaryColor. However, it's possible that the tab bar is on a // Material that isn't the primaryColor. In that case, if the indicator // color ends up matching the material's color, then this overrides it. // When that happens, automatic transitions of the theme will likely look // ugly as the indicator color suddenly snaps to white at one end, but it's // not clear how to avoid that any further. // // The material's color might be null (if it's a transparency). In that case // there's no good way for us to find out what the color is so we don't. // // TODO(xu-baolin): Remove automatic adjustment to white color indicator // with a better long-term solution. // https://github.com/flutter/flutter/pull/68171#pullrequestreview-517753917 if (widget.automaticIndicatorColorAdjustment && color.value == Material.of(context)?.color?.value) color = Colors.white; 5BC#BSͷதͷɺJOEJDBUPSͷॲཧ 'MVUUFSd IUUQTHJUIVCDPN fl VUUFS fl VUUFSCMPCQBDLBHFT fl VUUFSMJCTSDNBUFSJBMUBCTEBSU 50%0ίϝϯτͱҰॹʹϑϥά͕Ճ͞Ε͍ͯΔ -
IndicatorColorͷṖ /// Whether this tab bar should automatically adjust the
[indicatorColor]. /// /// If [automaticIndicatorColorAdjustment] is true, /// then the [indicatorColor] will be automatically adjusted to [Colors.white] /// when the [indicatorColor] is same as [Material.color] of the [Material] parent widget. final bool automaticIndicatorColorAdjustment; BVUPNBUJD*OEJDBUPS$PMPS"EKVTUNFOUͱ͍͏ϑϥά͕Ճ USVFͷ߹طଘͷΑ͏ʹɺJOEJDBUPS$PMPSQSJNBSZ$PMPSͷ߹ʹ JOEJDBUPS$PMPSΛࣗಈͰനʹ͢ΔڍಈʹͳΔɻ ͜ΕΛ͍͗ͨ߹ɺϑϥάΛGBMTFʹ͢Δ͜ͱͰରԠՄೳ -
IndicatorColorΛөͤ͞ΔͨΊʹ *OLͰͳ͘ɺ.BUFSJBMΟδΣοτΛ͏ DefaultTabController( length: 2, child: Scaffold( appBar: AppBar(
title: Text('Title'), bottom: PreferredSize( preferredSize: Size.fromHeight(72), child: Ink( child: Material( color: Colors.redAccent, child: TabBar( indicatorColor: Colors.amber, tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ), ), ), ), body: TabBarView( 5BC#BS͕ࢀর͢Δ.BUFSJBMͷ DPMPS͕มΘͬͨͨΊɺ JOEJDBUPS$PMPS͕ന্͘ॻ͖͞ Εͳ͍ -
IndicatorColorΛөͤ͞ΔͨΊʹ *OEJDBUPSΛ%FDPSBUJPOͱͯ͠ઃఆ͢Δ DefaultTabController( length: 2, child: Scaffold( appBar: AppBar(
title: Text('Title'), bottom: PreferredSize( preferredSize: Size.fromHeight(72), child: Ink( color: Colors.redAccent, child: TabBar( indicatorColor: Colors.amber, indicator: UnderlineTabIndicator( borderSide: BorderSide( width: 2, color: Colors.amber, ), ), tabs: [ Tab(child: Text('Tab A')), Tab(child: Text('Tab B')), ], ࣗલͰ%FDPSBUJPOΛ࡞Εɺ ࣗಈͰന্͘ॻ͖͞ΕΔ͜ͱ ͳ͍ -
Summary • Ripple EffectMaterialΟδΣοτͷ্Ͱඳը͞ΕΔͨΊɺ MaterialΟδΣοτͷ্ʹෆಁ໌ͳΟδΣοτ͕ଘࡏ͢Δͱ Ripple Effect͕ݟ͑ͳ͘ͳͬͯ͠·͏ •
ରԠํ๏ͱͯ͠ɺෆಁ໌ͳΟδΣοτΛInkͰସ͢Δɺ ·ͨMaterialΟδΣοτΛஔ͢Δ • TabBarͷIndicatorɺMaterialΟδΣοτͷcolorͱಉ͡৭ʹઃఆ͢Δͱ ࣗಈతʹന͘ͳͬͯ͠·͏ͷͰҙ