【发布时间】:2020-01-23 09:11:08
【问题描述】:
我想创建(新闻应用),我在网站上有一个类别列表,我关注了这个documentation,所以我需要将网站的类别添加到我的TabBar,就像这张图片:
我该怎么做?
我想将方向从左到右更改为从右到左,我该怎么做?
代码:
Category Class:
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<Category> fetchCatgeory() async {
final response = await http.get("url");
if (response.statusCode == 200) {
return Category.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load category');
}
}
class Category {
final int id;
final String title;
Category({this.id, this.title});
factory Category.fromJson(Map<String, dynamic> json) {
return Category (
id: json['id'],
title: json['title'],
);
}
}
HomePage Class:
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:munaw3_news/extra/json_file.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
final Future<Category> catg;
HomePage({Key key, this.catg}) : super(key: key);
}
class _HomePageState extends State<HomePage> {
_HomePageState();
int a = 0;
Future<Category> catg;
@override
void initState() {
// TODO: implement initState
super.initState();
catg = fetchCatgeory();
}
bool isPressed = false;
_pressed() {
var newVal = true;
if(isPressed) {
newVal = false;
} else {
newVal = true;
}
setState((){
isPressed = newVal;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: DefaultTabController(
length: catg.toString().length,
child: Scaffold(
appBar: AppBar(
title: Image.asset('assets/logo.png', fit: BoxFit.cover,),
centerTitle: true,
backgroundColor: Colors.grey[900],
bottom: TabBar(
tabs: [
// Tab(text: catg[0],),
// Tab(text: catg[1],),
// Tab(text: catg[2],),
// Tab(text: catg[3],)
],
),
),
body: TabBarView(
children: [
// Tab(text: catg[0],),
// Tab(text: catg[1],),
// Tab(text: catg[2],),
// Tab(text: catg[3],)
],
),
bottomNavigationBar: BottomAppBar(
clipBehavior: Clip.antiAlias,
elevation: 0,
color: Colors.grey[900],
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
height: 5,
width: double.infinity,
color: Colors.grey[900],
),
Padding(
padding: const EdgeInsets.only(left: 8, right: 8),
child: Container(
height: 60,
child: Align(
alignment: FractionalOffset.center,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
IconButton(icon: Icon(Icons.info), color: Colors.grey[600], iconSize: 30,
disabledColor: Colors.white,
onPressed: a==3 ? null : () => setState(() {
a=3;
})),
IconButton(icon: Icon(Icons.local_mall), color: Colors.grey[600], iconSize: 30,
disabledColor: Colors.white,
onPressed: a==2 ? null : () => setState(() {
a=2;
})),
IconButton(icon: Icon(Icons.bookmark), color: Colors.grey[600], iconSize: 30,
disabledColor: Colors.white,
onPressed: a==1 ? null : () => setState(() {
a=1;
})),
IconButton(icon: Icon(Icons.home), color: Colors.grey[600], iconSize: 30,
disabledColor: Colors.white,
onPressed: a==0 ? null : () => setState(() {
a=0;
})),
],
),
),
),
),
],
),
),
),),
);
}
}
【问题讨论】:
-
你找到解决办法了吗?
-
不...! :(@莎拉