我如何在颤振中应用这个设计

时间:2021-02-22 16:15:15

标签: flutter dart flutter-layout textfield flutter-widget

我希望所有的视图都做得很好,我想用颤振应用图片中的设计,我创建了一个自定义的 appBar,但我不能这样做,我尝试使用定位小部件并对齐容器,但它没有工作,有人可以帮助我吗? enter image description here

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Column(
          children: [
            Container(
              height: 147,
              decoration: BoxDecoration(color: Theme.of(context).primaryColor),
            ),
            Container(
              alignment: Alignment(.40, -0.80),
              height: 50,
              width: 343,
              child: TextField(
                  textAlign: TextAlign.right,
                  decoration: InputDecoration(
                    filled: true,
                    fillColor: Colors.white24,
                    hintText: 'Data',
                    prefixIcon: Icon(Icons.search),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(20),
                      borderSide: BorderSide(width: 0, color: Colors.white12),
                    ),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(100),
                        borderSide:
                            BorderSide(width: 0, color: Colors.white12)),
                  )),
            )
          ],
        ),
      ),
    );
  }
}

3 个答案:

答案 0 :(得分:1)

使用 Stack() 和 Positioned()

return Scaffold(
  body: Container(
    height: double.infinity,
    width: double.infinity,
    child: Stack(
      children: [
        Container(
          height: 147,
          decoration: BoxDecoration(color: Theme.of(context).primaryColor),
          child: Padding(
            padding: EdgeInsets.only(bottom: 10.0, left: 10.0, right: 10.0),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    IconButton(
                      icon: Icon(Icons.menu, color: Colors.white),
                      onPressed: () => print('Test'),
                    ),
                    SizedBox(width: 10.0),
                    Text('Data', style: TextStyle(color: Colors.white, fontSize: 20.0)),
                  ],
                ),
                Icon(Icons.bubble_chart_rounded, color: Colors.white)
              ],
            ),
          ),
        ),
        Positioned(
          top: 120,
          child: Container(
            width: MediaQuery.of(context).size.width,
            child: Padding(
              padding: const EdgeInsets.only(right: 25, left: 25),
              child: Column(
                children: [
                  TextField(
                      textAlign: TextAlign.right,
                      decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white24,
                        hintText: 'Data',
                        prefixIcon: Icon(Icons.search),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(20),
                          borderSide: BorderSide(width: 0, color: Colors.white12),
                        ),
                        focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(100), borderSide: BorderSide(width: 0, color: Colors.white12)),
                      )),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Text('Data'),
                      Text('Data'),
                    ],
                  )
                ],
              ),
            ),
          ),
        ),
      ],
    ),
  ),
);

答案 1 :(得分:1)

这是一个例子,我的朋友。

只更改你的图标

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

 
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
    statusBarColor: Colors.transparent, // transparent status bar
  ));
  runApp(MyApp());
} 
 
class MyApp extends StatelessWidget {

  final _primaryColor = Color(0xff1877F2);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text('Data'),
          centerTitle: true,
          leading: IconButton(
            icon: Icon(Icons.menu), 
            onPressed: (){}
          ),
          elevation: 0,
          backgroundColor: _primaryColor,
          actions: [
            IconButton(
              icon: Icon(Icons.shopping_bag_rounded), 
              onPressed: (){}
            ),
          ],
        ),
        body: Stack(
          children: [
            Column(
              children: [
                Container(
                  height: 50,
                  color: _primaryColor,
                ),
                Expanded(child: Container())
              ],
            ),

            Positioned(
              top: 20,
              left: 16,
              right: 16,
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(
                      blurRadius: 5,
                      color: Colors.grey[400],
                      offset: Offset(0, 1),
                      spreadRadius: -1
                    )
                  ]
                ),
                child: TextField(
                    decoration: InputDecoration(
                      filled: true,
                      contentPadding: EdgeInsets.only(left: 32, top: 16, bottom: 16),
                      fillColor: Colors.white,
                      hintText: 'Search to find more products...',
                      suffixIcon: Icon(Icons.search),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10),
                        borderSide: BorderSide.none,
                      ),
                    )),
              ),
            ),

            Positioned(
              top: 80,
              left: 0,
              right: 0,
              bottom: 0,
              child: ListView.builder(
                itemBuilder: (_, i) {
                  return ListTile(
                    title: Text('Row $i'),
                  );
                }
              )
            )

          ],
        ),
      )
    );
  }
}

答案 2 :(得分:0)

尝试使用 Stack()Positioned() - 对齐 Stack 中的容器

https://api.flutter.dev/flutter/widgets/Stack-class.html