其他分享
首页 > 其他分享> > Flutter实战之实现一个简单的新闻阅读器,androidrom开发环境

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

作者:互联网

),
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

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