我有一个 dart 文件作为“MyDrawer”,我想将此抽屉菜单添加到我的仪表板页面。这是我的代码。我不知道为什么它不起作用。
import 'package:flutter/material.dart';
class MyDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Menu drawer'),
decoration: BoxDecoration(
color: Colors.red,
),
),
ListTile(
leading: Icon(Icons.home, size: 40,),
title: Text('First item'),
subtitle: Text("This is the 1st item"),
trailing: Icon(Icons.more_vert),
onTap: () {
},
),
ListTile(
title: Text('Second item'),
onTap: () {
},
),
],
),
);}}
我在我的仪表板页面中调用了这个抽屉菜单。就是这样;
class Dash extends StatefulWidget { ...
class _DashState extends State<Dash> { ...
@override
void initState(){ ...
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: MyDrawer(),
body: Container(
... ),
答案 0 :(得分:0)
您的抽屉 MyDrawer
应该是一个返回 StatelessWidget
的 Drawer
。
你应该这样做:
class DashboardPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: MyDrawer(),
);
}
}
class MyDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Menu drawer'),
decoration: BoxDecoration(
color: Colors.red,
),
),
ListTile(
leading: Icon(Icons.home, size: 40,),
title: Text('First item'),
subtitle: Text("This is the 1st item"),
trailing: Icon(Icons.more_vert),
onTap: () {
},
),
ListTile(
title: Text('Second item'),
onTap: () {
},
),
],
),
);
}
}
答案 1 :(得分:0)
我所需要的只是添加一个应用栏。我绝对错过了这些细节。这是更新的版本。
from keras.applications.mobilenet_v2 import decode_predictions
#from keras.applications.imagenet_utils import decode_predictions
from keras.applications.imagenet_utils import preprocess_input
def relu6(x):
return K.relu(x, max_value=6)
def _conv_block(inputs, filters, kernel, strides, use_bias=True):
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
if nlay < 0 or nlay > 16:
basename = 'conv_%d' % (nlay + 1)
else:
basename = 'expanded_conv_%d_expand' % nlay
x = Conv2D(filters, kernel, padding='same', strides=strides, name=basename, use_bias=use_bias)
(inputs)
x = BatchNormalization(axis=channel_axis, name=basename + '_batch_normalization')(x)
return Activation(relu6, name=basename + '_activation')(x)
def _bottleneck(inputs, filters, kernel, t, s, r=False):
global nlay
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
#Create expansions layer only if needed (expansion factor >1)
if t > 1:
tchannel = K.int_shape(inputs)[channel_axis] * t
x = _conv_block(inputs, tchannel, (1, 1), (1, 1), use_bias=False)
else:
x = inputs
x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same',
name='expanded_conv_%d_depthwise' % nlay, use_bias=False)(x)
x = BatchNormalization(axis=channel_axis, name='expanded_conv_%d_depthwise_batch_normalization' %
nlay)(x)
x = Activation(relu6, name='expanded_conv_%d_depthwise_activation' % nlay)(x)
x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', name='expanded_conv_%d_project' % nlay,
use_bias=False)(x)
x = BatchNormalization(axis=channel_axis, name='expanded_conv_%d_project_batch_normalization' %
nlay)(x)
if r:
x = add([x, inputs], name="expanded_conv_%d_add" % nlay)
nlay += 1
return x
def _inverted_residual_block(inputs, filters, kernel, t, strides, n):
x = _bottleneck(inputs, filters, kernel, t, strides)
for i in range(1, n):
x = _bottleneck(x, filters, kernel, t, 1, True)
return x
def roundup(n):
x = (n + 6) // 8
return x * 8
def MobileNetv2(input_shape, k, width_multiplier=1.0):
global nlay
nlay = -1
inputs = Input(shape=input_shape)
x = _conv_block(inputs, roundup(int(32 * width_multiplier)), (3, 3), strides=(2, 2), use_bias=False)
nlay += 1
fix = 0
if width_multiplier - 1.3 < 0.01:
fix = -2
x = _inverted_residual_block(x, roundup(int(16 * width_multiplier)), (3, 3), t=1, strides=1, n=1)
x = _inverted_residual_block(x, roundup(int(24 * width_multiplier)), (3, 3), t=6, strides=2, n=2)
x = _inverted_residual_block(x, roundup(int(32 * width_multiplier)), (3, 3), t=6, strides=2, n=3)
x = _inverted_residual_block(x, roundup(int(64 * width_multiplier) + fix), (3, 3), t=6, strides=2,
n=4)
x = _inverted_residual_block(x, roundup(int(96 * width_multiplier)), (3, 3), t=6, strides=1, n=3)
x = _inverted_residual_block(x, roundup(int(160 * width_multiplier)), (3, 3), t=6, strides=2, n=3)
x = _inverted_residual_block(x, roundup(int(320 * width_multiplier)), (3, 3), t=6, strides=1, n=1)
last_conv_size = max(1280, int(1280 * width_multiplier))
x = _conv_block(x, last_conv_size, (1, 1), strides=(1, 1), use_bias=False)
x = GlobalAveragePooling2D()(x)
x = Reshape((1, 1, last_conv_size))(x)
x = Dropout(0.3, name='Dropout')(x)
x = Conv2D(k, (1, 1), padding='same', name='logits', use_bias=True)(x)
x = Activation('softmax', name='softmax')(x)
output = Reshape((k,), name='out')(x)
model = Model(inputs, output)
plot_model(model, to_file='MobileNetv2.png', show_shapes=True)
return model
if __name__ == '__main__':
model = MobileNetv2((224, 224, 3), 1000)
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print('Input image shape:', x.shape)
preds = model.predict(x)
print(':', decode_predictions(preds))