微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我想在调用小部件时获取颜色

如何解决我想在调用小部件时获取颜色

我要在另一个调用列表视图的文件中创建一个ListView,但是我想在调用列表视图时在列表视图文件中发送颜色。

这是列表视图代码>

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:Flutter/material.dart';

class ListViewFromApi extends StatefulWidget {
  @override
  _ListViewFromApiState createState() => _ListViewFromApiState();
}

class _ListViewFromApiState extends State<ListViewFromApi> {
  var data;
  var url = "https://jsonplaceholder.typicode.com/photos";

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

  getData() async {
    var res = await http.get(url);
    data = jsonDecode(res.body);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.all(8.0),child: data != null
            ? ListView.builder(
                // itemCount: 6,itemBuilder: (BuildContext context,int index) {
                  return Container(
                    child: Card(
                      margin: EdgeInsets.only(bottom: 20),elevation: 50,shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(1.0),),child: Column(
                        children: <Widget>[
                          Row(
                            mainAxisAlignment: MainAxisAlignment.start,children: <Widget>[
                              Padding(
                                padding: const EdgeInsets.all(8.0),child: Image.asset(
                                  "assets/icons/avatar.png",height: 45,Padding(
                                padding: const EdgeInsets.only(left: 5),child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
                                    Text(
                                      'Muhammad Anus',style: TextStyle(
                                          fontWeight: FontWeight.bold),Text(
                                      '10/17/2020',style: TextStyle(
                                        fontSize: 12,],)
                            ],Padding(
                            padding: const EdgeInsets.all(8.0),child: Container(
                              width: MediaQuery.of(context).size.width * 0.90,height: MediaQuery.of(context).size.height * 0.20,color: Colors.purple,child: Center(
                                child: Padding(
                                  padding: const EdgeInsets.all(18.0),child: Text(
                                    data[index]["title"],style: TextStyle(
                                      color: Colors.white,fontWeight: FontWeight.bold,SizedBox(
                            height: 5,Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,children: <Widget>[
                              Image.asset(
                                "assets/icons/dil.png",height: 30,Image.asset(
                                "assets/icons/copy.png",height: 25,Image.asset(
                                "assets/icons/whatsapp.png",height: 35,Image.asset(
                                "assets/icons/share.png",);
                },)
            : Center(
                child: CircularProgressIndicator(),));
  }
}

这是主文件,我要在其中使用颜色>>

import 'package:Flutter/material.dart';

import '../../bottom_nav.dart';
import 'components/Drawer.dart';
import 'components/list_view.dart';
// import 'package:saasti_shayari/screens/home_screen/components/body.dart';

// ignore: camel_case_types
class Home_Screen extends StatefulWidget {
  @override
  _Home_ScreenState createState() => _Home_ScreenState();
}

// ignore: camel_case_types
class _Home_ScreenState extends State<Home_Screen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Home",style: TextStyle(
            color: Colors.white,fontFamily: "google_fonts/Title.ttf",body: ListViewFromApi(),> Here I Want to send color from this file to another file with this List With
            i am begineer in Flutter please help me

      drawer: MyDrawer(),bottomNavigationBar: MyBottomNavBar(),);
  }
}

我该怎么做,请指导我,当我从HomeScreen调用ListView小部件时,我想从主屏幕向ListView屏幕发送颜色吗?

解决方法

您需要使用颜色属性为ListViewFormApi类创建一个构造函数:

class ListViewFromApi extends StatefulWidget {
  
  Color color;
  ListViewFromApi({this.color});
  
  @override
  _ListViewFromApiState createState() => _ListViewFromApiState();
}

调用ListViewFromApi时,您需要执行以下操作:

body: ListViewFromApi(color : Colors.blue),

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。