Skip to content

Commit

Permalink
Give YaruTabbedPage index on rebuild
Browse files Browse the repository at this point in the history
Fixes #159
  • Loading branch information
Feichtmeier committed Jun 23, 2022
1 parent 31d1f7b commit cdde2e5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
70 changes: 42 additions & 28 deletions example/lib/pages/tabbed_page_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,50 @@ import 'package:yaru_icons/yaru_icons.dart';
import 'package:yaru_widgets/yaru_widgets.dart';
import 'package:yaru_widgets_example/widgets/row_list.dart';

class TabbedPagePage extends StatelessWidget {
const TabbedPagePage({Key? key}) : super(key: key);
class TabbedPagePage extends StatefulWidget {
const TabbedPagePage({
Key? key,
}) : super(key: key);

@override
State<TabbedPagePage> createState() => _TabbedPagePageState();
}

class _TabbedPagePageState extends State<TabbedPagePage> {
int _initialIndex = 0;

@override
Widget build(BuildContext context) {
return YaruTabbedPage(views: [
YaruPage(
children: [RowList()],
),
YaruPage(children: [
YaruSection(
headline: 'Accessibility',
children: [Text('accessibility')],
)
]),
YaruPage(children: [Text('Audio')]),
YaruPage(children: [Text('AddressBook')]),
YaruPage(children: [Text('Television')])
], tabIcons: [
YaruIcons.addon,
YaruIcons.accessibility,
YaruIcons.audio,
YaruIcons.address_book,
YaruIcons.television
], tabTitles: [
'Addons',
'Accessibility',
'Audio',
'Address Book',
'Television'
]);
return YaruTabbedPage(
onTap: (index) => _initialIndex = index,
initialIndex: _initialIndex,
views: [
YaruPage(
children: [RowList()],
),
YaruPage(children: [
YaruSection(
headline: 'Accessibility',
children: [Text('accessibility')],
)
]),
YaruPage(children: [Text('Audio')]),
YaruPage(children: [Text('AddressBook')]),
YaruPage(children: [Text('Television')])
],
tabIcons: [
YaruIcons.addon,
YaruIcons.accessibility,
YaruIcons.audio,
YaruIcons.address_book,
YaruIcons.television
],
tabTitles: [
'Addons',
'Accessibility',
'Audio',
'Address Book',
'Television'
]);
}
}
15 changes: 14 additions & 1 deletion lib/src/yaru_tabbed_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class YaruTabbedPage extends StatefulWidget {
right: kDefaultPagePadding,
left: kDefaultPagePadding,
),
this.initialIndex = 0,
this.onTap,
}) : super(key: key);

/// A list of [IconData] used inside the tabs - must have the same length as [tabTitles] and [views].
Expand All @@ -34,6 +36,12 @@ class YaruTabbedPage extends StatefulWidget {
/// The padding [EdgeInsets] which defaults to [kDefaultPadding] at top, right and left.
final EdgeInsets padding;

/// The initialIndex of the [TabController]
final int initialIndex;

/// The [Function] used when one of the [Tab] is tapped.
final Function(int index)? onTap;

@override
State<YaruTabbedPage> createState() => _YaruTabbedPageState();
}
Expand All @@ -44,7 +52,11 @@ class _YaruTabbedPageState extends State<YaruTabbedPage>

@override
void initState() {
tabController = TabController(length: widget.views.length, vsync: this);
tabController = TabController(
initialIndex: widget.initialIndex,
length: widget.views.length,
vsync: this,
);
super.initState();
}

Expand Down Expand Up @@ -91,6 +103,7 @@ class _YaruTabbedPageState extends State<YaruTabbedPage>
color:
Theme.of(context).colorScheme.onSurface.withOpacity(0.1),
),
onTap: widget.onTap,
tabs: [
for (var i = 0; i < widget.views.length; i++)
Tab(
Expand Down

0 comments on commit cdde2e5

Please sign in to comment.