美文网首页
Flutter-Scaffold组件

Flutter-Scaffold组件

作者: 阿博聊编程 | 来源:发表于2022-05-20 10:26 被阅读0次
    配图来自网络,如侵必删

    Flutter开发当中,我们可能会遇到以下的需求:

    实现页面组合使用,比如说有悬浮按钮、顶部菜单栏、左右抽屉侧边栏、底部导航栏等等效果。

    Scaffold组件可以帮我们实现上面需求说的效果。这篇博客主要分享容器组件的Scaffold组件的使用,希望对看文章的小伙伴有所帮助。

    简单示例代码

    Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          backgroundColor: Colors.yellow,
          bottomNavigationBar: BottomAppBar(
            color: Colors.white,
            shape: const CircularNotchedRectangle(),
            child: Container(
              height: 50,
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        )
    

    效果如下所示:


    image.png

    组件源码

    const Scaffold({
        Key? key,
        this.appBar,
        this.body,
        this.floatingActionButton,
        this.floatingActionButtonLocation,
        this.floatingActionButtonAnimator,
        this.persistentFooterButtons,
        this.drawer,
        this.onDrawerChanged,
        this.endDrawer,
        this.onEndDrawerChanged,
        this.bottomNavigationBar,
        this.bottomSheet,
        this.backgroundColor,
        this.resizeToAvoidBottomInset,
        this.primary = true,
        this.drawerDragStartBehavior = DragStartBehavior.start,
        this.extendBody = false,
        this.extendBodyBehindAppBar = false,
        this.drawerScrimColor,
        this.drawerEdgeDragWidth,
        this.drawerEnableOpenDragGesture = true,
        this.endDrawerEnableOpenDragGesture = true,
        this.restorationId,
      }) : assert(primary != null),
           assert(extendBody != null),
           assert(extendBodyBehindAppBar != null),
           assert(drawerDragStartBehavior != null),
           super(key: key);
    

    组件属性说明

    这里针对源码做出相应的属性说明,熟悉控件的属性方便大家的使用。

    属性名称 属性说明
    backgroundColor 设置容器的背景颜色
    appBar 顶部状态栏
    body 容器的主体
    bottomNavigationBar 顶部导航栏
    floatingActionButton 悬浮按钮
    floatingActionButtonLocation 悬浮按钮的位置
    floatingActionButtonAnimator 悬浮按钮相关的动画设置
    drawer 侧边栏组件
    persistentFooterButtons 固定在下方显示的按钮

    完整的源码

    以下的代码,可以直接复制到编译器去运行,方便小伙伴查看运行结果或者直接使用:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      // This widget is the root of your application.
      @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> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          backgroundColor: Colors.yellow,
          bottomNavigationBar: BottomAppBar(
            color: Colors.white,
            shape: const CircularNotchedRectangle(),
            child: Container(
              height: 50,
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        );
      }
    }
    
    

    相关文章

      网友评论

          本文标题:Flutter-Scaffold组件

          本文链接:https://www.haomeiwen.com/subject/schfprtx.html