Android 单击bottomnavigationbar';最爱';
我的英语不好,我有一个GIF来描述下面,希望大家的支持。 当我在Android 单击bottomnavigationbar';最爱';,android,flutter,mobile,navigation,Android,Flutter,Mobile,Navigation,我的英语不好,我有一个GIF来描述下面,希望大家的支持。 当我在音乐页面.dart点击尾随图标时,如何在音乐最爱页面.dart中建立价值 这是主飞镖 import'package:abc/pages/music_page.dart'; 进口“包装:颤振/材料.省道”; void main()=>runApp(MyApp()); 类MyApp扩展了无状态小部件{ @凌驾 小部件构建(构建上下文){ 返回材料PP( 标题:“材料应用程序”, 主页:主页(), ); } } 类主页扩展了Statefu
音乐页面.dart
点击尾随图标时,如何在音乐最爱页面.dart
中建立价值
这是主飞镖
import'package:abc/pages/music_page.dart';
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“材料应用程序”,
主页:主页(),
);
}
}
类主页扩展了StatefulWidget{
主页({Key}):超级(Key:Key);
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
int _currentIndex=0;
最终列表_children=[MusicPage(),MusicFavoritePage()];
void\u onTappedBar(整数索引){
设置状态(){
_currentIndex=索引;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
//appBar:appBar(
//标题:文本(“应用程序”),
// ),
正文:_children[_currentIndex],
底部导航栏:底部导航栏(
onTap:_onTappedBar,
currentIndex:_currentIndex,
项目:[
底部导航气压计(
图标:图标(图标、图书馆和音乐),
标题:文本(“音乐”),
),
底部导航气压计(
图标:图标(Icons.favorite),
标题:文本(“收藏夹”),
),
],
),
);
}
}
这音乐是飞镖
导入“包装:颤振/材料.省道”;
类MusicPage扩展StatefulWidget{
音乐页面({Key}):超级(Key:Key);
@凌驾
_MusicPageState createState()=>\u MusicPageState();
}
类_musicpeartate扩展状态
使用AutomaticEpaLiveClientMixin{
final _karaokeList=Karaoke.getAll();
最终_savedkaraok={};
@凌驾
小部件构建(构建上下文){
super.build(上下文);
返回脚手架(
appBar:appBar(
标题:文本(“音乐”),
),
正文:_buildListViewKaraokeList(),
);
}
小部件_buildListViewKaraokeList(){
返回ListView.builder(
itemCount:_karaokeList.length,
itemBuilder:(上下文,索引)=>
_BuildListIlekaraokeList(_卡拉OK列表[索引]),
);
}
Widget_buildListTileKaraokeList(卡拉OK){
final _istaped=_savedKaraoke.contains(卡拉OK);
返回列表块(
前导:文本(karaoke.maBaiHat),
标题:文本(卡拉OK.tenBaiHat),
字幕:文本(卡拉OK.tenCaSi),
尾随:图标(
_isTapped?图标。收藏夹:图标。收藏夹\u边框,
颜色:_istaped?颜色。红色:空,
),
onTap:(){
设置状态(){
如果(_istaped){
_保存卡拉OK。删除(卡拉OK);
}否则{
_savedKaraoke.add(卡拉OK);
}
});
},
);
}
@凌驾
bool get wantKeepAlive=>true;
}
这是你最喜欢的音乐页面
类MusicFavoritePage扩展StatefulWidget{
MusicFavoritePage({Key}):超级(Key:Key);
@凌驾
_MusicFavoritePageState createState()=>\u MusicFavoritePageState();
}
类_MusicFavoritePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“音乐收藏”),
),
正文:中(
子:文本(“音乐收藏页”),
),
);
}
}
**GIF:
[1] :现在,无论何时访问音乐页面
,\u savedkaroke
列表都会在此处设置一个空数组,如{}
:
final _savedKaraoke = <Karaoke>{};
final _savedkaraok={};
对于一个非常简单的解决方案,您可以使用获取或设置\u savedKaraoke
列表。您可以复制粘贴运行下面的完整代码
您可以使用PageView
,您可以看到下面的工作演示
代码片段
void _onTappedBar(int index) {
print("onItemSelected");
setState(() => _currentIndex = index);
_pageController.jumpToPage(index);
}
...
body: SizedBox.expand(
child: PageView(
controller: _pageController,
children: _children,
onPageChanged: (int index) {
print("onPageChanged");
setState(() {
_currentIndex = index;
});
},
),
工作演示
完整代码
import 'package:flutter/material.dart';
class Karaoke {
String maBaiHat;
String tenBaiHat;
String tenCaSi;
Karaoke({this.maBaiHat, this.tenBaiHat, this.tenCaSi});
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(title: 'Flutter Demo Home Page'),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _currentIndex = 0;
PageController _pageController = PageController();
final List<Widget> _children = [MusicPage(), MusicFavoritePage()];
void _onTappedBar(int index) {
print("onItemSelected");
setState(() => _currentIndex = index);
_pageController.jumpToPage(index);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox.expand(
child: PageView(
controller: _pageController,
children: _children,
onPageChanged: (int index) {
print("onPageChanged");
setState(() {
_currentIndex = index;
});
},
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
selectedItemColor: Colors.amber[800],
unselectedItemColor: Colors.blue,
onTap: _onTappedBar,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.library_music),
title: Text("Music"),
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
title: Text("Favorite"),
),
],
),
);
}
}
class MusicPage extends StatefulWidget {
MusicPage({Key key}) : super(key: key);
@override
_MusicPageState createState() => _MusicPageState();
}
class _MusicPageState extends State<MusicPage>
with AutomaticKeepAliveClientMixin {
final _karaokeList = [
Karaoke(maBaiHat: "a", tenBaiHat: "a1", tenCaSi: "a2"),
Karaoke(maBaiHat: "b", tenBaiHat: "b1", tenCaSi: "b2"),
Karaoke(maBaiHat: "c", tenBaiHat: "c1", tenCaSi: "c2")
]; //Karaoke.getAll();
final _savedKaraoke = <Karaoke>{};
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
appBar: AppBar(
title: Text("Music"),
),
body: _buildListViewKaraokeList(),
);
}
Widget _buildListViewKaraokeList() {
return ListView.builder(
itemCount: _karaokeList.length,
itemBuilder: (context, index) =>
_buildListTileKaraokeList(_karaokeList[index]),
);
}
Widget _buildListTileKaraokeList(Karaoke karaoke) {
final _isTapped = _savedKaraoke.contains(karaoke);
return ListTile(
leading: Text(karaoke.maBaiHat),
title: Text(karaoke.tenBaiHat),
subtitle: Text(karaoke.tenCaSi),
trailing: Icon(
_isTapped ? Icons.favorite : Icons.favorite_border,
color: _isTapped ? Colors.red : null,
),
onTap: () {
setState(() {
if (_isTapped) {
_savedKaraoke.remove(karaoke);
} else {
_savedKaraoke.add(karaoke);
}
});
},
);
}
@override
bool get wantKeepAlive => true;
}
class MusicFavoritePage extends StatefulWidget {
MusicFavoritePage({Key key}) : super(key: key);
@override
_MusicFavoritePageState createState() => _MusicFavoritePageState();
}
class _MusicFavoritePageState extends State<MusicFavoritePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Music Favorite"),
),
body: Center(
child: Text("Music Favorite Page"),
),
);
}
}
导入“包装:颤振/材料.省道”;
班级卡拉OK{
串马拜帽;
弦琴;
弦天丝;
卡拉OK({this.maBaiHat,this.tenBaiHat,this.tenCaSi});
}
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:主页(标题:“颤振演示主页”),
);
}
}
类主页扩展了StatefulWidget{
主页({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
int _currentIndex=0;
PageController_PageController=PageController();
最终列表_children=[MusicPage(),MusicFavoritePage()];
void\u onTappedBar(整数索引){
打印(“选定”);
设置状态(()=>_currentIndex=index);
_pageController.jumpToPage(索引);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:SizedBox.expand(
子:页面视图(
控制器:_pageController,
儿童:_儿童,
onPageChanged:(int索引){
打印(“更改页面”);
设置状态(){
_currentIndex=索引;
});
},
),
),
底部导航栏:底部导航栏(
currentIndex:_currentIndex,
selectedItemColor:Colors.amber[800],
unselectedItemColor:Colors.blue,
onTap:_onTappedBar,
项目:常数[
底部导航气压计(
图标:图标(图标、图书馆和音乐),
标题:文本(“音乐”),
),
底部导航气压计(
图标:图标(Icons.favorite),
标题:文本(“收藏夹”),
),
],
),
);
}
}
音乐类外景
final _savedKaraoke = <Karaoke>{};
void _onTappedBar(int index) {
print("onItemSelected");
setState(() => _currentIndex = index);
_pageController.jumpToPage(index);
}
...
body: SizedBox.expand(
child: PageView(
controller: _pageController,
children: _children,
onPageChanged: (int index) {
print("onPageChanged");
setState(() {
_currentIndex = index;
});
},
),
import 'package:flutter/material.dart';
class Karaoke {
String maBaiHat;
String tenBaiHat;
String tenCaSi;
Karaoke({this.maBaiHat, this.tenBaiHat, this.tenCaSi});
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(title: 'Flutter Demo Home Page'),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _currentIndex = 0;
PageController _pageController = PageController();
final List<Widget> _children = [MusicPage(), MusicFavoritePage()];
void _onTappedBar(int index) {
print("onItemSelected");
setState(() => _currentIndex = index);
_pageController.jumpToPage(index);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox.expand(
child: PageView(
controller: _pageController,
children: _children,
onPageChanged: (int index) {
print("onPageChanged");
setState(() {
_currentIndex = index;
});
},
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
selectedItemColor: Colors.amber[800],
unselectedItemColor: Colors.blue,
onTap: _onTappedBar,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.library_music),
title: Text("Music"),
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
title: Text("Favorite"),
),
],
),
);
}
}
class MusicPage extends StatefulWidget {
MusicPage({Key key}) : super(key: key);
@override
_MusicPageState createState() => _MusicPageState();
}
class _MusicPageState extends State<MusicPage>
with AutomaticKeepAliveClientMixin {
final _karaokeList = [
Karaoke(maBaiHat: "a", tenBaiHat: "a1", tenCaSi: "a2"),
Karaoke(maBaiHat: "b", tenBaiHat: "b1", tenCaSi: "b2"),
Karaoke(maBaiHat: "c", tenBaiHat: "c1", tenCaSi: "c2")
]; //Karaoke.getAll();
final _savedKaraoke = <Karaoke>{};
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
appBar: AppBar(
title: Text("Music"),
),
body: _buildListViewKaraokeList(),
);
}
Widget _buildListViewKaraokeList() {
return ListView.builder(
itemCount: _karaokeList.length,
itemBuilder: (context, index) =>
_buildListTileKaraokeList(_karaokeList[index]),
);
}
Widget _buildListTileKaraokeList(Karaoke karaoke) {
final _isTapped = _savedKaraoke.contains(karaoke);
return ListTile(
leading: Text(karaoke.maBaiHat),
title: Text(karaoke.tenBaiHat),
subtitle: Text(karaoke.tenCaSi),
trailing: Icon(
_isTapped ? Icons.favorite : Icons.favorite_border,
color: _isTapped ? Colors.red : null,
),
onTap: () {
setState(() {
if (_isTapped) {
_savedKaraoke.remove(karaoke);
} else {
_savedKaraoke.add(karaoke);
}
});
},
);
}
@override
bool get wantKeepAlive => true;
}
class MusicFavoritePage extends StatefulWidget {
MusicFavoritePage({Key key}) : super(key: key);
@override
_MusicFavoritePageState createState() => _MusicFavoritePageState();
}
class _MusicFavoritePageState extends State<MusicFavoritePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Music Favorite"),
),
body: Center(
child: Text("Music Favorite Page"),
),
);
}
}