gpt4 book ai didi

flutter - 我可以在 flutter 的一页中两次使用 future builder 吗

转载 作者:行者123 更新时间:2023-12-03 03:32:13 25 4
gpt4 key购买 nike

我已经删除了上一个问题,并用这个问题替代了它,这可能是最清楚的,
我从API接收数据,并将其转换为List contains(id,title,description,activity,degree)。
现在,我想显示如下图所示的数据:
注意:(所有行的标题和描述都相同)
enter image description here
上课页面:

class Digree {
final int index;
final String title_k;
final String title_a;
final String aya;
final String link;
final String activity_k;
final String activity_a;
final String udigree;
Digree(this.index, this.title_k, this.title_a, this.aya, this.link,
this.activity_k, this.activity_a, this.udigree);
}
future 功能页面
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:jiyanUquraan/classes/viewdigree.dart';

class DisplayList extends StatefulWidget {
@override
_DisplayListState createState() => _DisplayListState();
}
class _DisplayListState extends State<DisplayList> {
@override
Widget build(BuildContext context) {
Map rdata = {};
List digrees = [];
double _value = 0;
var widthView = MediaQuery.of(context).size.width;
Future<List> fetchDigrees() async {
Map rdata = ModalRoute.of(context).settings.arguments;
int cm_id = int.parse(rdata['current_m_id'].toString());
int d_id = int.parse(rdata['d_id'].toString());
int w_id = int.parse(rdata['w_id'].toString());
int u_id = int.parse(rdata['u_id'].toString());
var url =
'http://10.0.2.2/jiyan/test/api/digrees/day_digree.php?u_id=$u_id&m_id=$cm_id&d_id=$d_id';
var response = await http.get(url);
var data = jsonDecode(response.body);
for (var x in data) {
Digree newdigree = Digree(
x['index'],
x['title_k'],
x['title_a'],
x['aya'],
x['link'],
x['activity_k'],
x['activity_a'],
x['udigree']);
digrees.add(newdigree);
}
print(digrees.length);
print(data);
return digrees;
}
return FutureBuilder(
future: fetchDigrees(),
builder: (context, snapshot) {
List digrees = snapshot.data;
if (snapshot.data == null) {
return Center(
child: Text("Loading"),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Directionality(
textDirection: TextDirection.rtl,
child: Column(
children: <Widget>[
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
border: Border.all(width: 2, color: Colors.white),
color: Color.fromRGBO(230, 200, 200, 0.2)),
width: widthView,
padding: EdgeInsets.all(25),
margin: EdgeInsets.all(25),
child: Column(
children: <Widget>[
Text(
snapshot.data[index].activity_k,
textAlign: TextAlign.justify,
style:
TextStyle(fontSize: 32, color: Colors.white),
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.red[700],
inactiveTrackColor: Colors.red[100],
trackShape: RectangularSliderTrackShape(),
trackHeight: 4.0,
thumbColor: Colors.redAccent,
thumbShape: RoundSliderThumbShape(
enabledThumbRadius: 12.0),
overlayColor: Colors.red.withAlpha(32),
overlayShape: RoundSliderOverlayShape(
overlayRadius: 28.0),
),
child: Slider(
value: 0,
min: 0,
max: 100,
divisions: 10,
label: '$_value',
onChanged: null,
),),],),),],),);});}},);}}
显示页面:
import 'package:flutter/material.dart';
import 'package:jiyanUquraan/components/daylist.dart';
class Days extends StatefulWidget {
@override
_DaysState createState() => _DaysState();
}
class _DaysState extends State<Days> {
var cm_id;
var d_id;
var w_id;
var u_id;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.pink[900],
title: Text(
'ژیان و قورئان',
style: TextStyle(fontSize: 30),
),
centerTitle: true,
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/background.png"),
fit: BoxFit.cover,
),
),
child: DisplayList()),
);
}
}

最佳答案

答案3.0
因此,据我对情况的正确理解,需要先调用title_kaya,然后再调用Directionality()构建的Column()。它将根据Directionality列表包装snapshot.data

ListView(
shrinkWrap: true,
children: [
Text('title_k'),
Text('aya'),
Column(
children: snapshot.data.map<Widget>((e) => Directionality(...)).toList()
)
]
)
请注意:您必须以 Directionality的形式获取 e.key_of_digree_model中的数据。 e可以是任何东西,无论如何,您将描述 map((item)) 例如: Text(snapshot.data[index].activity_k)仅在 Text(item.activity_k)中成为 Directionality现在,我们将编写正常的代码,它将为您提供答案。具体来说,您的答案仅在 DisplayList()中。因此,将其发布为答案
假设:我可以看到每个 title_kayaDigree Model都是相同的,因此我仅从 title_k中获取 ayaindex = 0。像这样
// title
Text(snapshot.data[0].title_k)

//Description
Text(snapshot.data[0].aya)
DisplayList()
//specific code only, since Column() has all the data required
// inside your else of FutureBuilder
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: ListView(
shrinkWrap: true,
children: [
//title
Text(snapshot.data[0].title_k),
//for top margin
SizedBox(height: 20.0),
// dexription
Text(snapshot.data[0].aya),
//for top margin
SizedBox(height: 20.0),

// your Directionality
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: snapshot.data.map<Widget>((item){
// for example snapshot.data[index].activity_k will become
// item.activity_k
return Directionality(....);
}).toList()
)
]
)
);
请阅读有关 Mapping a list item StackoverFlow的信息

关于flutter - 我可以在 flutter 的一页中两次使用 future builder 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63307358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com