美文网首页
Flutter AppBar基本用法、TabBar基本用法、自定

Flutter AppBar基本用法、TabBar基本用法、自定

作者: _悟_空 | 来源:发表于2021-08-31 09:26 被阅读0次

一、AppBar的基本配置


基本用法
隐藏左侧按钮
          appBar: AppBar(
            // automaticallyImplyLeading: false, // 隐藏左侧按钮
            leading: IconButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              icon: Icon(Icons.arrow_back_ios),
            ),
            title: Text('title'),
            centerTitle: true,
            actions: [
              IconButton(
                  onPressed: () {
                    print("这是一个Menu");
                  },
                  icon: Icon(Icons.menu)),
            ],
            backgroundColor: Colors.amber,
          ),

leading和actions 的图标需要是个按钮图标(IconButton)不然您的事件不好处理。
二、TabBar的基本用法
DefaultTabController 必须配置在 MaterialApp的里面Scaffold的外面,并且length 的长度必须与 TabBar的子集和TabBarView的子集保持一致。

DefaultTabController( // 必须配置
        length: 2, // ①  **②③的长度必须与①相同否则报错**
        child: Scaffold(
          appBar: AppBar(
            automaticallyImplyLeading: false,
            title: TabBar(
              tabs: [ // ②
                Tab(
                  text: "one",
                ),
                Tab(
                  text: 'two',
                )
              ],
            ),
          ),
          body: TabBarView(children: [ // ③
            Center(
              child: Text('OnePage'),
            ),
            Center(
              child: Text('TwoPage'),
            ),
          ]),
        ))
tabbar在title中
          // TabBar可以配置在bottom中
...
          appBar: AppBar(
            automaticallyImplyLeading: false,
            title: Text("头部"),
            centerTitle: true,
            bottom: TabBar(
              tabs: [
                Tab(
                  text: "one",
                ),
                Tab(
                  text: 'two',
                )
              ],
            ),
          ),
....
tabbar在bottom中
如果是多个Tab那么需要在TabBar添加属性
isScrollable: true,

三、自定义TabBarController

自定义TabBarController与TabBar的基本用法一样。只是增加的controller:这个参数。 如下图为注意点。


自定义TabBarController注意点
class _BaseTabControllerState extends State<BaseTabController>
    with SingleTickerProviderStateMixin { // ① 注意点
  late TabController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TabController(vsync: this, length: 2); // ②初始化
  }

  @override
  void dispose() {
    _controller.dispose(); // ③销毁
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: TabBar(
          controller: _controller,// ④加入TabBar
          tabs: [
            Tab(
              text: "one_controller",
            ),
            Tab(
              text: 'two_controller',
            )
          ],
        ),
      ),
      body: TabBarView(
        controller: _controller, // ⑤加入TabBarView
       children: [
        Container(
          margin: EdgeInsets.fromLTRB(5, 20, 0, 0),
          child: Text("OnePage_controller"),
        ),
        Container(
          child: Text('TwoPage_controller'),
        ),
      ]),
    );
  }
}

android studio快速生成TabBarController模板

1.在一个空页面输入sta回车
2.然后导入

import 'package:flutter/material.dart';

3.将
替换成

Demo地址

相关文章

  • Flutter AppBar基本用法、TabBar基本用法、自定

    一、AppBar的基本配置 leading和actions 的图标需要是个按钮图标(IconButton)不然您的...

  • Day3排序和筛选的高级用法

    基本用法、进阶用法、自定义排序、横向筛选、数据透视表筛选、辅助列的应用、高级筛选、动态筛选 1、基本用法 1.基本...

  • flutter 优秀工具文章地址

    多功能顶部导航appbar 多功能轮播、连续页面flutter_swiper详细用法

  • E战到底第10期-Day3

    一、学习内容及关联拓展——不为人知的排序和筛选的高级用法 (一)基本用法 1、基本用法-排序:升序、降序、自定义排...

  • iOS热更新

    技术: Flutter JSPtatch基本用法 Weex React Native Cordova 动态库

  • 定时器

    setTimeout和clearTimeout基本用法 setInterval和clearInterval基本用法...

  • 2019-11-16

    E战到底DAY14 SUMIF和SUMIFS函数 一.基本用法 SUMIF基本用法 SUMIFS基本用法 SUMI...

  • Flutter基础组件

    Text基本用法和外边框 图片的用法 官网例子 包含横向排列纵向排列,图片 参考链接 Flutter常用组件学习以...

  • 关于Glide v4 使用

    集成 配置 app gradle中: 混淆 用法 基本用法 自定义方法 v4特性 v4中Glide.with()....

  • flutter_swiper 图片轮播

    事例项目地址 flutter_swiper中文地址 以上链接中能看到 flutter_swiper 的基本用法。如...

网友评论

      本文标题:Flutter AppBar基本用法、TabBar基本用法、自定

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