Skip to content

Commit

Permalink
Make example project null-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
jb3rndt committed Sep 4, 2023
1 parent ad14ba8 commit 113f8c3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 77 deletions.
55 changes: 28 additions & 27 deletions example/lib/interactive_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class InteractiveExample extends StatefulWidget {
}

class _InteractiveExampleState extends State<InteractiveExample> {
PersistentTabController _controller;
PersistentTabController _controller =
PersistentTabController(initialIndex: 0);
bool _hideNavBar = false;
NavBarStyle _navBarStyle = NavBarStyle.style15;
bool _hideNavigationBarWhenKeyboardShows = true;
Expand All @@ -20,12 +21,6 @@ class _InteractiveExampleState extends State<InteractiveExample> {
bool _popAllScreensOnTapOfSelectedTab = true;
bool _confineInSafeArea = true;

@override
void initState() {
super.initState();
_controller = PersistentTabController(initialIndex: 0);
}

List<Widget> _buildScreens() {
return [
MainScreen(
Expand Down Expand Up @@ -107,8 +102,10 @@ class _InteractiveExampleState extends State<InteractiveExample> {
},
),
onPressed: (context) {
pushDynamicScreen(context,
screen: SampleModalScreen(), withNavBar: true);
if (context != null) {
pushDynamicScreen(context,
screen: SampleModalScreen(), withNavBar: true);
}
}),
PersistentBottomNavBarItem(
icon: Icon(Icons.message),
Expand Down Expand Up @@ -174,10 +171,12 @@ class _InteractiveExampleState extends State<InteractiveExample> {
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (NavBarStyle newStyle) {
setState(() {
_navBarStyle = newStyle;
});
onChanged: (newStyle) {
if (newStyle != null) {
setState(() {
_navBarStyle = newStyle;
});
}
},
items: NavBarStyle.values
.map<DropdownMenuItem<NavBarStyle>>(
Expand Down Expand Up @@ -294,21 +293,23 @@ class _InteractiveExampleState extends State<InteractiveExample> {
popActionScreens: PopActionScreensType.all,
bottomScreenMargin: 0.0,
onWillPop: (context) async {
await showDialog(
context: context,
useSafeArea: true,
builder: (context) => Container(
height: 50.0,
width: 50.0,
color: Colors.white,
child: ElevatedButton(
child: Text("Close"),
onPressed: () {
Navigator.pop(context);
},
if (context != null) {
await showDialog(
context: context,
useSafeArea: true,
builder: (context) => Container(
height: 50.0,
width: 50.0,
color: Colors.white,
child: ElevatedButton(
child: Text("Close"),
onPressed: () {
Navigator.pop(context);
},
),
),
),
);
);
}
return false;
},
hideNavigationBar: _hideNavBar,
Expand Down
80 changes: 36 additions & 44 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PersistenBottomNavBarDemo extends StatelessWidget {
}

class MainMenu extends StatefulWidget {
MainMenu({Key key}) : super(key: key);
MainMenu({Key? key}) : super(key: key);

@override
_MainMenuState createState() => _MainMenuState();
Expand Down Expand Up @@ -78,22 +78,16 @@ class _MainMenuState extends State<MainMenu> {
// ----------------------- Using a provided Navbar style ---------------------//

class ProvidedStyleExample extends StatefulWidget {
ProvidedStyleExample({Key key}) : super(key: key);
ProvidedStyleExample({Key? key}) : super(key: key);

@override
_ProvidedStyleExampleState createState() => _ProvidedStyleExampleState();
}

class _ProvidedStyleExampleState extends State<ProvidedStyleExample> {
PersistentTabController _controller;
bool _hideNavBar;

@override
void initState() {
super.initState();
_controller = PersistentTabController(initialIndex: 0);
_hideNavBar = false;
}
PersistentTabController _controller =
PersistentTabController(initialIndex: 0);
bool _hideNavBar = false;

List<Widget> _buildScreens() {
return [
Expand Down Expand Up @@ -176,8 +170,10 @@ class _ProvidedStyleExampleState extends State<ProvidedStyleExample> {
},
),
onPressed: (context) {
pushDynamicScreen(context,
screen: SampleModalScreen(), withNavBar: true);
if (context != null) {
pushDynamicScreen(context,
screen: SampleModalScreen(), withNavBar: true);
}
}),
PersistentBottomNavBarItem(
icon: Icon(Icons.message),
Expand Down Expand Up @@ -238,21 +234,23 @@ class _ProvidedStyleExampleState extends State<ProvidedStyleExample> {
popActionScreens: PopActionScreensType.all,
bottomScreenMargin: 0.0,
onWillPop: (context) async {
await showDialog(
context: context,
useSafeArea: true,
builder: (context) => Container(
height: 50.0,
width: 50.0,
color: Colors.white,
child: ElevatedButton(
child: Text("Close"),
onPressed: () {
Navigator.pop(context);
},
if (context != null) {
await showDialog(
context: context,
useSafeArea: true,
builder: (context) => Container(
height: 50.0,
width: 50.0,
color: Colors.white,
child: ElevatedButton(
child: Text("Close"),
onPressed: () {
Navigator.pop(context);
},
),
),
),
);
);
}
return false;
},
hideNavigationBar: _hideNavBar,
Expand All @@ -279,22 +277,16 @@ class _ProvidedStyleExampleState extends State<ProvidedStyleExample> {
// ------------------------ Using a custom Navbar style ----------------------//

class CustomWidgetExample extends StatefulWidget {
CustomWidgetExample({Key key}) : super(key: key);
CustomWidgetExample({Key? key}) : super(key: key);

@override
_CustomWidgetExampleState createState() => _CustomWidgetExampleState();
}

class _CustomWidgetExampleState extends State<CustomWidgetExample> {
PersistentTabController _controller;
bool _hideNavBar;

@override
void initState() {
super.initState();
_controller = PersistentTabController(initialIndex: 0);
_hideNavBar = false;
}
PersistentTabController _controller =
PersistentTabController(initialIndex: 0);
bool _hideNavBar = false;

List<Widget> _buildScreens() {
return [
Expand Down Expand Up @@ -409,7 +401,7 @@ class _CustomWidgetExampleState extends State<CustomWidgetExample> {
items: _navBarsItems(),
onItemSelected: (index) {
setState(() {
navBarEssentials.onItemSelected(index);
navBarEssentials.onItemSelected?.call(index);
});
},
selectedIndex: _controller.index,
Expand All @@ -422,12 +414,12 @@ class _CustomWidgetExampleState extends State<CustomWidgetExample> {
class CustomNavBarWidget extends StatelessWidget {
final int selectedIndex;
final List<PersistentBottomNavBarItem> items;
final ValueChanged<int> onItemSelected;
final ValueChanged<int>? onItemSelected;

CustomNavBarWidget({
Key key,
this.selectedIndex,
@required this.items,
Key? key,
required this.selectedIndex,
required this.items,
this.onItemSelected,
});

Expand Down Expand Up @@ -460,7 +452,7 @@ class CustomNavBarWidget extends StatelessWidget {
type: MaterialType.transparency,
child: FittedBox(
child: Text(
item.title,
item.title??"",
style: TextStyle(
color: isSelected
? (item.activeColorSecondary == null
Expand Down Expand Up @@ -492,7 +484,7 @@ class CustomNavBarWidget extends StatelessWidget {
return Expanded(
child: InkWell(
onTap: () {
this.onItemSelected(index);
this.onItemSelected?.call(index);
},
child: _buildItem(item, selectedIndex == index),
),
Expand Down
2 changes: 1 addition & 1 deletion example/lib/modal-screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SampleModalScreen extends ModalRoute<void> {
Color get barrierColor => Colors.black.withOpacity(0.5);

@override
String get barrierLabel => null;
String? get barrierLabel => null;

@override
bool get maintainState => true;
Expand Down
10 changes: 5 additions & 5 deletions example/lib/screens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import 'package:persistent_bottom_nav_bar_v2/persistent-tab-view.dart';
import 'modal-screen.dart';

class MainScreen extends StatelessWidget {
final Function onScreenHideButtonPressed;
final Function? onScreenHideButtonPressed;
final bool hideStatus;

const MainScreen({
Key key,
Key? key,
this.onScreenHideButtonPressed,
this.hideStatus = false,
}) : super(key: key);
Expand Down Expand Up @@ -116,7 +116,7 @@ class MainScreen extends StatelessWidget {
Center(
child: ElevatedButton(
onPressed: () {
this.onScreenHideButtonPressed();
this.onScreenHideButtonPressed?.call();
},
child: Text(
this.hideStatus
Expand Down Expand Up @@ -149,7 +149,7 @@ class MainScreen extends StatelessWidget {
}

class MainScreen2 extends StatelessWidget {
const MainScreen2({Key key}) : super(key: key);
const MainScreen2({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -187,7 +187,7 @@ class MainScreen2 extends StatelessWidget {
}

class MainScreen3 extends StatelessWidget {
const MainScreen3({Key key}) : super(key: key);
const MainScreen3({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand Down

0 comments on commit 113f8c3

Please sign in to comment.