Skip to content

Instantly share code, notes, and snippets.

@derekedelaney
Created June 25, 2021 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekedelaney/f52f76eda42e8cae39aac949d9c0ba63 to your computer and use it in GitHub Desktop.
Save derekedelaney/f52f76eda42e8cae39aac949d9c0ba63 to your computer and use it in GitHub Desktop.
Showing a selected list bug when using IndexedStack
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _currentIndex = 0;
Widget build(BuildContext context) {
final _pages = ['Favorites', 'Music', 'Places', 'News'];
return Scaffold(
body: IndexedStack(
index: _currentIndex,
sizing: StackFit.expand,
children: List.generate(_pages.length, (index) {
if (index == 0) {
return Favorites();
}
return Center(
child: Text(_pages[index]),
);
}),
),
appBar: AppBar(title: Text(_pages[_currentIndex])),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _currentIndex,
onTap: (value) {
setState(() => _currentIndex = value);
},
items: [
BottomNavigationBarItem(
label: 'Favorites',
icon: Icon(Icons.favorite),
),
BottomNavigationBarItem(
label: 'Music',
icon: Icon(Icons.music_note),
),
BottomNavigationBarItem(
label: 'Places',
icon: Icon(Icons.location_on),
),
BottomNavigationBarItem(
label: 'News',
icon: Icon(Icons.library_books),
),
],
),
);
}
}
class Favorites extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListView(children: [
ListTile(
title: Text('Favorite 1', style: TextStyle(color: Colors.white)),
selected: true,
selectedTileColor: Theme.of(context).primaryColor,
),
ListTile(
title: Text('Favorite 2'),
),
ListTile(
title: Text('Favorite 3'),
),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment