tabs - 我可以将 TabBar 和 TabBarView 用作页面小部件吗?

标签 tabs flutter

在探索flutter框架的过程中遇到了一个问题,不知道怎么解决。我可以使用 TabBar结合 TabBarView作为其他小部件的 child ?

最佳答案

事实证明它有效。这是相关的代码片段:

new Container(
  decoration: new BoxDecoration(color: Theme.of(context).primaryColor),
  child: new TabBar(
    controller: _controller,
    tabs: [
      new Tab(
        icon: const Icon(Icons.home),
        text: 'Address',
      ),
      new Tab(
        icon: const Icon(Icons.my_location),
        text: 'Location',
      ),
    ],
  ),
),
new Container(
  height: 80.0,
  child: new TabBarView(
    controller: _controller,
    children: <Widget>[
      new Card(
        child: new ListTile(
          leading: const Icon(Icons.home),
          title: new TextField(
            decoration: const InputDecoration(hintText: 'Search for address...'),
          ),
        ),
      ),
      new Card(
        child: new ListTile(
          leading: const Icon(Icons.location_on),
          title: new Text('Latitude: 48.09342\nLongitude: 11.23403'),
          trailing: new IconButton(icon: const Icon(Icons.my_location), onPressed: () {}),
        ),
      ),
    ],
  ),
),

这是一个有效的例子:

Screenshot

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',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  TabController _controller;

  @override
  void initState() {
    super.initState();
    _controller = new TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('TestProject'),
      ),
      body: new ListView(
        children: <Widget>[
          new Card(
            child: new ListTile(
              title: const Text('Some information'),
            ),
          ),
          new Container(
            decoration: new BoxDecoration(color: Theme.of(context).primaryColor),
            child: new TabBar(
              controller: _controller,
              tabs: [
                new Tab(
                  icon: const Icon(Icons.home),
                  text: 'Address',
                ),
                new Tab(
                  icon: const Icon(Icons.my_location),
                  text: 'Location',
                ),
              ],
            ),
          ),
          new Container(
            height: 80.0,
            child: new TabBarView(
              controller: _controller,
              children: <Widget>[
                new Card(
                  child: new ListTile(
                    leading: const Icon(Icons.home),
                    title: new TextField(
                      decoration: const InputDecoration(hintText: 'Search for address...'),
                    ),
                  ),
                ),
                new Card(
                  child: new ListTile(
                    leading: const Icon(Icons.location_on),
                    title: new Text('Latitude: 48.09342\nLongitude: 11.23403'),
                    trailing: new IconButton(icon: const Icon(Icons.my_location), onPressed: () {}),
                  ),
                ),
              ],
            ),
          ),
          new Card(
            child: new ListTile(
              title: const Text('Some more information'),
            ),
          ),
          new RaisedButton(
            color: Theme.of(context).primaryColor,
            onPressed: () {},
            child: const Text(
              'Search for POIs',
              style: const TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
    );
  }
}

关于tabs - 我可以将 TabBar 和 TabBarView 用作页面小部件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45588300/

相关文章:

javascript - jQuery 选项卡 : Get rid of 'previous' navigation button on second tab

html - CSS 选项卡与 Google Chrome 一样

jquery-ui - jQuery 选项卡 - 仅在单击时加载内容

json - 如何在 Flutter App 启动前显示 CircularProgressIndicator?

flutter - (RenderViewport 不支持返回固有维度

android-studio - 在Flutter中五秒钟后隐藏互联网连接栏

Android:选项卡中嵌套 Activity 的选项菜单

background - 代号一 - 如何将自定义背景应用于命令菜单选项卡

dart - 使用 http.get Flutter 检索的带有 Utf-8 字符集的无效阿拉伯字符

Flutter 桌面窗口获取应用程序路径