我可以将TabBar和TabBarView用作页面内小部件吗? [英] Can I use a TabBar and TabBarView as an in page widget?

查看:100
本文介绍了我可以将TabBar和TabBarView用作页面内小部件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在探索Flutter框架的过程中,我遇到了一个问题,我不知道如何解决.我可以将 TabBar

In course of my exploration of the flutter framework I came across a problem, I didn't know how to solve. Can I use TabBar in combination with TabBarView as a child of other widgets?

推荐答案

证明它可行.这是相关的代码片段:

Turns out it works. This is the relevant code snippet:

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: () {}),
        ),
      ),
    ],
  ),
),

这是一个有效的示例:

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),
            ),
          ),
        ],
      ),
    );
  }
}

这篇关于我可以将TabBar和TabBarView用作页面内小部件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆