flutter 尝试使用一个线性梯度作为我的note_editor.dart的背景,但我似乎不能使它工作

mrwjdhj3  于 2022-12-24  发布在  Flutter
关注(0)|答案(1)|浏览(130)

我已经使它在我的note_cards.dart上工作,但是当我创建/打开note_card时,我似乎不能使它显示分配给我的卡片的渐变背景。欢迎任何帮助。提前感谢!
这是我的笔记编辑器。dart:

import 'dart:math';

import 'package:BetterNotes/style/app_style.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class NoteEditorScreen extends StatefulWidget {
  const NoteEditorScreen({Key? key}) : super(key: key);

  @override
  State<NoteEditorScreen> createState() => _NoteEditorScreenState();
}

class _NoteEditorScreenState extends State<NoteEditorScreen> {
  late int color_id;

  final TextEditingController _titleController = TextEditingController();
  final TextEditingController _mainController = TextEditingController();
  @override
  void initState() {
    super.initState();
    color_id = Random().nextInt(AppStyle.cardsColor.length);
  }

  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        iconTheme: const IconThemeData(color: Colors.black),
        title: const Text(
          "Add a new Note",
          style: TextStyle(color: Colors.black),
        ),
      ),
      body: FutureBuilder(
        future: AppStyle.cardsColor['color_id'],
        builder: (BuildContext context, AsyncSnapshot<List<Color>> snapshot) {
          if (snapshot.hasData) {
            return Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [snapshot.data![color_id], Colors.white],
                ),
              ),
              child: Padding(
                padding: const EdgeInsets.all(8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    TextField(
                      controller: _titleController,
                      decoration: const InputDecoration(
                          border: InputBorder.none, hintText: 'Note Title'),
                      style: AppStyle.mainTitle,
                    ),
                    const SizedBox(
                      height: 8,
                    ),
                    TextField(
                      controller: _mainController,
                      keyboardType: TextInputType.multiline,
                      maxLines: null,
                      decoration: const InputDecoration(
                          border: InputBorder.none, hintText: 'Note Content'),
                      style: AppStyle.mainContent,
                    ),
                  ],
                ),
              ),
            );
          } else {
            return Container();
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: AppStyle.accentColor,
        onPressed: () async {
          FirebaseFirestore.instance.collection('notes').add({
            'note_title': _titleController.text,
            'note_content': _mainController.text,
            'color_id': color_id
          }).then((value) {
            print(value.id);
            Navigator.pop(context);
          }).catchError(
              (error) => print('Failed to add new Note due to $error'));
        },
        child: const Icon(Icons.save),
      ),
    );
  }
}

这是我的app_style.dart:

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:google_fonts/google_fonts.dart';

class AppStyle {
  static Color bgColor = const Color(0xffffffff);
  static Color mainColor = const Color(0xff000000);
  static Color accentColor = const Color(0xFF0065FF);

  // Setting the Cards different Color

  static List<LinearGradient> cardsColor = [
    const LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.topRight,
        colors: [Colors.white, Colors.white]),
    LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.topRight,
        colors: [Colors.red.shade100, Colors.red.shade200]),
    LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.topRight,
        colors: [Colors.pink.shade100, Colors.pink.shade200]),
    LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.topRight,
        colors: [Colors.orange.shade100, Colors.orange.shade200]),
    LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.topRight,
        colors: [Colors.yellow.shade100, Colors.yellow.shade200]),
    LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.topRight,
        colors: [Colors.green.shade100, Colors.green.shade900]),
    LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.topRight,
        colors: [Colors.blue.shade100, Colors.blue.shade200]),
    LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.topRight,
        colors: [Colors.blueGrey.shade100, Colors.blueGrey.shade200]),
  ];

  // Setting the text style

  static TextStyle mainTitle =
      GoogleFonts.roboto(fontSize: 18, fontWeight: FontWeight.bold);

  static TextStyle mainContent =
      GoogleFonts.nunito(fontSize: 16, fontWeight: FontWeight.bold);

  static TextStyle dateTitle =
      GoogleFonts.nunito(fontSize: 16, fontWeight: FontWeight.w500);
}

我尝试在color_id上使用late和final,将主体更改为FutureBuilder。

vlf7wbxs

vlf7wbxs1#

这是一个非常简单的修复你不小心添加了列表在你的FutureBuilder,而不是列表,你试图通过在你的未来。

FutureBuilder(
        future: AppStyle.cardsColor['color_id'],
        builder: (BuildContext context, AsyncSnapshot<List<Color>> snapshot) {

试试这个:

FutureBuilder(
        future: AppStyle.cardsColor['color_id'],
        builder: (BuildContext context, AsyncSnapshot<List<LinearGradient>> snapshot) {

相关问题