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 61f1081
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 7 additions & 2 deletions example/lib/pages/tabbed_page_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ 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);
const TabbedPagePage({
Key? key,
this.initialIndex = 0,
}) : super(key: key);

final int initialIndex;

@override
Widget build(BuildContext context) {
return YaruTabbedPage(views: [
return YaruTabbedPage(initialIndex: initialIndex, views: [
YaruPage(
children: [RowList()],
),
Expand Down
13 changes: 12 additions & 1 deletion lib/src/yaru_tabbed_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class YaruTabbedPage extends StatefulWidget {
right: kDefaultPagePadding,
left: kDefaultPagePadding,
),
this.initialIndex = 0,
}) : super(key: key);

/// A list of [IconData] used inside the tabs - must have the same length as [tabTitles] and [views].
Expand All @@ -34,6 +35,9 @@ 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;

@override
State<YaruTabbedPage> createState() => _YaruTabbedPageState();
}
Expand All @@ -42,9 +46,15 @@ class _YaruTabbedPageState extends State<YaruTabbedPage>
with TickerProviderStateMixin {
late TabController tabController;

int _index = 0;

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

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

0 comments on commit 61f1081

Please sign in to comment.