Flutter:拉动刷新和流生成器
Posted
技术标签:
【中文标题】Flutter:拉动刷新和流生成器【英文标题】:Flutter: Pull to refresh and stream builder 【发布时间】:2019-05-10 18:58:06 【问题描述】:我想要一个 RefreshIndicator,它作为一个子项有一个 StreamBuilder,它可以构建一个列表,但是它不起作用。这是我的代码:
return Scaffold(
appBar: buildAppBar(context),
body:RefreshIndicator(
onRefresh: _refresh,
child:StreamBuilder(
stream: list.stream,
builder: (context, snapshot)
return buildListView(snapshot)
Future<Null> _refresh() async
loadList.sink.add(x);
return null;
【问题讨论】:
什么不起作用?什么是 loadList 和列表变量/ 它根本不允许我拉。就像他不认识这个手势一样 在您的方法中 -buildListView
添加 ListView( physics: const AlwaysScrollableScrollPhysics(),
【参考方案1】:
RefreshIndicator 仅在其后代可滚动时出现。你可以在这里做的是在buildListView()
上添加一个ListView。
这是一个演示。
示例代码。
import 'dart:async';
import 'package:flutter/material.dart';
void main()
runApp(const MyApp());
class MyApp extends StatelessWidget
const MyApp(Key? key) : super(key: key);
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
class MyHomePage extends StatefulWidget
const MyHomePage(Key? key, required this.title) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
_increment()
bloc.updateIncrement();
@override
void dispose()
super.dispose();
bloc.dispose();
@override
Widget build(BuildContext context)
return StreamBuilder<int>(
stream: bloc.incrementCounter,
builder: (context, AsyncSnapshot<int> snapshot)
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: RefreshIndicator(
onRefresh: () async => _increment(),
child: ListView.builder(
// if snapshot data is null, set to 0
itemCount: snapshot.data ?? 0,
itemBuilder: (BuildContext context, int index)
return Card(
child: SizedBox(
height: 64,
child: Text('Item $index'),
),
);
,
),
),
);
,
);
class Bloc
int counter = 0;
final _incrementFetcher = StreamController<int>.broadcast();
Stream<int> get incrementCounter => _incrementFetcher.stream;
updateIncrement()
_incrementFetcher.sink.add(counter+=1);
dispose()
_incrementFetcher.close();
final bloc = Bloc();
【讨论】:
以上是关于Flutter:拉动刷新和流生成器的主要内容,如果未能解决你的问题,请参考以下文章