flutter 如何调整颜色选取器微件的大小

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

我在我的项目中使用flutter_colorpicker: ^1.0.3。我希望小部件被放置在页面上而不弹出对话框。很难调整小部件的大小和内部的颜色网格。就像这样:enter image description here
我试着用容器和SizedBox Package ,但结果不是我所期望的。
我期待:enter image description here
网格更小。

qaxu7uf2

qaxu7uf21#

这里的想法是为BlockPicker提供一个布局构建器。
我只做了最少的代码来展示UI。

import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        appBarTheme: const AppBarTheme(
          backgroundColor: Colors.transparent,
        ),
        useMaterial3: true,
      ),
      home: const ColorPickerExample(),
    );
  }
}

class ColorPickerExample extends StatelessWidget {
  const ColorPickerExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Edit'),
        leading: IconButton(
          onPressed: () {},
          icon: const Icon(Icons.arrow_back),
        ),
      ),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        const CircleAvatar(
                          backgroundImage:
                              NetworkImage('https://i.pravatar.cc/300?img=30'),
                          radius: 60,
                        ),
                        const SizedBox(
                          width: 30,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: const [
                            Text('Herman Yun'),
                            Divider(),
                            Text('foobar@example.com')
                          ],
                        ),
                      ],
                    ),
                    const SizedBox(
                      height: 15,
                    ),
                    Text(
                      'Select a color',
                      style: Theme.of(context).textTheme.headline5,
                      textAlign: TextAlign.start,
                    ),
                    const SizedBox(
                      height: 5,
                    ),
                    BlockPicker(
                      pickerColor: Colors.red,
                      onColorChanged: (_) {},
                      layoutBuilder: (context, colors, child) {
                        return GridView(
                          physics: const NeverScrollableScrollPhysics(),
                          shrinkWrap: true,
                          gridDelegate:
                              const SliverGridDelegateWithMaxCrossAxisExtent(
                            maxCrossAxisExtent: 100,
                            childAspectRatio: 1.0,
                            crossAxisSpacing: 10,
                            mainAxisExtent: 100,
                            mainAxisSpacing: 10,
                          ),
                          children: [for (Color color in colors) child(color)],
                        );
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(top: 25),
            child: ElevatedButton(
              onPressed: () {},
              child: const SizedBox(
                width: double.infinity,
                child: Center(child: Text('Save')),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

相关问题