首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flutter Stepper -显示所有内容

Flutter Stepper -显示所有内容
EN

Stack Overflow用户
提问于 2019-02-26 05:46:23
回答 3查看 2.8K关注 0票数 3

我需要一个Stepper来显示一些东西的当前状态(在步骤中)。因此,我想一次显示所有状态的内容。我删除了默认的Continue和Cancel按钮,但它只显示第一步的内容。

这是我的代码:

代码语言:javascript
复制
body: Center(
        child: new Stepper(
          steps: my_steps,
          controlsBuilder: (BuildContext context,
              {VoidCallback onStepContinue, VoidCallback onStepCancel}) {
            return Row(
              children: <Widget>[
                Container(
                  child: null,
                ),
                Container(
                  child: null,
                ),
              ],
            );
          },
        ),
      ),

下面是我的步骤:

代码语言:javascript
复制
List<Step> my_steps = [
    new Step(
        title: new Text("Step 1"),
        content: new Text("Hello 1!")),
    new Step(
        title: new Text("Step 2"),
        content: new Text("Hello 2!")
    ),
    new Step(
        title: new Text("Step 3"),
        content: new Text("Hello World!"),
    )
  ];

在这里,它只显示第一步的"Hello 1“。对于其余部分,内容是空的。

有没有人?

EN

回答 3

Stack Overflow用户

发布于 2019-12-10 04:45:39

您只需在步骤中使用isActive标记,即可显示用户已完成的所有状态。我想这会帮助你显示当前的状态。

要设置标记的所有状态-

代码语言:javascript
复制
bool getIsActive(int currentIndex, int index){
  if(currentIndex<=index){
    return true;
  }else{
    return false;
  }
}

和你的代码

代码语言:javascript
复制
body: Center(
    child: new Stepper(
      steps: [
    new Step(
      title: new Text("Step 1"),
      content: new Text("Hello 1!"),
      isActive: getIsActive(0,_index),
    ),
    new Step(
      title: new Text("Step 2"),
      content: new Text("Hello 2!"),
      isActive: getIsActive(1,_index),
    ),
    new Step(
      title: new Text("Step 3"),
      content: new Text("Hello World!"),,
      isActive: getIsActive(2,_index),
    )
   ],
      controlsBuilder: (BuildContext context,
          {VoidCallback onStepContinue, VoidCallback onStepCancel}) => Container(),
    ),
  ),
票数 1
EN

Stack Overflow用户

发布于 2020-02-04 19:46:07

我做到了:

  1. 将MaterialUI的Stepper类从目录flutter/lib/src/material/stepper.dart克隆到项目中的另一个目录。例如,您复制它并将新modified_stepper.dart中的两个类名重命名为您的/lib/widget/modified_stepper.dart.
  2. Rename,这样在您使用它时就不会引起冲突。有两个类需要重命名:StepperStep。例如,将它们重命名为ModifiedStepperModifiedStep.

(提示:如果你使用的是Visual Studio Code,你可以使用多个光标来更快地执行重命名,方法是将光标移到类名上,然后按Ctrl + F2 (Windows)或Cmd + F2 (Mac))

  1. 在这个新的ModifiedStepper类中,找到方法_isCurrent,它将如下所示:

代码语言:javascript
复制
bool _isCurrent(int index) {
  return widget.currentStep == index;
}

此方法用于检测当前显示的是哪个步骤:如果currentStep等于该步骤的index,则显示该步骤。所以,简单地做一个小把戏:返回true将始终按照您期望的方式工作:

代码语言:javascript
复制
bool _isCurrent(int index) {
  return true;
  // return widget.currentStep == index;
}

现在你可以导入你的ModifiedStepper来使用了。

票数 1
EN

Stack Overflow用户

发布于 2019-02-26 08:25:23

根据我对你的问题的理解,你实际上有两个问题。

  1. 第一个问题是您没有使用适当的Flutter小部件来完成您的任务。Stepper
  2. 第二个问题是你的

示例代码中有一个bug。

由于修复问题#2不会以任何方式解决问题#1,因此我将提出以下建议作为您的解决方案:

Flutter的Stepper Widget相互工作的隐式方式排除了同时显示处于活动状态的所有步骤。从根本上说,Stepper并不是为做你想做的事情而设计的。在您的例子中,一种更好的方法可能是使用类似ListBuilder的代码:

代码语言:javascript
复制
import 'dart:io' as Io;
import 'package:flutter/material.dart';

class zListTileExample extends StatefulWidget {
  zListTileExample({Key key}) : super(key: key);

  @override
  zListTileExampleState createState() => zListTileExampleState();
}

class zListTileExampleState extends State <zListTileExample> {

  List<SampleStepTile> my_steps = [
    new SampleStepTile(
        title: new Text("Step 1"),
        content: new Text("Hello 1!")),
    new SampleStepTile(
        title: new Text("Step 2"),
        content: new Text("Hello 2!")
    ),
    new SampleStepTile(
      title: new Text("Step 3"),
      content: new Text("Hello World!"),
    )
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
    title: Text("My App Title"),
    ),
    body:

        ListView.builder(
          itemCount: my_steps.length,
          itemBuilder: (context, index) {
              return
                Column ( children: <Widget> [
                  ListTile(
                    title: my_steps[index].title ,
                    subtitle: my_steps[index].content
                    )
                  , Row ( children : <Widget> [
                      RaisedButton ( child: new Text ("Use") 
                          , onPressed: () { print ("Step $index is ok");}),
                      RaisedButton ( child: new Text ("Cancel") 
                          , onPressed: () { print ("Step $index is canceled");})
                      ]
                    )
                  ]
                );
              },
            )
      );
    }
}

class SampleStepTile {

  SampleStepTile({Key key, this.title, this.content });

  Text  title;
  Text content;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54875152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档