This should be simple, but struggling. I am adding tabs to screen and tab to two different views. BUT, I need to pass user’s role into screen called, but for some reason it is not allowing widget. reference to work? It shows up underlined.
Not working: myUserRole: widget.myUserRole,
Working: myUserRole: “Member”,
widget is underlined and error: The instance member ‘widget’ can’t be accessed in an initializer. Try replacing the reference to the instance member with a different expression
Any suggested ways to do this?
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'v1_national_list_home.dart';
import 'v1_nomad_list_home.dart';
// ignore: must_be_immutable
class NationalTabbedListV1 extends StatefulWidget {
NationalTabbedListV1(this.myUserRole, {super.key});
String myUserRole = '';
@override
State<NationalTabbedListV1> createState() => _NationalTabbedListV1State();
}
class _NationalTabbedListV1State extends State<NationalTabbedListV1>
with SingleTickerProviderStateMixin {
var screens = [
NationalListHomeV1(
chapterSelected: 'Virginia (National)',
myChapter: 'Virginia (National)',
myUserRole: widget.myUserRole,
),
NomadListHomeV1(
chapterSelected: 'Virginia (National)',
myChapter: 'Virginia (National)',
myUserRole: "Member",
),
];
late TabController _tabController;
// ignore: unused_field
int _selectedIndex = 0;
@override
initState() {
super.initState();
//Create Tab Controller
_tabController = TabController(length: 3, vsync: this);
_tabController.addListener(() {
setState(() {
_selectedIndex = _tabController.index;
});
});
}
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
backgroundColor: Colors.white54,
child: CupertinoTabScaffold(
resizeToAvoidBottomInset: true,
tabBar: CupertinoTabBar(
backgroundColor: const Color.fromARGB(255, 43, 55, 138),
activeColor: Colors.white,
inactiveColor: Colors.white54,
items: const [
BottomNavigationBarItem(
label: "Chapter",
icon: Icon(Icons.domain_outlined),
),
BottomNavigationBarItem(
label: "NOMAD",
icon: Icon(Icons.emoji_transportation_outlined),
),
],
),
tabBuilder: (BuildContext context, int index) {
return screens[index];
},
),
);
}
}
Tried many ways but only hard coding Text seems to work.