Flutter中CustomScrollView_SliverGrid和SliverList

2022年7月14日 1002点热度 0人点赞 0条评论

例子效果图:
Flutter中CustomScrollView_SliverGrid和SliverList插图

代码:

<code>import &#039;dart:math&#039;;
import &#039;package:flutter/material.dart&#039;;

main(){
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        // appBar: AppBar(
        //   title: const Text(&quot;SliverGrid&quot;),
        //   centerTitle: true,
        // ),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
        slivers: [
           SliverAppBar(
            expandedHeight: 300,
            flexibleSpace: FlexibleSpaceBar(
              title: const Text(&quot;CustomScrollView 例子&quot;),
              background: Image.asset(&quot;assets/images/lizi.jpeg&quot;,fit: BoxFit.cover,),
            ),
            pinned: true,//当往上拉的时候,true表示标题栏固定在头部,否则会随着内容一起消失
          ),
           SliverGrid(
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 4,
                    mainAxisSpacing: 8,
                    crossAxisSpacing: 8,
                    childAspectRatio: 1.5
                ),
                delegate: SliverChildBuilderDelegate(
                        (BuildContext context,int index){
                      return Container(
                        color: Color.fromARGB(255, Random().nextInt(255), Random().nextInt(255), Random().nextInt(255)),
                      );
                    },
                    childCount: 12
                )
            ),
            SliverList(
                   delegate: SliverChildBuilderDelegate(
                           (BuildContext context, int index) {
                         return Text(&quot;这是标题${index+1}&quot;);
                       },
                       childCount: 30
                   )
               ),

        ],
      );
  }
}</code>

小小调酒师

此刻打盹,你将做梦; 此刻学习,你将圆梦。 个人邮箱:shellways@foxmail.com

文章评论