设置Timepicker并使用http请求触发设置时间

时间:2020-03-11 05:29:01

标签: flutter

嗨,我想问一下如何在我的时间选择器中触发我的设置时间并在设置时间触发时发出http请求 导入'dart:async';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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


  final String title;

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

//此代码用于获取用户想要的时间

class _MyHomePageState extends State<MyHomePage> {
  TimeOfDay _time = TimeOfDay.now();
  TimeOfDay picked;
  Future<Null> selectTime(BuildContext context) async {
    picked = await showTimePicker(
    context: context, 
    initialTime: _time,
    );
    setState(() {
     print(_time);
      print(picked);

    });

  }

1 个答案:

答案 0 :(得分:0)

请查看以下代码。使用时间选择器设置时间后,会发出网络请求并在可用时显示结果。

main.dart

import 'package:flutter/material.dart';
import 'package:timepicker/post.dart';
import 'package:timepicker/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  TimeOfDay _selectedTime = TimeOfDay.now();
  String postTitle = "";

  Future<Null> selectTime(BuildContext context) async {
    TimeOfDay picked = await showTimePicker(
      context: context,
      initialTime: _selectedTime,
    );
    if (picked != null && picked != _selectedTime) {
      print(picked);
      Post post = await getPost();
      setState(() {
        _selectedTime = picked;
        postTitle = post.title;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text("${_selectedTime.toString()}"),
            SizedBox(
              height: 20,
            ),
            RaisedButton(
              onPressed: () => selectTime(context),
              child: Text("Select time"),
            ),
            SizedBox(
              height: 20,
            ),
            Text('$postTitle'),
          ],
        ),
      ),
    );
  }
}

post.dart

import 'dart:convert';

Post postFromJson(String str) => Post.fromJson(json.decode(str));

String postToJson(Post data) => json.encode(data.toJson());

class Post {
  int userId;
  int id;
  String title;
  String body;

  Post({
    this.userId,
    this.id,
    this.title,
    this.body,
  });

  factory Post.fromJson(Map<String, dynamic> json) => Post(
        userId: json["userId"],
        id: json["id"],
        title: json["title"],
        body: json["body"],
      );

  Map<String, dynamic> toJson() => {
        "userId": userId,
        "id": id,
        "title": title,
        "body": body,
      };
}

services.dart

import 'package:timepicker/post.dart';
import 'package:http/http.dart' as http;

String url = 'https://jsonplaceholder.typicode.com/posts';

Future<Post> getPost() async {
  final response = await http.get('$url/1');
  return postFromJson(response.body);
}