如何在 Flutter 中实现嵌套的 ListView? [英] How to implement Nested ListView in Flutter?

查看:92
本文介绍了如何在 Flutter 中实现嵌套的 ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现嵌套 ListView 的首选方法是什么,或者换句话说,可以包含在可滚动父级中的 ListView 小部件是什么?

想象一个报告"页面,其中一个部分是一个逐项列表.

解决方案

如果你想让内部 ListView 独立于主滚动视图滚动,你应该使用

import 'package:flutter/material.dart';无效主(){运行应用程序(新的我的应用程序());}class MyApp 扩展 StatelessWidget {@覆盖小部件构建(BuildContext 上下文){返回新的 MaterialApp(title: 'Flutter 演示',主题:新主题数据(主色板:Colors.blue,),主页:新的我的主页(),);}}class MyHomePage 扩展了 StatelessWidget {@覆盖小部件构建(BuildContext 上下文){返回新的脚手架(正文:新的 NestedScrollView(headerSliv​​erBuilder: (BuildContext context, bool innerBoxIsScrolled) {返回 <小部件>[新的 SliverAppBar(固定:真实,标题:新文本('颤振演示'),),];},正文:新列(孩子们:<小部件>[新的 FlutterLogo(大小:100.0,颜色:Colors.purple),新容器(高度:300.0,孩子:新的 ListView.builder(itemCount: 60,itemBuilder:(BuildContext 上下文,int 索引){return new Text('Item $index');},),),新的 FlutterLogo(大小:100.0,颜色:Colors.orange),],),),);}}

What is the preferred way to achieve a nested ListView, or in other words, ListView Widgets that could be included within a scrollable parent?

Imagine a "Reports" page, where a section is an itemized list.

解决方案

If you want to have the inner ListView be scrollable independently of the main scroll view, you should use NestedScrollView. Otherwise, use a CustomScrollView.

Here is some code illustrating the NestedScrollView approach.

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            new SliverAppBar(
              pinned: true,
              title: new Text('Flutter Demo'),
            ),
          ];
        },
        body: new Column(
          children: <Widget>[
            new FlutterLogo(size: 100.0, colors: Colors.purple),
            new Container(
              height: 300.0,
              child: new ListView.builder(
                itemCount: 60,
                itemBuilder: (BuildContext context, int index) {
                  return new Text('Item $index');
                },
              ),
            ),
            new FlutterLogo(size: 100.0, colors: Colors.orange),
          ],
        ),
      ),
    );
  }
}

这篇关于如何在 Flutter 中实现嵌套的 ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆