Flutter 在同一页面显示List和Grid
阅读原文时间:2023年07月11日阅读:1

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: ListView(
        children: <Widget>[
          ListTile(
            title: Text('data'),
          ),
          ListTile(
            title: Text('data'),
          ),
          GridView.count(
            primary:
                false, // https://api.flutter.dev/flutter/widgets/ScrollView/primary.html
            shrinkWrap: true, // 限制约束 https://api.flutter.dev/flutter/widgets/ScrollView/shrinkWrap.html
            crossAxisCount: 3,
            children: List<Widget>.generate(
                7,
                (_) => Container(
                      color: Colors.red,
                      margin: const EdgeInsets.all(8),
                    )),
          ),
          ListTile(
            title: Text('data'),
          ),
          ListTile(
            title: Text('data'),
          ),
        ],
      ),
    );
  }
}


import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: HomePage());
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: CustomScrollView(
        slivers: <Widget>[
          SliverList(
              delegate: SliverChildListDelegate([
            ListTile(title: Text("data")),
            ListTile(title: Text("data")),
          ])),
          SliverGrid.count(
            crossAxisCount: 3,
            children: List<Widget>.generate(
              7,
              (_) => Container(
                color: Colors.red,
                margin: const EdgeInsets.all(8),
              ),
            ),
          ),
          SliverList(
              delegate: SliverChildListDelegate([
            ListTile(title: Text("data")),
            ListTile(title: Text("data")),
          ])),
        ],
      ),
    );
  }
}