【问题标题】:Segments in FlutterFlutter 中的分段
【发布时间】:2018-08-28 03:02:06
【问题描述】:

我正在开发一个 Flutter 应用程序并尝试向我的应用程序添加一个细分。是否有可能在 Flutter 中实现。所以我想要 2 个按钮的 2 个不同的小部件。类似于 Flutter 中的 TabBar 或原生应用中的 Segment

【问题讨论】:

  • 是的,当然有可能,到目前为止你尝试过什么?
  • @HemanthRaj 我尝试使用 TabBar 没有成功
  • 您想要达到的目标的一些视觉参考,例如参考图片?有问题的图片仅显示两个按钮。这可以通过行小部件来实现
  • @HemanthRaj 我已经更新了我的问题中的图片。所以我想要 2 个按钮的 2 个不同的小部件。类似于 Flutter 中的 TabBar 或原生应用中的 Segment
  • @HemanthRaj 如果我们在选项卡之间手动滑动,平面按钮索引不会改变。当用户在标签之间滑动时,是否可以限制滑动或更新按钮索引?

标签: tabs flutter segment


【解决方案1】:

CupertinoSegmentedControl 是你的朋友

示例(在 StatefulWidget 内部):

  int segmentedControlValue = 0;

  Widget _segmentedControl() => Container(
    width: 500,
    child: CupertinoSegmentedControl<int>(
      selectedColor: Colors.blue,
      borderColor: Colors.white,
      children: {
        0: Text('All'),
        1: Text('Recommendations'),
      },
      onValueChanged: (int val) {
        setState(() {
          segmentedControlValue = val;
        });
      },
      groupValue: segmentedControlValue,
    ),
  );

【讨论】:

    【解决方案2】:

    如您所愿,您可以使用TabBarView 轻松实现。下面的代码展示了如何实现它的一个非常基本的实现。

    例子:

    class Example extends StatefulWidget {
      @override
      _ExampleState createState() => new _ExampleState();
    }
    
    class _ExampleState extends State<Example> with SingleTickerProviderStateMixin {
      // TabController to control and switch tabs
      TabController _tabController;
    
      // Current Index of tab
      int _currentIndex = 0;
    
      @override
      void initState() {
        super.initState();
        _tabController =
            new TabController(vsync: this, length: 2, initialIndex: _currentIndex);
      }
    
      @override
      void dispose() {
        _tabController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text("Example"),
          ),
          body: new Column(
            children: <Widget>[
              new Padding(
                padding: const EdgeInsets.symmetric(vertical: 20.0),
                child: new Container(
                  decoration:
                      new BoxDecoration(border: new Border.all(color: Colors.blue)),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      // Sign In Button
                      new FlatButton(
                        color: _currentIndex == 0 ? Colors.blue : Colors.white,
                        onPressed: () {
                          _tabController.animateTo(0);
                          setState(() {
                            _currentIndex = 0;
                          });
                        },
                        child: new Text("SignIn"),
                      ),
                      // Sign Up Button
                      new FlatButton(
                        color: _currentIndex == 1 ? Colors.blue : Colors.white,
                        onPressed: () {
                          _tabController.animateTo(1);
                          setState(() {
                            _currentIndex = 1;
                          });
                        },
                        child: new Text("SignUp"),
                      )
                    ],
                  ),
                ),
              ),
              new Expanded(
                child: new TabBarView(
                    controller: _tabController,
                    // Restrict scroll by user
                    physics: const NeverScrollableScrollPhysics(),
                    children: [
                      // Sign In View
                      new Center(
                        child: new Text("SignIn"),
                      ),
                      // Sign Up View
                      new Center(
                        child: new Text("SignUp"),
                      )
                    ]),
              )
            ],
          ),
        );
      }
    }
    

    希望有帮助!

    【讨论】:

    • 如果我们在标签之间手动滑动,平面按钮索引不会改变。当用户在标签之间滑动时,是否可以限制滑动或更新按钮索引?
    • 是的,可以限制用户在视图之间手动滚动。您可以使用将new NeverScrollableScrollPhysics() 传递给TabBarViewphysics 属性。更新了我的答案以显示如何完成。
    猜你喜欢
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 2021-07-29
    • 2021-10-29
    • 1970-01-01
    • 2023-01-17
    相关资源
    最近更新 更多