Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在flatter中显示x个小部件_Flutter - Fatal编程技术网

Flutter 如何在flatter中显示x个小部件

Flutter 如何在flatter中显示x个小部件,flutter,Flutter,编辑: 下面是我现在的结果: 正如你所看到的,我做了很多工作,现在已经很好了。现在我在2个绿色按钮的顶部选择了5个红色数字(5-34-37-42-49)。目前,函数getWidget再次使用gridview返回5个红色数字,但不确定它是否是我需要使用的。你能帮我解决5个圆圈大小的问题吗,我需要它居中,而不是使用滚动 以下是我的完整代码: import 'package:flutter/material.dart'; import 'dart:math'; import 'dart:conver

编辑:

下面是我现在的结果:

正如你所看到的,我做了很多工作,现在已经很好了。现在我在2个绿色按钮的顶部选择了5个红色数字(5-34-37-42-49)。目前,函数getWidget再次使用gridview返回5个红色数字,但不确定它是否是我需要使用的。你能帮我解决5个圆圈大小的问题吗,我需要它居中,而不是使用滚动

以下是我的完整代码:

import 'package:flutter/material.dart';
import 'dart:math';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'package:flutter_app/menu_member.dart';
import 'package:flutter_app/globals.dart' as globals;

class Lotto extends StatefulWidget {
  @override
  _LottoState createState() => new _LottoState();
}

class _LottoState extends State<Lotto> {

  @override
  void initState() {
    super.initState();
  }

  var i=1;
  var nb_num=49;
  var no_select=[];
  var no_a_select=5;

  List<Color> colorList = List<Color>.generate(49, (int index) => Colors.lightBlue);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: new AppBar(
            title: new Text('GRILLE DE LOTTO'),
          ),
            body:
            Center(
              child: Column(
                children: <Widget>[
                  Container(
                    width:400,
                    height:30,
                    margin: const EdgeInsets.only(top: 10.0),
                      child : new Text("Selectionnez 5 numéros",textAlign: TextAlign.center,style: TextStyle(fontSize: 30.0),),
                  ),
                  Container(
                    width:400,
                    height:300,
                child: new GridView.count(
                crossAxisCount: 9,
                padding: const EdgeInsets.all(30.0),
                mainAxisSpacing: 4.0,
                crossAxisSpacing: 4.0,
                children: new List<Widget>.generate(49, (index) {
                  return new GestureDetector(
                    onTap: () {
                      setState(() {
                        if (colorList[index] == Colors.lightBlue) {
                          if (no_select.length<no_a_select) {
                            colorList[index] = Colors.redAccent;
                            no_select.add(index+1);
                          }
                          else {
                            showDialog(
                                context: context,
                                builder: (BuildContext context){
                                  return AlertDialog(
                                    title: Text("INFORMATION"),
                                    content: Text("Vous ne pouvez pas sélectionner plus de 5 numéros !!!"),
                                  );
                                }
                            );
                          }
                          print(no_select);
                        }
                        else {
                          colorList[index] = Colors.lightBlue;
                          no_select.remove(index+1);
                          print(no_select);
                        }
                      });
                    },
                    child: Container(
                      child: ClipOval(
                        child: Container(
                          color: colorList[index],
                          height: 20.0,
                          width: 20.0,
                          child: Center(
                            child: new Text((index+1).toString(),
                                style: TextStyle(color: Colors.white, fontSize: 24),
                                textAlign: TextAlign.center),
                          ),
                        ),
                      ),
                    ),
                  );
                }
                ),
              ),
                  ),
                  Container(
                    width:400,
                    height:30,
                    margin: const EdgeInsets.only(top: 10),
                      child : new Text("Vos Numéros",textAlign: TextAlign.center,style: TextStyle(fontSize: 30.0),),
                  ),
                  Container(
                    width:400,
                    height:80,
                    margin: const EdgeInsets.only(top: 10.0),
                    decoration: BoxDecoration(
                      border: Border.all(
                    color: Colors.lightBlueAccent,
                    width: 2,
                  ),
                      borderRadius: BorderRadius.circular(12),
                    ),
                  child:
                    getWidget()
                  ),
                  Container(
                    width:300,
                    height:45,
                    margin: const EdgeInsets.only(top: 10.0),
                    child:
                  RaisedButton(
                    color: Colors.green,
                    textColor: Colors.white,
                    padding: EdgeInsets.fromLTRB(9, 9, 9, 9),
                    child: Text('TIRAGE ALEATOIRE'),
                    onPressed: () {
                      Select_numbers();
                    },
                  ),
                  ),
                  Container(
                    width:300,
                    height:45,
                    margin: const EdgeInsets.only(top: 10.0),
                    child:
                  RaisedButton(
                    color: Colors.green,
                    textColor: Colors.white,
                    padding: EdgeInsets.fromLTRB(9, 9, 9, 9),
                    child: Text('VALIDER VOTRE GRILLE'),
                    onPressed: () {
                      Valide_grille();
                    },
                  ),
                  ),
                  ]
              )
                          ),
                        ),
                    );
  }

  getWidget() {
    if (no_select.length==0) {
      return Text("Pas de numéros");
    }
    else {
      return GridView.count(
          crossAxisCount: 5,
          padding: const EdgeInsets.all(10.0),
          mainAxisSpacing: 4.0,
          crossAxisSpacing: 4.0,
          children: new List<Widget>.generate(no_select.length, (index) {
            return  ClipOval(
            child: Container(
            color: Colors.red,
    height: 20.0,
    width: 20.0,
    child: Center(
    child: new Text((no_select[index].toString()),
    style: TextStyle(color: Colors.white, fontSize: 24),
    textAlign: TextAlign.center),
    ),
    ),
    );
          }
    )
      );
    }
  }

  Select_numbers() {
    setState(() {
      var j = 1;
      var num_sel;
      var pos_sel;
      no_select=[];
      colorList=[];
      colorList=List<Color>.generate(49, (int index) => Colors.lightBlue);
      var rng = new Random();
      List tab=[];
      tab = List.generate(49, (int index) => index + 1);
      print (tab);
      while (j <= no_a_select) {
        pos_sel = rng.nextInt(tab.length-1);
        num_sel=tab[pos_sel];
        no_select.add(num_sel);
        colorList[num_sel-1] = Colors.redAccent;
        tab.remove(num_sel);
        print(tab);
        j++;
      }
      print(no_select);
    });
  }

  Future Valide_grille() async{
    // For CircularProgressIndicator.
    bool visible = false ;
    // Showing CircularProgressIndicator.
    setState(() {
      visible = true ;
    });

    // SERVER LOGIN API URL
    var url = 'https://www.easytrafic.fr/game_app/valide_lotto.php';

    // Store all data with Param Name.
    var data = {'id_membre':globals.id_membre, 'result':no_select};

    print (data);

    var grille_encode=jsonEncode(data);

    print(grille_encode);

    // Starting Web API Call.
    var response = await http.post(url, body: grille_encode,headers: {'content-type': 'application/json','accept': 'application/json','authorization': globals.token});

    print(response.body);

    // Getting Server response into variable.
    var message = json.decode(response.body);

    // If the Response Message is Matched.
    if(message == 'OK')
    {
      print('VALIDATION DE LA GRILLE OK');
      // Hiding the CircularProgressIndicator.
      setState(() {
        visible = false;
      });

    }else{
      // Hiding the CircularProgressIndicator.
      setState(() {
        visible = false;
      });

      // Showing Alert Dialog with Response JSON Message.
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: new Text(message),
            actions: <Widget>[
              FlatButton(
                child: new Text("OK"),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        },
      );
    }
  }
}
导入“包装:颤振/材料.省道”;
导入“dart:math”;
导入“dart:convert”;
将“package:http/http.dart”导入为http;
导入“dart:async”;
导入“包:颤振应用程序/菜单成员.dart”;
将“package:flatter_app/globals.dart”导入为globals;
类Lotto扩展StatefulWidget{
@凌驾
_LottoState createState()=>新的_LottoState();
}
类_lotostate扩展状态{
@凌驾
void initState(){
super.initState();
}
var i=1;
var nb_num=49;
var no_select=[];
var no_a_select=5;
List colorList=List.generate(49,(int index)=>Colors.lightBlue);
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:新的appBar(
标题:新文本(“乐透格栅”),
),
正文:
居中(
子:列(
儿童:[
容器(
宽度:400,
身高:30,
边距:仅限常量边集(顶部:10.0),
子项:新文本(“Selectionnez 5 numéros”,textAlign:textAlign.center,style:TextStyle(fontSize:30.0)),
),
容器(
宽度:400,
身高:300,
子项:新建GridView.count(
交叉轴计数:9,
填充:常数边集全部(30.0),
主轴间距:4.0,
交叉轴间距:4.0,
子项:新列表。生成(49,(索引){
返回新的手势检测器(
onTap:(){
设置状态(){
if(颜色列表[索引]==颜色.浅蓝色){
如果(无选择长度颜色浅蓝色);
var rng=新随机数();
列表选项卡=[];
tab=List.generate(49,(int-index)=>index+1);
打印(制表符);

而(j您可以复制粘贴运行下面的完整代码
您可以使用列表保留每个数字的颜色
代码片段

List<Color> colorList = List<Color>.generate(49, (int index) => Colors.lightBlue);
...
setState(() {
                    if (colorList[index] == Colors.lightBlue) {
                      if (no_select.length < no_a_select) {
                        colorList[index] = Colors.redAccent;
...
 child: Container(
                      color: colorList[index],  
List colorList=List.generate(49,(int index)=>Colors.lightBlue);
...
设置状态(){
if(颜色列表[索引]==颜色.浅蓝色){
如果(无选择。长度<无选择){
colorList[index]=Colors.redAccent;
...
子:容器(
颜色:颜色列表[索引],
工作演示

完整代码

import 'package:flutter/material.dart';

class Lotto extends StatefulWidget {
  @override
  _LottoState createState() => new _LottoState();
}

class _LottoState extends State<Lotto> {
  Color color;
  void message() {
    print('Clicked');
  }

  List<Color> colorList = List<Color>.generate(49, (int index) => Colors.lightBlue);

  @override
  void initState() {
    super.initState();

    color = Colors.lightBlue;
    print(colorList[0].toString());
  }

  var i = 1;
  var nb_num = 49;
  var no_select = [];
  var no_a_select = 5;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            children: <Widget>[
              Expanded(
                flex: 3,
                child: new GridView.count(
                  crossAxisCount: 7,
                  children: new List<Widget>.generate(49, (index) {
                    return new GestureDetector(
                      onTap: () {
                        setState(() {
                          if (colorList[index] == Colors.lightBlue) {
                            if (no_select.length < no_a_select) {
                              colorList[index] = Colors.redAccent;
                              no_select.add(index + 1);
                            } else {
                              print(
                                  "Vous ne pouvez pas sélectionner plus de 5 numéros !!!");
                            }
                            print(no_select);
                          } else {
                            colorList[index] = Colors.lightBlue;
                            no_select.remove(index + 1);
                            print(no_select);
                          }
                        });
                      },
                      child: Container(
                        child: ClipOval(
                          child: Container(
                            color: colorList[index],
                            height: 20.0,
                            width: 20.0,
                            child: Center(
                              child: new Text((index + 1).toString(),
                                  style: TextStyle(color: Colors.white, fontSize: 24),
                                  textAlign: TextAlign.center),
                            ),
                          ),
                        ),
                      ),
                    );
                  }),
                ),
              ),
              Expanded(flex: 1, child: Text("abc")),
            ],

          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Lotto(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
类Lotto扩展StatefulWidget{
@凌驾
_LottoState createState()=>新的_LottoState();
}
类_lotostate扩展状态{
颜色;
无效消息(){
打印(“单击”);
}
List colorList=List.generate(49,(int index)=>Colors.lightBlue);
@凌驾
void initState(){
super.initState();
颜色=颜色。浅蓝色;
打印(颜色列表[0].toString());
}
var i=1;
var nb_num=49;
var no_select=[];
var no_a_select=5;
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
正文:中(
子:列(
儿童:[
扩大(
弹性:3,
子项:新建GridView.count(
交叉轴计数:7,
子项:新列表。生成(49,(索引){
返回新的手势检测器(
onTap:(){
设置状态(){
if(颜色列表[索引]==颜色.浅蓝色){
如果(无选择。长度<无选择){
colorList[index]=Colors.redAccent;
无选择添加(索引+1);
}否则{
印刷品(
“你有五个选择!!!”;
}
打印(无选择);
}否则{
colorList[索引]=Colors.lightBlue;
无选择。删除(索引+1);
打印(无选择);
}
});
},
子:容器(
孩子:斜坡(
子:容器(
颜色:颜色列表[索引],
身高:20.0,
宽度:20.0,
儿童:中心(
子项:新文本((索引+1).toString(),
样式:TextStyle(颜色:Colors.white,字体大小:24),
textAlign:textAlign.center),
),
),
),
),
);
}),
),
),
扩展(弹性体:1,子体:文本(“abc”)),
],
),
),
),
);
}
}
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
首页:乐透(),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
 print(
   "Vous ne pouvez pas sélectionner plus de 5 numéros !!!");
showDialog(
     context: context,
     builder: (BuildContext context){
        return AlertDialog(
          title: Text("Alert Dialog"),
          content: Text("Vous ne pouvez pas sélectionner plus de 5 numéros !!!"),
          );
        }
     );
Edit:
          Expanded(
            flex:2,
            child:
            RaisedButton(
              color: Colors.green,
              textColor: Colors.white,
              padding: EdgeInsets.fromLTRB(9, 9, 9, 9),
              child: Center(child: Text('TIRAGE ALEATOIRE')),
              onPressed: () {
                Select_numbers();
              },
            ),
          ),
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: new AppBar(
          title: new Text('GRILLE DE LOTTO'),
        ),
        body:
        Center(
            child: Column(
                children: <Widget>[
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(40),),