ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Flutter实战之实现一个简单的新闻阅读器,androidrom开发环境

2021-12-22 15:59:00  阅读:185  来源: 互联网

标签:TextStyle style TAB androidrom Text TabBar 阅读器 new Flutter


),
new Text(
StringResources.TAB_SH_CN,
style: new TextStyle(
fontSize: 20.0
)
),
new Text(
StringResources.TAB_GN_CN,
style: new TextStyle(
fontSize: 20.0
)
),
new Text(
StringResources.TAB_GJ_CN,
style: new TextStyle(
fontSize: 20.0
)
),
new Text(
StringResources.TAB_YL_CN,
style: new TextStyle(
fontSize: 20.0
)
),
new Text(
StringResources.TAB_TY_CN,
style: new TextStyle(
fontSize: 20.0
)
),
new Text(
StringResources.TAB_JS_CN,
style: new TextStyle(
fontSize: 20.0
)
),
new Text(
StringResources.TAB_KJ_CN,
style: new TextStyle(
fontSize: 20.0
)
),
new Text(
StringResources.TAB_CJ_CN,
style: new TextStyle(
fontSize: 20.0
)
),
new Text(
StringResources.TAB_SS_CN,
style: new TextStyle(
fontSize: 20.0
)
)
];
final List tabs = [];
for (int

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

i = 0 ; i < tabTexts.length ; i++) {
tabs.add(
new Tab(
child: tabTexts[i],
)
);
}

return new DefaultTabController(
length: tabs.length,
child: new Scaffold(
appBar: new AppBar(
title: new Text(
widget.title
),
bottom: new TabBar(
isScrollable: true,
tabs: tabs,

),
),
body: new TabBarView(
children: tabTexts.map((Text tab) {
return new Center(
child: new NewsPage(
tabName: tab.data,
)
);
}).toList()
),
drawer: new DrawerPage(),
)
);
}

主要通过DefaultTabController + TabBar + TabBarView三个Widget配合实现类似Android中ViewPager + Indicator的效果。

build方法返回的最外层是一个DefaultTabController Widget,正如它的名字那样,它是一个控制器,用来控制TabBar和TabBarView的联动。要在界面上绘制一个TabBar和其对应的内容页使用TabBar和TabBarView两个Widget来实现的。

通过配置Scaffold的appbar参数为界面添加一个标题栏,再配置AppBar的bottom参数为标题栏添加一个显示在其底部的Widget,在这里传递给bottom参数的是一个TabBar Widget,所以在标题栏下方会显示一个TabBar。TabBar的每个Tab默认是固定位置的,这里配置其isScrollable参数为true使其可滚动,tabs参数配置TabBar中的每个Tab Widget。

通过配置Scaffold的body参数为其添加主内容界面,这里的内容界面需要配合TabBar的切换而切换,所以配置一个TabBarView Widget。最终通过外层的DefaultTabController使得TabBar和TabBarView联动起来。

关于实现类似效果的更多详细信息可参阅DefaultTabControllerTabBarTabBarView

  • Drawer实现:

return new DefaultTabController(
length: tabs.length,
child: new Scaffold(
appBar: new AppBar(
title: new Text(
widget.title
),
bottom: new TabBar(
isScrollable: true,
tabs: tabs,

),
),
body: new TabBarView(
children: tabTexts.map((Text tab) {
return new Center(
child: new NewsPage(
tabName: tab.data,
)
);
}).toList()
),
drawer: new DrawerPage(),
)
);

Drawer的实现也很简单,Flutter都已经封装好了,直接配置Scaffold的drawer参数来实现一个Drawer界面。

  • 数据缓存:

利用shared_preferences和sqflite插件分别实现SharePreference和DB的数据本地缓存,在项目的pubspec.yaml中配置依赖的插件:

dependencies:


sqflite: “^0.8.3”
shared_preferences: “^0.4.0”

然后就可以在使用的地方import相应的package来使用对应功能。

标签:TextStyle,style,TAB,androidrom,Text,TabBar,阅读器,new,Flutter
来源: https://blog.csdn.net/m0_65511992/article/details/122087744

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有